springmvc多个视图解析器管理跳转资源
项目结构
第一步:将所有资源交由springmvc管理
<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:spring-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
在springmvc配置文件中配置多个视图解析器
根据属性viewNames的value进行区分,*jsp*:处理器方法返回的资源包括jsp的则进来
<?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/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.yyjc.springmvc.handler"/>
<mvc:annotation-driven/>
<!--使用默认的Servlet来响应静态文件-->
<mvc:default-servlet-handler/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"/>
<property name="viewNames" value="*jsp*"/>
<property name="order" value="2"/>
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".html"/>
<property name="viewNames" value="i*"/>
<property name="order" value="1"/><!--order:设置优先级,order值越低,则优先级越高-->
</bean>
</beans>
第二步:处理器方法返回跳转资源
注:方法返回结果可以是ModelAndView或者String
@RequestMapping("/userlogin.action")
public String userLogin() {
System.out.println("进来了");
return "jsp/success";
}
/*
@RequestMapping("/userlogin.action")
public ModelAndView userLogin() {
ModelAndView modelAndView = new ModelAndView();
System.out.println("进来了");
modelAndView.setViewName("index");
return modelAndView;
}
*/