Spring MultiAction的简单示例
2008-01-05 10:10:07 来源:WEB开发网先是一个简单的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<paths.length-1;i++){
beanName+=StringUtils.capitalize( paths[i]);
}
registerHandler(urlPath,StringUtils.uncapitalize(beanName));
handler=super.lookupHandler(urlPath);
}
return handler;
}
}
Tags:Spring MultiAction 简单
编辑录入:爽爽 [复制链接] [打 印]更多精彩
赞助商链接