zoukankan      html  css  js  c++  java
  • [转]spring的ParameterMethodNameResolver

    例1:

    ParameterMethodNameResolver,这个可以根据请求的参数来确定一个需要调用的方法。
    例如,http://www.sf.net/index.view?testParam=testIt,这个请求会调用名称为testIt的处理方法。

    1:新建项目test

    2:在web.xml中配置如下:

     <servlet>
      <servlet-name>spring</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <load-on-startup>2</load-on-startup>
     </servlet>
     <servlet-mapping>
      <servlet-name>spring</servlet-name>
      <url-pattern>/spring/*</url-pattern>
     </servlet-mapping>

    3:控制器:

    package cn.iwoo.server.controller;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

    public class BController extends MultiActionController {
     public ModelAndView doSearch(HttpServletRequest request,HttpServletResponse response) throws Exception{
      System.out.println(this.getClass()+""+"doSearch()");//得到现在运行的class
      return new ModelAndView("index");//返回试图名
     }
    }

    4:编辑ModelAndView视图在WEB-INF下新建jsp,把默认的index.jsp放到里面:

    5:在WEB-INF下面新建spring-servlet.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:p="http://www.springframework.org/schema/p"
     xmlns:context="http://www.springframework.org/schema/context"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">
          
           <!-- <context:component-scan base-package="cn.iwoo.server.controller"/> --><!-- 扫描web包,应用Spring的注解 -->
           <!-- 视图解析规则 -->
           <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
           p:viewClass="org.springframework.web.servlet.view.JstlView"
           p:prefix="/WEB-INF/jsp/"
           p:suffix=".jsp"/>
          
           
           <bean id="urlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
             <property name="mappings">
              <props>
               <prop key="/user.view">bController</prop>
              </props>
             </property>
           </bean>
          
           <!-- 根据请求的参数确定调用的方法 -->
           <bean id="bMethodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
             <property name="paramName" value="method"></property>
             <property name="defaultMethodName" value="doSearch"></property>              
           </bean>      

           
           <bean id="bController" class="cn.iwoo.server.controller.BController">
             <property name="methodNameResolver">
              <ref local="bMethodNameResolver"/>
             </property>
           </bean>     
    </beans>

    6:在浏览器中输入:

    http://localhost:8080/test/spring/user.view?method   会调用配置文件配置的默认的方法doSearch

    得到结果如下:

    方法名称解析器MethodNameResolver之一ParameterMethodNameResolver! - 刘立 - 刘立的博客
     
    例2:
    spring的ParameterMethodNameResolver的可以根据不同的参数执行不同方法,类似Struts的dyncaction
    下面有一个例子
    package ee.bug;

    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
    public class ProductController extends MultiActionController {

    /**
      * Handles view
      */
    public ModelAndView view(HttpServletRequest request, HttpServletResponse response) throws Exception {

      response.getOutputStream().print("Viewing");

      return null;
    }

    /**
      * Handles index
      */
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response) throws Exception {
      response.getOutputStream().print("index");
      return null;
    }

    }
     
    配置文件:
    1.<bean id="productMethodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
    2.        <property name="paramName">
    3.    <value>method</value>
    4.     </property>
    5.       <property name="defaultMethodName">
    6.    <value>view</value>
    7.  </property>
    8.</bean>
    9.   
    10.<bean id="productController" class="ee.bug.ProductController">
    11.        <property name="methodNameResolver">
    12.    <ref local="productMethodNameResolver"/>
    13.  </property>
    14.</bean>

        注: <property name="paramName"><value>method</value></property>参数名为method 默认为action
        
    输入view.do?method=view 显示Viewing
    输入view.do?method=indedx 显示index
    输入/view.do?method=fdsf  显示404错误
  • 相关阅读:
    html5 file 上传文件
    JavaScript数组去重—ES6的两种方式
    音频资源下载
    whistle工具全程入门
    解构赋值
    节流(Throttling)和去抖(Debouncing)详解
    HTML5实现全屏
    Vue.js 子组件的异步加载及其生命周期控制
    6个Async/Await完胜Promise的原因
    Docker容器学习与分享08
  • 原文地址:https://www.cnblogs.com/ZhuRenWang/p/4777114.html
Copyright © 2011-2022 走看看