BeanNameViewResolver自定义视图解析器

RedirectView:定义外部资源视图对象

JstlView:定义内部资源视图对象

使用自定义视图解析器

<?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:tx="http://www.springframework.org/schema/tx" 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/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">


    <!--///////////////////////////////////////////////////////////////////////////////////-->
    <!--方式三:参数方法名解析器-->
   <bean id="methodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
       <property name="paramName" value="actionName"/>
   </bean>

    <!--视图解析器BeanNameViewResolver-->
    <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
    <!--定义内部资源试图对象-->
   <bean id="Inner" class="org.springframework.web.servlet.view.JstlView">
       <property name="url" value="/index.jsp"/>
   </bean>

    <!--定义外部资源视图对象-->
    <bean id="jd" class="org.springframework.web.servlet.view.RedirectView">
        <property name="url" value="https://www.jd.com"/>
    </bean>

    <!--处理器映射-->
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="urlMap">
            <map>
                <entry key="/hello" value="multiActionController"/>
            </map>
        </property>
    </bean>

    <!--处理器-->
    <bean id="multiActionController" class="cn.happy.day04BeanNameViewResolver.MyMultiActionController">
        <property name="methodNameResolver" ref="methodNameResolver"/>
    </bean>


</beans>

  实体类

package cn.happy.day04BeanNameViewResolver;

import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


//自定义视图解析器
public class MyMultiActionController extends MultiActionController {

    //自定义两个方法
    public String doFirst(HttpServletRequest request,HttpServletResponse response){
        return "jd";//定义外部资源视图id
    }

    public String doSecond(HttpServletRequest request,HttpServletResponse response){
        return "Inner";//返回内部资源视图id
    }

}

  

原文地址:https://www.cnblogs.com/xuchangqi1/p/8641071.html