扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
简单介绍我自己写的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,来访问了
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者