ActionForward详解

struts-config.xml

<forward>可以看成是一个ActionForward对象,这个对象有两个属性name和path

ActionForward forward1 = new ActionForward ();

forward1 .setName("success");

forward1 .setPath("/success.jsp");

ActionForward forward2 = new ActionForward ();

forward2 .setName("error");

forward2 .setPath("/error.jsp");

map.put("success",forward1 );//这个map是封装action标签中的属性的ActionMapping对象拥有的

map.put("error",forward2 );


<action-mappings><action path="/testAction" type="com.xxc.action.TestAction" name="TestForm" scope="request"><forward name="success" path="/success.jsp"></forward><forward name="error" path="/error.jsp"></forward></action>
</action-mappings>



Action

public class TestAction extends Action{@Overridepublic ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception {return mapping.findForward("success");}
}


这个参数中的ActionMapping对象是封装了<action>标签中的各个属性,ActionMapping对应的是<action>标签。而不是说ActionMapping对象对应的是 <action-mappings>标签。如果在 struts-config.xml如下取名就好理解了

<action-mappings><!--将action改成action-mapping--><action-mapping path="/testAction" type="com.xxc.action.TestAction" name="TestForm" scope="request"><forward name="success" path="/success.jsp"></forward><forward name="error" path="/error.jsp"></forward></action-mapping>
</action-mappings>


所以action标签中有很多forward子标签,将其可以理解成ActionMapping对象中有一个map集合用于放actionForward对象。

那么execute最后返回的时候  mapping.findForward("success");也就不难理解了。可以理解成,ActionMapping对象在找它的map集合中key为success的对应的ActionForward。

存值map.put("success",forward1 );  取值 mapping.findForward("success");取到ActionForward对象,然后将ActionForward交给中央控制器ActionServlet去处理(获取path属性,然后跳转)




本文链接:https://my.lmcjl.com/post/11864.html

展开阅读全文

4 评论

留下您的评论.