- 1 2 3xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 5http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- 6
- 7struts2 8class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterclass> 910
- 11struts212 /*</url-pattern>
- 13 </filter-mapping>
- 14 <welcome-file-list>
- 15 <welcome-file>index.jsp</welcome-file>
- 16 </welcome-file-list>
- 17 </web-app>
3. 基于 web 开发中 Struts2 框架的结构图:
1. 从结构上分析 Struts2 图解原理:
Struts2 的思想是基于 POJO(简单的老的 Java 对象) 的一种新思想而产生的。一度影响了重量级的容器。它的实现是靠用简单的 javabean 对象去完成各种复杂的功能。我们也可以把这一实现的过程称为 POJO。
在 Struts2 中是没有容器的。当客户端发送一个请求时,在服务器上会经过层层过滤器,最后到达 FilterDispatcher(Struts2 中的中央核心控制器), 中央核心控制器在通过层层的拦截器去找相应的 Action, 最后 Action 返给我们一个所访问的页面,由于在 Struts2 中的拦截器是上下文相互环绕的,所以,在当 Action 中的 Resulet 返给我们一个页面的同时还会在次进行后期的拦截器处理,就这样一层层的拦截,直到将最终所取得的信息传给 HttpServletResponse, 在接着就是 HttpServletResponse 会将得到的信息响应给 HttpServletRequest. 但在这个响应的过程中也再次经过了过滤器的过滤。直到信息完整的到达 HttpServletRequest。
2 从 web.xml 配置上:
- 1 2 3xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 5http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- 6
- 7struts2 8class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterclass> 910
- 11struts212 /*</url-pattern>
- 13 </filter-mapping>
- 14 <welcome-file-list>
- 15 <welcome-file>index.jsp</welcome-file>
- 16 </welcome-file-list>
- 17 </web-app>
3. 控制器:核心控制器:FilterDispatcher,在 web 应用中负责拦截所有的用户请求,如果用户的请求以 .ation 结尾,则该请求被传入到 struts2 框架处理。业务控制器: Action(在 struts2 中起作用的实际上不是用户自定义的 Action,而是系统自动生成的 action 代理,但该代理是以用户自定义的 action 为目标的)
4. 作用域对象:有前面的 struts1 中我们可以从 Action 的 execute 方法中将请求和响应当做参数传递给了 Action,但是在 Struts2 中我们会发现 Action 的 execute 方法是没有任何的参数,所以也就无法得到作用域对象。那么,Struts2 又是如何得到作用域对象的呢?分析:当 Struts2 的过滤器启动的时候,首先就会初始化一个叫做 ServletActionContext 的类。它掌管着所有的作用域对象。代码展示如下:
- 1 mport org.apache.struts2.ServletActionContext;
- 2 public class ContextAction implements Action {
- 3 public String execute() throws Exception {
- 4 //获得请求
- 5 HttpServletRequest request = ServletActionContext.getRequest();
- 6 //获得会话
- 7 HttpSession session = request.getSession();
- 8 //获得上下文对象
- 9 ServletContext application = ServletActionContext.getServletContext();
- 10
- }
- 11
- }
10. 在 js 页面做显示:OGNL
来源: http://www.bubuko.com/infodetail-2091275.html