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