本站首页    管理页面    写新日志    退出


«August 2025»
12
3456789
10111213141516
17181920212223
24252627282930
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>


阅读全文(7869) | 回复(-1) | 编辑 | 精华
 


回复:spring 未公开的秘密 ParameterMethodNameResolver的使用
原创空间,  软件技术

xiaowang2008发表评论于2008/7/19 21:43:12

haode 


个人主页 | 引用回复 | 主人回复 | 返回 | 编辑 | 删除
 


» 1 »

发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.340 second(s), page refreshed 144765787 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号