Controller加载控制(重点)
SpringMVC的处理器对应的bean必须按照规范格式开发,未避免加入无效的bean可通过bean加载过滤器进行包含设定或排除设定,表现层bean标注通常设定为@Controller
对于spring来说,它有它自己的配置文件。SpringMVC也有它自己的配置文件。这两个配置文件不能混用。
在进行配置的时候,这两个的配置文件是有一定的冲突的。所以最好是仅扫描springMVC它自己该扫描的那些东西,所以我们需要加一个控制,如下
-
测试
(1)使用注解过滤
静态资源加载
核心控制器拦截的是所有请求,需要对静态资源请求进行放行,通过配置放行资源实现
<!-- img下面的所有东西都给放行,对应的路径就是/img/-->
<mvc:resources mapping="/img/**" location="/img/"/>
<mvc:resources mapping="/js/**" location="/js/"/>
使用简化格式可以放行所有普通资源调用,无需一一枚举
<mvc:default-servlet-handler/>
中文乱码处理
-
SpringMVC提供专用的中文字符过滤器,用于处理乱码问题。在web.xml中进行配置
<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> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
注解驱动
-
上面的配置是使用xml进行配置,分别使用了web.xml和SpringMVC.xml在这里将这两个xml文件给删除掉,使用注解进行配置并且那够运行
spring-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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.yy"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- img下面的所有东西都给放行,对应的路径就是/img/--> <mvc:resources mapping="/img/**" location="/img/"/> <mvc:default-servlet-handler/> </beans>
web.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"> <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> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 加载核心配置文件--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:Spring-mvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <!-- 拦截所有的请求--> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
-
使用注解形式转化SpringMVC核心配置文件为配置类,即使用SpringMVCConfiguration和ServletContainersInitConfig分别替换SpringMVC.xml和web.xml
-
替换SpringMVC.xml
- 替换web.xml
-
SpringMVCConfiguration
package com.yy.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.FilterType; import org.springframework.stereotype.Controller; import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; //表明是一个配置 @Configuration //扫谁 @ComponentScan(value = "com.yy",includeFilters = @ComponentScan.Filter(type=FilterType.ANNOTATION,classes = {Controller.class}) ) public class SpringMVCConfiguration implements WebMvcConfigurer{ //注解配置放行指定资源格式 // @Override // public void addResourceHandlers(ResourceHandlerRegistry registry) { // registry.addResourceHandler("/img/**").addResourceLocations("/img/"); // registry.addResourceHandler("/js/**").addResourceLocations("/js/"); // registry.addResourceHandler("/css/**").addResourceLocations("/css/"); // } //注解配置通用放行资源的格式 @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable();; } }
-
ServletContainersInitConfig
package com.yy.config; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.filter.CharacterEncodingFilter; import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer; import javax.servlet.DispatcherType; import javax.servlet.FilterRegistration; import javax.servlet.ServletContext; import javax.servlet.ServletException; import java.util.EnumSet; public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer { //创建Servlet容器时,使用注解的方式加载SPRINGMVC配置类中的信息,并加载成WEB专用的ApplicationContext对象 //该对象放入了ServletContext范围,后期在整个WEB容器中可以随时获取调用 @Override protected WebApplicationContext createServletApplicationContext() { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.register(SpringMVCConfiguration.class); return ctx; } //注解配置映射地址方式,服务于SpringMVC的核心控制器DispatcherServlet @Override protected String[] getServletMappings() { return new String[]{"/"}; } @Override protected WebApplicationContext createRootApplicationContext() { return null; } //乱码处理作为过滤器,在servlet容器启动时进行配置,相关内容参看Servlet零配置相关课程 @Override public void onStartup(ServletContext servletContext) throws ServletException { super.onStartup(servletContext); CharacterEncodingFilter cef = new CharacterEncodingFilter(); cef.setEncoding("UTF-8"); FilterRegistration.Dynamic registration = servletContext.addFilter("characterEncodingFilter", cef); registration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST,DispatcherType.FORWARD,DispatcherType.INCLUDE),false,"/*"); } }