Chinaunix首页 | 论坛 | 博客
  • 博客访问: 663461
  • 博文数量: 779
  • 博客积分: 5000
  • 博客等级: 大校
  • 技术积分: 5000
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-27 13:28
文章分类

全部博文(779)

文章存档

2011年(1)

2008年(778)

我的朋友

分类:

2008-10-27 13:28:37

  先是一个简单的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了,变得很简单



 "">
 default-dependency-check="none">
 
  /WEB-INF/view/
  .jsp
 

 
 

 
 
 

 
 


jsp很简单就是标识一下不同的页面,就不做展示了。启动之后就可以通过,来访问了

【责编:Star】

--------------------next---------------------

阅读(411) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~