1. 程式人生 > >SSM-SpringMVC-10:SpringMVC中PropertiesMethodNameResolver屬性方法名稱解析器

SSM-SpringMVC-10:SpringMVC中PropertiesMethodNameResolver屬性方法名稱解析器

port public 方法名 return ati AC schema 定義 try

------------吾亦無他,唯手熟爾,謙卑若愚,好學若饑-------------

上次的以繼承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配置文件改了嗎?

SSM-SpringMVC-10:SpringMVC中PropertiesMethodNameResolver屬性方法名稱解析器