WebMvcConfigurer接口使用——閱讀開源項目中的代碼


 作用:功能等同於原來的springMVC.xml。可以注冊視圖解析器,多部件解析器

 示例:  主配置類實現WebMvcConfigurer接口。

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@Configuration
@EnableWebMvc      //開啟支持webMVC
@ComponentScan("com.yrc.fileuplod")
public class MVCConfig  implements WebMvcConfigurer {

    //配置內置解析器
    @Bean
    public InternalResourceViewResolver viewResolver(){
        InternalResourceViewResolver viewResolve = new InternalResourceViewResolver();
        viewResolve.setPrefix("/WEB-INF/classes/views");
        viewResolve.setSuffix(".jsp");
        viewResolve.setViewClass(JstlView.class);
        return viewResolve;
    }

  //配置多部件解析器 @Bean
public MultipartResolver multipartResolver() { CommonsMultipartResolver resolver = new CommonsMultipartResolver(); resolver.setMaxUploadSize(1000000); return resolver; } /* 視圖跳轉控制器 */ @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/index").setViewName("/index"); } }

 參考博客:https://www.cnblogs.com/sueyyyy/p/11608900.html

 

 

補充1:springboot下啟動web工程,不建立Main方法。

 

  步驟1:主配置實現WebMvcConfigurer接口。略

 

  步驟2:定義web初始化類,功能等同於web.xml。需要實現WebApplicationInitializer接口

 1 import org.springframework.web.WebApplicationInitializer;
 2 import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
 3 import org.springframework.web.servlet.DispatcherServlet;
 4 
 5 import javax.servlet.ServletContext;
 6 import javax.servlet.ServletException;
 7 import javax.servlet.ServletRegistration;
 8 
 9 public class WebInit implements WebApplicationInitializer {
10     
11     //啟動初始化
12     @Override
13     public void onStartup(ServletContext servletContext) throws ServletException {
14         AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
15         //注冊springMVC配置
16         context.register(MVCConfig.class);
17         //設置servletContext上下文
18         context.setServletContext(servletContext);
19         
20         
21         ServletRegistration.Dynamic servlet =servletContext.addServlet("dispatcher",new DispatcherServlet(context));
22         //設置映射規則
23         servlet.addMapping("/");
24         
25         //設置啟動權限
26         servlet.setLoadOnStartup(1);
27 
28 
29     }
30 }
WebApplicationInitializer詳解:

  參考博客:https://blog.csdn.net/zq17865815296/article/details/79464403

 

  步驟3:建立控制器

  

 1 import org.apache.commons.io.FileUtils;
 2 import org.springframework.stereotype.Controller;
 3 import org.springframework.web.bind.annotation.RequestMapping;
 4 import org.springframework.web.bind.annotation.RequestMethod;
 5 import org.springframework.web.bind.annotation.ResponseBody;
 6 import org.springframework.web.multipart.MultipartFile;
 7 
 8 import java.io.File;
 9 import java.io.IOException;
10 
11 /**
12  * Created by sang on 16-12-16.
13  */
14 @Controller
15 public class UploadController {
16     @ResponseBody
17     @RequestMapping(value = "/upload",method = RequestMethod.POST,produces = "text/plain;charset=UTF-8")
18     public String upload(MultipartFile file) {
19         try {
20             FileUtils.writeByteArrayToFile(new File("/home/sang/workspace/"+file.getOriginalFilename()),file.getBytes());
21             return "上傳成功";
22         } catch (IOException e) {
23             e.printStackTrace();
24             return "上傳失敗";
25         }
26     }
27 }

 

 補充2:案例為實現文件上傳,在不實現main方法的前提下,想啟動web工程需要將工程的打包方式設置為war

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM