spring mvc: 多解析器映射(资源绑定视图解析器 + 内部资源[普通模式/]视图解析器)
资源绑定视图解析器 + 内部资源(普通模式)视图解析器 并存方式
内部资源视图解析器:
http://localhost:8080/guga2/student/bate
http://localhost:8080/guga2/student/bate
资源绑定视图解析器:
http://localhost:8080/guga2/hello/test
项目:guga2
包:springmultiaction
views3.properties需要放在/WEB-INF/class/目录下
配置文件web.xml, applicationContext, springmultiaction-servlet.xml, views3.properties
web.xml
<!--配置文件路径--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <!-- 字符过滤器 --> <filter> <filter-name>encodingFilter</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>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 监听转发 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>springmultiaction</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmultiaction</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
applicationContext.xml自动引入bean
<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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 默认:注解映射支持 --> <mvc:annotation-driven/> <!-- 静态资源配置 --> <mvc:resources location="/pages/**" mapping="/pages/"/> <!-- 自动扫描包名,controller --> <context:component-scan base-package="springmultiaction"/> </beans>
springmultiaction-servlet.xml资源绑定视图解析器+内部资源视图解析器
<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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver"> <property name="basename" value="views3"/> <property name="order" value="0"/> </bean> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> <property name="order" value="1"/> </bean> </beans>
views3.properties
hello.(class)=org.springframework.web.servlet.view.JstlView hello.url=/WEB-INF/jsp/test.jsp
jsp大同小异
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page isELIgnored="false" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>hello test</title> </head> <body> hi,${message} </body> </html>