论坛首页 Java企业应用论坛

Spring三大URL路径解析器(附工程源代码ParameterMethodNameResolver http://localhost:9999/metho)

浏览 3436 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-06-26  

Spring有三个方法名称解析器(MethodNameResolver):

(1)ParameterMethodNameResolver

(2)InternalPathMethodNameResolver

(3)PropertiesMethodNameResolver

关于三个的使用方法,不必多说,网上可以搜到很多,这里只是简单的提下

ParameterMethodNameResolver在给定的路径后会有一个指定调用哪个方法的参数跟随

例如:http://localhost:9999/method/parameterMethodNameResolver?funcflg=getDate

getDate就是要调用的方法

InternalPathMethodNameResolver方法名包含在路径中

http://localhost:9999/method/internalPathMethodController/getDate

getDate就是要调用的方法

PropertiesMethodNameResolver 调用的方法写在=后面

http://localhost:9999/method/propertiesMethodController/suibian=getDate

getDate就是要调用的方法

 

相关的XML配置:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>method</display-name>
  <servlet>
  	<servlet-name>springmvc</servlet-name>
  	<servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:mvc.xml</param-value>
  	</init-param>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>springmvc</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

Spring的配置文件:

mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
	
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix"><value>/</value></property>
        <property name="suffix"><value>.jsp</value></property>
    </bean>
    <import resource="parameterMethod.xml" />   
    <import resource="internalPathMethod.xml" />  
    <import resource="propertiesMethod.xml" />  
    
</beans> 

parameterMethod.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <!--ParameterMethodNameResolver 开始 -->
    <bean id="paramResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
		<property name="paramName">
			<value>funcflg</value>
		</property>
		<property name="defaultMethodName" value="getDate"></property>  
	</bean>
	<!--ParameterMethodNameResolver 结束 -->
	
	<!-- ParameterMethodNameResolver业务方法  开始-->
	<bean name="/parameterMethodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.MultiActionController">
		<property name="methodNameResolver">
			<ref bean="paramResolver"/>
		</property>
		<property name="delegate">
			<ref bean="methodController"/>
		</property>
	</bean>
	<bean id="methodController" class="com.liuc.ui.ParameterMethodController"/>
	<!-- ParameterMethodNameResolver业务方法 结束-->
</beans>

  internalPathMethod.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <!--InternalPathMethodNameResolver 开始 -->
    <bean id="urlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    	<property name="mappings">
    		<props>
    			<prop key="/internalPathMethodController/*">internalPathMethodController</prop>
    		</props>
    	</property>
    </bean>
    <!-- 根据请求的路径名称来调用特定的方法 -->
    <bean id="cMethodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver" >
    </bean>
	<!--InternalPathMethodNameResolver 结束 -->
	
	<!-- InternalPathMethodNameResolver业务方法  开始-->
	<bean id="internalPathMethodController" class="com.liuc.ui.InternalPathMethodController">
		<property name="methodNameResolver">
			<ref local="cMethodNameResolver"/>
		</property>
	</bean>
	<!-- InternalPathMethodNameResolver业务方法 结束-->
</beans>

propertiesMethod.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <!--PropertiesMethodNameResolver 开始 -->
    <bean id="urlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    	<property name="mappings">
    		<props>
    			<prop key="/propertiesMethodController/*">propertiesMethodController</prop>
    		</props>
    	</property>
    </bean>
    <!-- 根据请求的路径名称来调用特定的方法 -->
    <bean id="cMethodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver" >
    	<property name="mappings" value="/propertiesMethodController/*=getDate"/>
    </bean>
	<!--PropertiesMethodNameResolver 结束 -->
	
	<!-- PropertiesMethodNameResolver业务方法  开始-->
	<bean id="propertiesMethodController" class="com.liuc.ui.PropertiesMethodController">
		<property name="methodNameResolver">
			<ref local="cMethodNameResolver"/>
		</property>
	</bean>
	<!-- PropertiesMethodNameResolver业务方法 结束-->
</beans>
 

最后工程的访问路径为

 

ParameterMethodNameResolver  http://localhost:9999/method/parameterMethodNameResolver?funcflg=getDate

InternalPathMethodNameResolver  http://localhost:9999/method/internalPathMethodController/getDate

PropertiesMethodNameResolver  http://localhost:9999/method/propertiesMethodController/suibian=getDate

 

 

工程源代码见附件

 

 

 

 

 

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics