SpringMVC中的视图解析器默认的是InternalResourceViewResolver类,在第一次写springmvc的案例的时候就是使用的这种
现在说一下两种配置方式
首先是 BeanNameViewResolver
这种配置方式就不需要原来的配置了,直接删掉原来的就可以了
首先springmvc.xml中要配置,其他的不变
<!--视图解析器BeanNameViewResolver--> <bean id="viewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver"></bean>
<!--外部试图对象-->
<bean id="outViewObject" class="org.springframework.web.servlet.view.RedirectView">
<property name="url" value="https://www.jd.com"></property>
</bean>
<!--内部视图解析器-->
<bean id="innerViewObject" class="org.springframework.web.servlet.view.JstlView">
<property name="url" value="/second.jsp"></property>
</bean>
在后台的类的方法中
package demo06ViewResolver; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.multiaction.MultiActionController; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Created by mycom on 2018/3/18. */ public class MyViewResolver extends MultiActionController { //外部引用对象 public String doFirst(HttpServletRequest request, HttpServletResponse response){ return "outViewObject"; } public String doSecond(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { return "innerViewObject"; } }
我们也可以将配置文件中外部对象和内部对象放到另一个配置文件中,比如:myView.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--外部试图对象--> <bean id="outViewObject" class="org.springframework.web.servlet.view.RedirectView"> <property name="url" value="https://www.jd.com"></property> </bean> <!--内部视图解析器--> <bean id="innerViewObject" class="org.springframework.web.servlet.view.JstlView"> <property name="url" value="/second.jsp"></property> </bean> </beans>
那么在原来的配置文件中写什么呢,我把整个配置文件弄过来,我标记的就是需要改动的
<?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" 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"> <!--注册处理器--> <bean id="myMultiActionController" class="demo06ViewResolver.MyViewResolver" > <property name="methodNameResolver" ref="methodNameResolver"></property> </bean> <!--视图解析器BeanNameViewResolver--> <bean id="viewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver"></bean> <!--XmlViewResolver--> <bean class="org.springframework.web.servlet.view.XmlViewResolver"> <property name="location" value="classpath:myView.xml" ></property> </bean> <!--参数方法名称解析器--> <!--<bean id="parameterMethodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver"> <property name="paramName" value="actionName"></property> </bean>--> <!--属性方法名称解析器--> <bean id="methodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver"> <property name="mappings"> <props> <prop key="/first">doFirst</prop> <prop key="/second">doSecond</prop> </props> </property> </bean> <!--处理器映射器--> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <!--方式一--> <property name="mappings"> <props> <prop key="/*">myMultiActionController</prop> </props> </property> </bean> </beans>