struts2 ActionFoward工作流程:
struts提供了ActionForward和ActionMapping两个类来控制页面转发。
- ActionFoward
在使用struts框架开发web应用程序时,Action在完成用户逻辑处理后,需要把处理结果展示给用户,这个时候就需要程序控制页面的转发,在struts中使用ActionForward对象控制程序转向。ActonForward对象是一种配置对象,代表了一般的web资源,可以是jsp页面,servlet以及其他的Action,ActonForward对象映射的是struts配置文件struts-config.xml中的元素,在这个元素中封装了目标页面的URI。ActionForward是struts中的核心类,以下主要参数。
(1)Name属性:指定ActionForward名称,在其他类中也可以通过这个名字调用ActionForward。
(2)Path属性:指明了ActionForward对应的URI。
(3)redirect属性:属性设置为true时被设置为重定向,默认false。
在struts中ActionForward有全局转发ActionForward和局部转发ActionForward,全局ActionForward应用于全部的Action和局部的转发仅仅对于当前的Action有效。- ActionMapping
在struts-config.xml配置文件中,每个元素都对应着一个ActionMapping,当用户请求被ServletAction接受以后,ActionServlet会根据用户请求URL以及元素设定的path属性确定对应的ActionMapping对象,ActionMapping对象会告诉ActionServlet使用哪个Action对象处理用户请求。
ActionMapping描述了struts中用户请求路径和Action的映射关系,在struts中每个ActionMapping都是通过path属性和一个特定的用户请求URL关联。它负责转发用户请求给具体的Action,同时还转发了其他的一些相关信息,这种请求和处理动作之间的映射关系保存在struts-config.xml这个配置文件中,在web服务器初始化的时候,会加载这个配置文件,struts给每个Action都创建一个ActionMapping对象,用来提供给Action使用,当ActionServlet转发请求的时候,ActionMapping对象会被作为参数传递给Action的execute()方法。- 实例详解
(1)首先在struts-config.xml中注入需要的Form实体,如下:
<form-beans> <form-bean name="MatchedVideoForm" type="cn.vobile.datastruct.relation.MatchedVideoForm" /> </form-beans>
(2)在struts-config中配置ActionMaping与Foward,如下:
<action-mappings> <action path="/videoMatches" type="cn.vobile.videotracker.relation.VideoMatchesAction" name="MatchedVideoForm" validate="true" parameter="method" scope="request"> <forward name="getMatches" path="/videoMatchesGroup.jsp" /> <forward name="getMatches_nogroup" path="/videoMatches.jsp" /> <!-- forward redirect默认为false,所以此Action是在做转发,而不明重定向 --> </action> </action-mappings> <!-- name scope type分别是actionMapping的一个属性,它对应着action的需要访问的位置和数据来源,还有数据范围,<forward>标签配置action执行完返回以后,需要到达的地方。相关数据信息会被存储到ActionMapping中,ActionServelt自动地将ActionMapping传送到Action类execute()方法,Action将使用ActionMapping的findForward()方法,此方法返回一个指定名称的ActionForward,这样Action就完成了本地转发。-->
(3)后台在收到一个http请求之后的处理流程:<以目前的理解情况来看>
1. 浏览器发出url请求 2. 在Action对应的方法中收到请求参数和Form参数,例如: public ActionForward getLiteMatches(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {} 其中参入的参数从收到到转发均会由Servlet进行管理,比如说request经过Action方法业务处理之后会形成一个mapping,在jsp中可以使用${key}取出mapping中的数值。 3. 进行Action处理,构建mapping,返回ActionForward对象。例如: public ActionForward getLiteMatches(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { request.getSession().setAttribute(Constants.USER_LEVEL, Constants.LITE); request.setAttribute("method", MatchedVideo.VIDEO_MATCHES_LITE_METHOD); request.setAttribute("matchedurl", "videoMatchesLite.do?method=" + MatchedVideo.VIDEO_MATCHES_LITE_METHOD); return this.getForwardBasic(mapping, form, request, response); } public ActionForward getLiteMatches(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { //更新form request中的值,它们本质是key-value的map ... // String forward = getForward(show_mode, groupById, userLevel); return new ActionForward(forward); } private String getForward(String showMode, String groupById, int userLevel) { String forward = ""; if ("4".equals(groupById)) { if ("box view".equals(showMode)) { forward = "videoMatchesMultipart.jsp"; } else { forward = "videoMatchesLineMultipart.jsp"; } } else if (("1".equals(groupById)) || ("2".equals(groupById)) || ("3".equals(groupById))) { if ("box view".equals(showMode)) { forward = "videoMatchesGroup.jsp"; } else { forward = "videoMatchesLineGroup.jsp"; } } else { if ("box view".equals(showMode)) { forward = "videoMatches.jsp"; } else { forward = "videoMatchesLine.jsp"; } } return "/" + forward; } 4. jstl从actionmapping中取数据,渲染jsp文件,返回一个带数据的html静态页面
struts2 ActionFoward工作流程
最新推荐文章于 2021-03-06 12:17:49 发布