« | August 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | | | | | | | |
| 公告 |
戒除浮躁,读好书,交益友 |
Blog信息 |
blog名称:邢红瑞的blog 日志总数:523 评论数量:1142 留言数量:0 访问次数:9692045 建立时间:2004年12月20日 |

| |
[j2ee]spring 未公开的秘密 ParameterMethodNameResolver的使用  原创空间, 软件技术
邢红瑞 发表于 2008/7/11 16:11:48 |
一般情况下 我很少使用spring的MultiActionController,原因很简单,简单的AbstractController做了,复杂的SimpleFormController。即使使用也使用PropertiesMethodNameResolver。就是petclinc的例子<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/welcome.htm">clinicController</prop> <prop key="/vets.htm">clinicController</prop> <prop key="/findOwners.htm">findOwnersForm</prop> <prop key="/owner.htm">clinicController</prop> <prop key="/addOwner.htm">addOwnerForm</prop> <prop key="/editOwner.htm">editOwnerForm</prop> <prop key="/addPet.htm">addPetForm</prop> <prop key="/editPet.htm">editPetForm</prop> <prop key="/addVisit.htm">addVisitForm</prop> </props> </property> </bean> <!-- ========================= CONTROLLER DEFINITIONS ========================= -->
<!-- - This bean is a MultiActionController that manages general View rendering. - It uses the "clinicControllerResolver" bean below for method name resolution. --> <bean id="clinicController" class="org.springframework.samples.petclinic.web.ClinicController"> <property name="methodNameResolver" ref="clinicControllerResolver"/> <property name="clinic" ref="clinic"/> </bean>
<!-- - This bean is a MethodNameResolver definition for a MultiActionController. - It maps URLs to methods for the "clinicController" bean. --> <bean id="clinicControllerResolver" class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver"> <property name="mappings"> <props> <prop key="/welcome.htm">welcomeHandler</prop> <prop key="/vets.htm">vetsHandler</prop> <prop key="/owner.htm">ownerHandler</prop> </props> </property> </bean>现在要做一个根据不同的method的参数,调用不同的方法,思路很简单,使用ParameterMethodNameResolver,但是有一个问题,参数是axx.bxx.nnn,如何做是个问题。java不允许有axx.bxx.nnn。看了一下MultiActionController源码,因为调用MethodNameResolver ,使用GOF的策略模式,其实整个spring都是使用策略模式的,自己实现MethodNameResolver,然后注入到MultiActionController的子类。看了一下ParameterMethodNameResolver的源码,发现不必了继承或实现MethodNameResolver。
在ParameterMethodNameResolver的getHandlerMethodName方法中
if (methodName != null && this.logicalMappings != null) { // Resolve logical name into real method name, if appropriate. String originalName = methodName; methodName = this.logicalMappings.getProperty(methodName, methodName); if (logger.isDebugEnabled()) { logger.debug("Resolved method name '" + originalName + "' to handler method '" + methodName + "'"); } }就可以做到了这点。 private Properties logicalMappings; 注入这个就行啦!配置文件 <property name="logicalMappings"> <props> <prop key="axx.bxx.nnn">getnnn</prop> </props> </property> |
|
» 1 »
|