SSM-SpringMVC-11:SpringMVC中ParameterMethodNameResolver参数方法名称解析器

 

 

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

 

或许曾经的我们也见过一种方式http://localhost:8080/项目名/后台servlet?actionName=login&uname=admin&upwd=123

这种方式调度servlet并且传参数,这里我要表达什么呢?就是?后面可以拼接内容,

所以,此处的ParameterMethodNameResolver就是通过这种方式来访问到的方法名的

 

说一下案例使用步骤

  一,定义ParameterMethodNameResolver参数方法名称解析器

  二,将上方一定义的方法名称解析器注入自己定义的处理器的bean中

  三,在处理器映射器SimpleUrlHandlerMapping中将那个访问的key值,由/*,改为具体值,不能再通配符的方式

 

案例源码

  处理器类

 

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="parameterMethodNameResolver"></property>
    </bean>
    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!--第一步,参数方法名称解析器-->
    <bean id="parameterMethodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
        <!--这个actionName就是url问号?后面等于号=左边的那个参数名-->
        <property name="paramName" value="actionName"></property>
    </bean>

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

</beans>

 

 

 

  按照此配置方法,你的访问url为http://ip地址:tomcat端口号/项目名/处理器映射器配的key值?actionName=你处理器中的方法名

 

 

注意检查你有没有我上面的first.jsp和second.jsp页面,和web.xml有没有将自己最后写的xml配置文件引用上

 

转载于:https://www.cnblogs.com/DawnCHENXI/p/8640840.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值