注解配置SpringMVC
- [1. 初始化类,代替web.xml](#1. 初始化类,代替web.xml)
- [2. 创建SpringConfig配置类,代替spring的配置文件](#2. 创建SpringConfig配置类,代替spring的配置文件)
- [3. 创建SpringMVCConfig配置类,代替SpringMVC.xml配置文件](#3. 创建SpringMVCConfig配置类,代替SpringMVC.xml配置文件)
- [4. 项目结构](#4. 项目结构)
1. 初始化类,代替web.xml
Spring3.2
引入了一个便利的WebApplicationInitializer
基础实现,名为AbstractAnnotationConfigDispatcherServletInitializer
,当我们的类扩展了AbstractAnnotationConfigDispatcherServletInitializer
并将其部署到Servlet3.0
容器的时候,容器会自动发现它,并用它来配置Servlet
上下文。
web.xml
文件
xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--配置前端控制器DispatcherServlet-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--乱码过滤器-->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--put,post请求方式-->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
初始化类代替web.xml
java
/**
* 创建初始化类,代替web.xml
*/
public class WebInit extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
// 指定Spring的配置类
protected Class<?>[] getRootConfigClasses() {
return new Class[]{SpringConfig.class};
}
@Override
// 加载SpringMVC的配置类
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringMVCConfig.class};
}
@Override
// 指定DispatcherServlet的映射规则,即url-pattern
protected String[] getServletMappings() {
return new String[]{"/"};
}
@Override
// 添加过滤器--> web.xml中一般存在两个编码顾虑去和put,post请求过滤器
protected Filter[] getServletFilters() {
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
characterEncodingFilter.setEncoding("UTF-8");
characterEncodingFilter.setForceEncoding(true);
HiddenHttpMethodFilter hiddenHttpMethodFilter = new HiddenHttpMethodFilter();
return new Filter[]{characterEncodingFilter, hiddenHttpMethodFilter};
}
}
2. 创建SpringConfig配置类,代替spring的配置文件
@Configuration
表示该类为配置文件类
java
@Configuration
public class SpringConfig {
//ssm整合之后,spring的配置信息写在此类中
}
3. 创建SpringMVCConfig配置类,代替SpringMVC.xml配置文件
springmvc.xml
配置文件内容:
- 扫描组件
- 文件上传解析器
- 视图解析器themeleaf
- 默认的servlet
- mvc的注解启动
- 视图控制器
- 拦截器(无)
- 异常解析器(无)
xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.lsm.controller"></context:component-scan>
<!--上传文件, id名必须为multipartResolver-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</bean>
<!-- 配置Thymeleaf视图解析器 -->
<bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<property name="order" value="1"/>
<property name="characterEncoding" value="UTF-8"/>
<property name="templateEngine">
<bean class="org.thymeleaf.spring5.SpringTemplateEngine">
<property name="templateResolver">
<bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
<!--/WEB-INF/templates/xxx.html-->
<!-- 视图前缀 -->
<property name="prefix" value="/WEB-INF/templates/"/>
<!-- 视图后缀 -->
<property name="suffix" value=".html"/>
<property name="templateMode" value="HTML5"/>
<property name="characterEncoding" value="UTF-8" />
</bean>
</property>
</bean>
</property>
</bean>
<!--配置默认首页, 开启mvc的注解驱动-->
<mvc:annotation-driven />
<!--
配置默认的servlet处理静态资源,浏览器发送的请求会被DispatcherServlet处理,无法处理再交给DefaultServlet处理
-->
<mvc:default-servlet-handler/>
<mvc:view-controller path="/" view-name="index" />
</beans>
创建SpringMVCConfig
配置类,代替SpringMVC.xml
配置文件,需要实现WebMvcConfigurer
接口
java
/**
* SpringMVC配置文件中可以配置的内容:
* 1. 扫描组件---
* 2. 文件上传解析器---
* 3. 视图解析器themeleaf---
* 4. 默认的servlet----
* 5. mvc的注解启动---
* 6. 视图控制器---
* 7. 拦截器---
* 8. 异常解析器
*/
@Configuration
@ComponentScan("qc.lsm.controller") // 对应1: 扫描组件
@EnableWebMvc // 对应5: mvc的注解驱动
public class SpringMVCConfig implements WebMvcConfigurer {
@Override
// 对应6: 视图控制器
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
}
@Override
// 对应4: 默认视图servlet
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Bean
// 对应2: 文件解析器
public CommonsMultipartResolver getMultipartResolver(){
return new CommonsMultipartResolver();
}
@Override
// 对应7: 拦截器
public void addInterceptors(InterceptorRegistry registry) {
FirstInterceptor firstInterceptor = new FirstInterceptor();
registry.addInterceptor(firstInterceptor);
}
// 对应3: 视图解析器
// 配置生成模板解析器
@Bean
public ITemplateResolver templateResolver() {
WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
ServletContextTemplateResolver templateResolver = new
ServletContextTemplateResolver(
webApplicationContext.getServletContext());
templateResolver.setPrefix("/WEB-INF/templates/");
templateResolver.setSuffix(".html");
templateResolver.setCharacterEncoding("UTF-8");
templateResolver.setTemplateMode(TemplateMode.HTML);
return templateResolver;
}
//生成模板引擎并为模板引擎注入模板解析器
@Bean
public SpringTemplateEngine templateEngine(ITemplateResolver
templateResolver) {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver);
return templateEngine;
}
//生成视图解析器并未解析器注入模板引擎
@Bean
public ViewResolver viewResolver(SpringTemplateEngine templateEngine) {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setCharacterEncoding("UTF-8");
viewResolver.setTemplateEngine(templateEngine);
return viewResolver;
}
}