springmvc多個檢視解析器管理跳轉資源
阿新 • • 發佈:2021-02-01
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;
}
*/