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{
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
return mapping.findForward("success");
}
}
<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属性,然后跳转)