SSM-SpringMVC-10:SpringMVC中PropertiesMethodNameResolver属性方法名称解析器

------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------

上次的以继承MultiActionController可以实现一个处理器中多个处理方法,但是局限出来了,他们的url访问地址只能是与方法名称相同的,因为他默认的方法名称解析器是InternalPathMethodNameResolver

如果我们想为了程序的安全甚至更多考虑(从url简洁啊,同名啊,等等)所以引入了一个叫PropertiesMethodNameResolver属性方法名称解析器的这个东西

我说一下使用在上一个案例基础上使用的步骤

  1.在配置文件中写一个bean定义这个PropertiesMethodNameResolver这个属性方法名称解析器

  2.在自定义的继承MultiActionController的类的bean中注入上面的步骤一配置的那个方法名称解析器

具体使用,我给俩段代码就够了

  继承MultiActionContoller的类,我的类名和源码在下面

package cn.dawn.day05multiActioncontroller;

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 Dawn on 2018/3/23.
 */
public class MyMultiActionController extends MultiActionController{

    public String doFirst(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        ModelAndView me=new ModelAndView();
        me.setViewName("first");
        return "first";
    }

    public ModelAndView doSecond(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        ModelAndView me=new ModelAndView();
        me.setViewName("second");
        return me;
    }
}

  

  在自己的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" 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处理器-->
    <bean id="myMultiActionController" class="cn.dawn.day05multiActioncontroller.MyMultiActionController">
        <!--第二步=========注入方法名称解析器-->
        <property name="methodNameResolver" ref="propertiesMethodNameResolver"></property>
    </bean>
    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!--第一步=======属性方法名称解析器-->
    <bean id="propertiesMethodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
        <property name="mappings">
            <props>
                <prop key="/doone">doFirst</prop>
                <prop key="/dotwo">doSecond</prop>
            </props>
        </property>
    </bean>

    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <!--第一种方式-->
        <property name="urlMap">
            <map>
                <!--这儿需要改成/*,而不是之前写死的那种-->
                <entry key="/*">
                    <value>myMultiActionController</value>
                </entry>
            </map>
        </property>
    </bean>

</beans>

然后你网页访问的url地址就是那里那个key的值,

此处需要注意的是first.jsp和second.jsp你准备了吗?

web.xml你指向的xml配置文件改了吗?

原文地址:https://www.cnblogs.com/DawnCHENXI/p/8640749.html