上篇说过,所有提交到web程序的url都被此UrlFilter拦截。拦截到请求后,UrlFilter则召集它的好多个得力干将Router 们, 询问他们:“谁能处理此URL啊 ?”
这时一位叫做AMP的Router 首当其冲 说:“这个url交给我了”。这时filter就会把此url全权交给AMPRouter来办,至于如何去处理,filter也不再过问,它觉得:“我把任务都交给你了,怎么解决是你的事”。
根据单一职责的原则,UrlFilter就负责上面情景中的分发url到Router中的差事,url如何分发交给Router处理。并且Router实际是一个接口,使用框架的用户完全可以自己实现Router,这样用户可以自主定义的url分发的策略。另外呢,框架初始化的一些操作它也是 推脱不掉的,像根据unicorn-config.xml初始化系统中的Router和Action'。下面是具体的代码:
@Override
public void init(FilterConfig config) throws ServletException {
application = config.getServletContext();
String loadPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
String classPath = loadPath.substring(1, loadPath.length());
ArrayList<String> actions = XMLReader.getNodeValues(classPath + "unicorn-config.xml", "actions");
this.initActions(actions);
ArrayList<String> routers = XMLReader.getNodeValues(classPath + "unicorn-config.xml", "routers");
this.initRouters(routers);
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
String path = request.getContextPath();
String uri = request.getRequestURI();
String relativeUri = uri.substring(path.length(), uri.length());
this.request = request;
this.session = request.getSession();
this.response = (HttpServletResponse) servletResponse;
// 用户自定义的Router优先级最高,url先通过用户定义的
Iterator<IPathRouter> iterator = routerList.iterator();
while(iterator.hasNext()) {
IPathRouter router = iterator.next();
if(router.route(relativeUri, this)) {
return ;
}
}
// 拦截不到的继续访问
filterChain.doFilter(servletRequest, servletResponse);
}
其中Router类的初始化,Action类的初始化于这个类似:
private void initRouters(ArrayList<String> routers) {
routerList = new ArrayList<IPathRouter>();
for (int i = 0; i < routers.size(); i++) {
String routerName = routers.get(i);
try {
Class<?> clz = Class.forName(routers.get(i));
// 单例模式通过方法获取对象实例
IPathRouter router = (IPathRouter) clz.newInstance();
routerList.add(router);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
}
// 最后把框架默认的Router加入进来
routerList.add(new AMPPathRouter());
}
其中unicorn-config.xml文件的编写,拿其中我一个项目里的这个文件来举例:
<?xml version="1.0" encoding="UTF-8" ?>
<config>
<routers>
<router class="com.mh.router.MySessionCheckRouter"></router>
</routers>
<actions>
<action class="com.mh.action.UserAction"></action>
<action class="com.mh.action.InformationAction"></action>
<action class="com.mh.action.UploadInformationIconAction"></action>
<action class="com.mh.action.TempPicAction"></action>
<action class="com.mh.action.MobileAction"></action>
</actions>
</config>
这里即定义了Action,也定义了自己的Router,并且从名称上可以看出,这个SessionCheckRouter是要判断所有提交到服务器的指定url的请求 是否已经登录过,没有登录,可能会把此请求遣送会登录页。以及初始化所有的Action,在Router处理完请求,分发给action时,可以从filter里面去取。
分享到:
相关推荐
使用RESTful Web Service实现转账业务,包括事务处理和并发控制
大神的Restful Web Service 详细介绍,Objectives By the end of this class, you should be able to: Give a working definition of RESTful Web Services actions, Enable a RESTful user in the SM operator ...
《Java RESTful Web Service实战》PDF版本下载
Building a RESTful Web Service with Spring 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权...
RESTful Web Services 中文版 高清 PDF 电子书
RESTful Web Service Primer RESTful Web Service Primer
Java RESTful Web Service实战.pdf Java RESTful Web Service实战.pdf
Restful web service cook book 中文版
Restful Web Service电子书 英文版
restful web service with sse, totourial
rest风格的web service实例,采用java语言编写完成,可为不同客户端提供CURD服务,连接数据表customer后可运行。
restful web service cookbook
本文展示了Http 协议的强大能力, 如何定义什么是Restful Web Service 架构以及以当今RPC 式Web 服务的对比,并解析了Restful web Service 架构的四个特征:可寻址性、无状态性、连通性和统一接口。
使用Spring 实现RESTful Web 服务 demo
因為剛入職 公司要求會使用struts2 restful風格,剛開始都沒聽說過restful,然後在網上找了N久的講解或demo,都沒找到,花了一週 在老大的指導下搞出來了,希望可以幫助到想學restful風格的同學們
Building a RESTful Web Service with Spring
本文展示了Http协议的强大能力,如何定义什么是Restful Web Service架构以及以当今RPC式Web服务的对比,并解析了Restful Web Service架构的四个特征:可寻址性、无状态性、连通性和统一接口.
使用Java创建RESTful Web Service
已经测试可用的,2018最新谷歌浏览器RESTful Web Service离线版包含definitions.json文件
building a restful web service