ps: it's relatively simple. It's used to lay the foundation for us to use springboot
I. project construction
(1) guide packet
<!-- servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> <scope>provided</scope> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>5.1.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.1.7.RELEASE</version> </dependency> <!-- logback --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> <scope>test</scope> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-access</artifactId> <version>1.2.3</version> </dependency>
(II) log configuration
-
logback.xml
<?xml version="1.0" encoding="utf-8" ?> <configuration> <contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator"> <resetJUL>true</resetJUL> </contextListener> <jmxConfigurator/> <appender name="console" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>logback: %d{HH:mm:ss.SSS} %logger{36} - %msg%n</pattern> </encoder> </appender> <!-- take springweb The following package is set to debug,Error reporting information will be more comprehensive --> <logger name="org.springframework.web" level="debug"/> <root level="info"> <appender-ref ref="console" /> </root> </configuration>
(3) web configuration
-
WebApplicationInitializer is an interface provided by Spring to configure Servlet 3.0 + configuration, thus realizing the position of replacing web.xml. Implementing this interface will be automatically retrieved by the SpringServletContainerInitializer (used to start the Servlet3.0 container).
-
Create a new WebApplicationContext, register the configuration class, and associate it with the current servletContext.
-
Register the dispatcher servlet for Spring MVC.
public class WebInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.register(SpringMvcConfig.class); context.setServletContext(servletContext); ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(context)); dispatcher.addMapping("/"); dispatcher.setLoadOnStartup(1); } }
(IV) spring MVC configuration
-
@EnableWebMvc will enable some default configurations, such as ViewResolver and MessageConverter
-
Note: no matter which implementation class the view parser (the core rendering mechanism of spring MVC) uses, its method name (i.e. id) should write the name of its interface
@Configuration @EnableWebMvc @ComponentScan("club.info.springmvc") public class SpringMvcConfig { /* view resolver */ @Bean public InternalResourceViewResolver viewResolver(){ return new InternalResourceViewResolver(){{ setPrefix("/"); setSuffix(".jsp"); setViewClass(JstlView.class); }}; } }
(five) page
-
index.jsp
<html> <body> <h2>Hello World!</h2> </body> </html>
II. Basic configuration
- Some custom configurations in spring MVC require us to inherit a class (WebMvcConfigurerAdapter)
- And mark @ EnableWebMvc on the configuration class
(I) static resource mapping
@Configuration @EnableWebMvc @ComponentScan("club.info.springmvc") public class SpringMvcConfig implements WebMvcConfigurer { /* Static resource mapping */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/img/**").addResourceLocations("classpath:/img/"); } }
(II) interceptor
-
custom interceptor
public class HelloInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("Preposition..."); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("Postposition..."); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("complete..."); } }
-
Configuring Interceptors
@Configuration @EnableWebMvc @ComponentScan("club.info.springmvc") public class SpringMvcConfig implements WebMvcConfigurer { /* Interceptor */ @Bean public HelloInterceptor helloInterceptor(){ return new HelloInterceptor(); } /* Configuring Interceptors */ @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(helloInterceptor()).addPathPatterns("/hello"); } }