标签:
例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
得到结果如下:
[转]spring的ParameterMethodNameResolver
标签:
原文地址:http://www.cnblogs.com/ZhuRenWang/p/4777114.html