SpringMultiAction的简单示例

发表于:2007-06-22来源:作者:点击数: 标签:
先是一个简单的eclipse的工程目录结构, 简单介绍我自己写的MethodNameResolver和UrlHandlerMapping PathMethodNameResolver,根据url取得调用MultiAction的方法名称,比如/user/userAdd.action,就可以调用到id为user的类的userAdd的方法来处理action packa

   
  先是一个简单的eclipse的工程目录结构,

SpringMultiAction的简单示例



  简单介绍我自己写的MethodNameResolver和UrlHandlerMapping



  PathMethodNameResolver,根据url取得调用MultiAction的方法名称,比如/user/userAdd.action,就可以调用到id为user的类的userAdd的方法来处理action



package com.zgc.test.multiaction;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.servlet.mvc.multiaction.MethodNameResolver;
import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException;
public class PathMethodNameResolver implements MethodNameResolver {
 public String getHandlerMethodName(HttpServletRequest request)
   throws NoSuchRequestHandlingMethodException {
  String uri=request.getRequestURI();
  int begin = uri.lastIndexOf(’/’);
  if (begin == -1) {
   begin = 0;
  }
  else {
   begin++;
  }
  int end;
  if (uri.indexOf(";") != -1) {
   end = uri.indexOf(";");
  }
  else if (uri.indexOf("?") != -1) {
   end = uri.indexOf("?");
  }
  else {
   end = uri.length();
  }
  String fileName = uri.substring(begin, end);
  if (fileName.indexOf(".") != -1) {
   fileName = fileName.substring(0, fileName.lastIndexOf("."));
  }
  return fileName;
 }
}



  DynamicUrlHandlerMapping,考虑到有的时候有特殊需求,所以就最后一个来指定方法,前面的来构成beanname,如/admin/user/viewUser.action,就可以查找到adminUser这个id的类,然后调用viewUser这个方法。用到了lazy load而不是一开始就把所有的bean都放到工厂,有利于服务的启动速度(最近老是觉得这个烦),也可以不实例化一辈子都用不到的类(这种类可以删除了)



package com.zgc.test.multiaction;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.handler.AbstractUrlHandlerMapping;
public class DynamicUrlHandlerMapping extends AbstractUrlHandlerMapping {
 protected Object lookupHandler(String urlPath) {
  Object handler = super.lookupHandler(urlPath);
  if (handler == null) {
   String[] paths=urlPath.split("/");
   String beanName="";
   for(int i=0;i<paths.length-1;i++){
    beanName+=StringUtils.capitalize( paths[i]);
   }
   registerHandler(urlPath,StringUtils.uncapitalize(beanName));
   handler=super.lookupHandler(urlPath);
  }
  return handler;
 }
}



  用到了一个简单的方法去viewName,我喜欢约定多于配置,于是有这么一个helper类



package com.zgc.test.multiaction;
public class MethodNameViewName {
 public static String getViewName(){
  StackTraceElement[] elements = (new Throwable()).getStackTrace();
  return elements[1].getMethodName();
 }
}

 



  再接着就是实现类了,很简单,示例吗



package com.zgc.test.multiaction;
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 ChannelManager extends MultiActionController {
 public ModelAndView channelList(HttpServletRequest request,HttpServletResponse response){
  return new ModelAndView(MethodNameViewName.getViewName(),"message","channelList");
 }
 public ModelAndView channelAdd(HttpServletRequest request,HttpServletResponse response){
  return new ModelAndView(MethodNameViewName.getViewName(),"message","channelAdd");
 }
}



  还有就是配置文件,因为autowire了,变得很简单



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
 "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="byName" default-lazy-init="false"
 default-dependency-check="none">
 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix"><value>/WEB-INF/view/</value></property>
  <property name="suffix"><value>.jsp</value></property>
 </bean>
 <bean id="handlerMapping" class="com.zgc.test.multiaction.DynamicUrlHandlerMapping">
 </bean>
 <bean id="methodNameResolver" class="com.zgc.test.multiaction.PathMethodNameResolver"/>
 <bean id="user" class="com.zgc.test.multiaction.UserManager">
 </bean>
 <bean id="channel" class="com.zgc.test.multiaction.ChannelManager">
 </bean>
</beans>



jsp很简单就是标识一下不同的页面,就不做展示了。启动之后就可以通过http://server:port/beanname/methodName.action,来访问了

原文转自:http://www.ltesting.net