zoukankan      html  css  js  c++  java
  • 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配置文件引用上

  • 相关阅读:
    AJAX(XMLHttpRequest)进行跨域请求方法详解(二)
    AJAX(XMLHttpRequest)进行跨域请求方法详解(一)
    深入理解javascript
    仿360新闻的热搜图片,win8风格随机九宫格布局
    基于structs2的用户登录控制(filter)和基于spring mvc的用户登录控制的对比(基于“XX公共平台”ksplatform和XX社区系统myAcl1)
    hibernate 一对一 one to one的两种配置方式
    Hibernate 的复杂用法HibernateCallback
    Hibernate和spring中的session总结
    SpringMVC中使用Interceptor拦截器
    java中如何通过Class获取类的属性、方法、注释
  • 原文地址:https://www.cnblogs.com/DawnCHENXI/p/8640840.html
Copyright © 2011-2022 走看看