Username: Password:

SpringMultiAction的简单示例
来源:linux宝库作者:linux宝库 发布时间:2007-09-30 00:00:00


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

  

  简单介绍我自己写的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
  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了,变得很简单

  



  
  "http://www.springframework.org/dtd/spring-beans.dtd">

  
  default-dependency-check="none">

  

  /WEB-INF/view/

  .jsp

  


  

  


  

  

  


  

  


  


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

喜欢本文,那就收藏到:

    Del.icio.us Google书签 Digg Live Bookmark Technorati Furl Yahoo书签 Facebook 百度搜藏 新浪ViVi 365Key网摘 天极网摘 和讯网摘 博拉网 POCO网摘 添加到饭否 QQ书签 Digbuzz我挖网
相关评论  我也要评论
还没有关于此文章的相关评论!
  • 昵称: (为空则显示guest)
  • 评论分数: ★ ★ ★★★ ★★★★ ★★★★★
  • 评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
  • 导航
    赞助商
    文章类别
    订阅