The Spring-SSM integration detail builds on XML for integration.
Create project configuration POM files, etc.
, Initializer to replace web. XML
Inheritance AbstractAnnotationConfigDispatcherServletInitializer
Create two class allocation configuration parent and child containers
Public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {/ * * * configuration class * / @ Override the parent container protected Class<? >[] getRootConfigClasses() { return new Class[]{SpringConfig.class}; } /** * subcontainer configuration Class (SpringMVC related configuration Class) */ @override protected Class<? >[] getServletConfigClasses() { return new Class[]{SpringMVCConfig.class}; } /** * Configure DispatcherServlet url-pattern */ @override protected String[] getServletMappings() {return new String[]{"/"}; }}Copy the code
Nature, Initializer
SpringConfig
Remember to add @bean or the IoC container will be lost
Static resource access
Create an asset under WebApp and create test.html which you can access using the contenxt address
- Implement the interface WebMvcConfigurer
/* Override public void Override public void Override public void Override configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); }Copy the code
The interceptor
You can create a custom interceptor myInterceptor.java and import it into SpringMvcconfig.java
- Add a custom interceptor
@Component
- @ComponentScan({“com.mj.interceptor”})
- The following
@Autowired
private MyInterceptor myInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(myInterceptor)
.addPathPatterns("/**")
.excludePathPatterns("/asset/**");
}
Copy the code
ViewResolver
I don’t care how much I care
- To create the test. The JSP
- Copy the code
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/page/");
resolver.setSuffix(".jsp");
return resolver;
}
Copy the code
- Create a Controller
@Controller
public class TestController {
@GetMapping("/test1")
public String test1() {
// /WEB-INF/page/test.jsp
return "test";
}
}
Copy the code
MessageConverters
Encoding for text and JSON
@Override public void configureMessageConverters(List<HttpMessageConverter<? > > converters) {/ / set to return to normal string coding StringHttpMessageConverter stringConverter = new StringHttpMessageConverter (); stringConverter.setDefaultCharset(StandardCharsets.UTF_8); converters.add(stringConverter); / / set the returned JSON data coding MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter (); jsonConverter.setDefaultCharset(StandardCharsets.UTF_8); converters.add(jsonConverter); }Copy the code
The test case
@getMapping ("/test2") @responseBody public String test2() {return "test3 "; }Copy the code
POST coding problem
I did it in web.xml
@Override public void onStartup(ServletContext servletContext) throws ServletException { super.onStartup(servletContext); / / add Filter FilterRegistration. Dynamic encodingFilter = servletContext. AddFilter (" CharacterEncodingFilter ", new CharacterEncodingFilter("UTF-8")); encodingFilter.addMappingForUrlPatterns(null, false, "/*"); }Copy the code
Converter
Time to turn the Date
Look at the pain points
- Create DateConverter
Remember to add springConfig.java to the parent container so that the container can also be used
@Component public class DateConverter implements Converter <String, Date>{ @Override public Date convert(String s) { try { return new SimpleDateFormat("yyyy-MM-dd").parse(s); } catch (ParseException e) { e.printStackTrace(); return null; }}}Copy the code
@ComponentScan({"com.mj.converter"})
Copy the code
- In springMVCConfig registry
Automatic injection
@Autowired
private DateConverter dateConverter;
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(dateConverter);
}
Copy the code
MultipartResolver
Add dependency Commons – Fileupload
< the dependency > < groupId > Commons fileupload - < / groupId > < artifactId > Commons fileupload - < / artifactId > < version > 1.4 < / version > </dependency>Copy the code
Configure the beans in SpringMVCConfig
@Bean
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
resolver.setDefaultEncoding("UTF-8");
return resolver;
}
Copy the code
@PostMapping("/test4")
@ResponseBody
public String test4(@RequestParam("name") String name, @RequestParam("photo")MultipartFile photo) {
System.out.println(photo);
System.out.println(photo.toString());
return "Success:" + name;
}
Copy the code
Verify WebApplicationInitializer interface
It’s not the point.
It prints as soon as it is deployed
public class WebInitializer2 implements WebApplicationInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { System.out.println("WebInitializer2 - onStartup"); }}Copy the code
public class WebInitializer2 implements WebApplicationInitializer { @Override public void onStartup(ServletContext ServletContext) throws ServletException {/ / parent container configuration AnnotationConfigWebApplicationContext springCtx = new AnnotationConfigWebApplicationContext(); springCtx.register(SpringConfig.class); / / by the listener load configuration information servletContext. AddListener (new ContextLoaderListener (springCtx)); / / child container configuration AnnotationConfigWebApplicationContext mvcCtx = new AnnotationConfigWebApplicationContext (); mvcCtx.register(SpringMVCConfig.class); ServletRegistration.Dynamic servlet = servletContext.addServlet( "DispatcherServlet", new DispatcherServlet(mvcCtx)); servlet.setLoadOnStartup(0); servlet.addMapping("/"); // filter FilterRegistration.Dynamic encodingFilter = servletContext.addFilter( "CharacterEncodingFilter", new CharacterEncodingFilter("UTF-8")); encodingFilter.addMappingForUrlPatterns(null, false, "/*"); }}Copy the code