视图解析器
在spring-mvc文件中编写
<?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:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--将Controller注入到容器当中 id就是浏览请求地址--> <!--<bean id="/firstController" class="com.spring.controller.FirstController"/>--> <!--扫描注解--> <context:component-scan base-package="com.spring"/> <!--Spring支持SpringMVC注解--> <mvc:annotation-driven/> <!--试图间隙器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
mvc静态资源拦截
方式一:基于DefaultServlet,默认访问前会执行DefaultServlet
<!-- 解决乱码的过滤器 --> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.css</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.jpg</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.bmp</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.png</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.gif</url-pattern> </servlet-mapping>
方式二:利用DefaultServlet放行资源
<!--Spring支持SpringMVC注解-->
<mvc:annotation-driven/>
方式三: 在Spring3.0.4之后,定义了专门用于处理静态资源请求的处理器
ResourceHttpRequestHandler。并且添加了<mvc:resources/>标签,专门用于解决静态资源无法访问问题。
location:静态资源所在目录
mapping: 对资源的请求
<mvc:resources mapping="/**" location="/img"></mvc:resources>