[转]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
得到结果如下:
下面有一个例子
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">
5. <property name="defaultMethodName">
8.</bean>
9.
11. <property name="methodNameResolver">
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错误