1.动态ActionForm。
前文已经实现基于静态的ActionForm,而如果每一个表单都需要创建一个ActionForm,那么ActionForm就太多了。那么Struts可以通过动态的DynaActionForm来解决上述问题,只需要配置,而不用再创建新的ActionForm。
①配置:
<form-beans>
<!-- 定义DynaActionForm,至少指定两个属性: name(自定义) , type 指定Struts中的路径 -->
<form-bean name="dynaForm" type="org.apache.struts.action.DynaActionForm">
<form-property name="userName" type="java.lang.String"/>
<form-property name="pswd" type="java.lang.String"/>
</form-bean>
</form-beans>
②Action中取值,是通过强制转换form为DynaActionForm,且要得到的对象的值,类似map的key/value,getString(name),name就是属性名字。
public class DynaAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
DynaActionForm form2 = (DynaActionForm) form;
String userName = form2.getString("userName");
String pswd = form2.getString("pswd");
if ("admin".equals(userName) && "admin".equals(pswd)) {
System.out.println(userName+" " + pswd);
return mapping.findForward("error");
}
return mapping.findForward("error");
}
}
2.DispathAction
如果要对用户实现增删改查有2种方式:
①创建4个Action来实现功能,就显得很杂,难以管理。
②使用DispathAction,不用重写execute方法,而是定义自己需要实现的Action方法。
③配置:DispathAction在xml的配置中比Action多了一个parameter属性,该属性自定义了要去访问DispathAction方法的字段。
<action-mappings>
<action path="/login" type="com.example.UserAction" name="dynaForm_2"
scope="request" parameter="method">
<!-- 配置局部Forward -->
<forward name="welcome" path="/index.jsp" />
<forward name="input" path="/login.jsp" />
<forward name="main" path="/main.jsp"></forward>
<forward name="add" path="/add_success.jsp"></forward>
<forward name="delete" path="/delete_success.jsp"></forward>
<forward name="modify" path="/modify_success.jsp"></forward>
</action>
</action-mappings>
④实现:
public class UserAction extends DispatchAction{
public ActionForward add(ActionMapping arg0, ActionForm arg1,
HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
// TODO Auto-generated method stub
PrintWriter out=arg3.getWriter();
out.println("add!!!!!");
return arg0.findForward("add");
}
public ActionForward delete(ActionMapping arg0, ActionForm arg1,
HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
// TODO Auto-generated method stub
PrintWriter out=arg3.getWriter();
out.println("add!!!!!");
return arg0.findForward("delete");
}
public ActionForward modify(ActionMapping arg0, ActionForm arg1,
HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
// TODO Auto-generated method stub
PrintWriter out=arg3.getWriter();
out.println("add!!!!!");
return arg0.findForward("modify");
}
}
⑤访问方式:
<a href="login.do?method=add">add</a><br>
<a href="login.do?method=delete">delete</a><br>
<a href="login.do?method=modify">modify</a><br>
3.MappingDispathAction
使用DispathAction每次都是同一个parameter字段,容易出错,所以可以使用MappingDispathAction。
①配置:其中parameter中的名字要与Action中的名字一模一样。
<action path="/add" type="com.example.UserMapAction" name="dynaForm_2"
scope="request" parameter="add"></action>
<action path="/delete" type="com.example.UserMapAction" name="dynaForm_2"
scope="request" parameter="delete"></action>
<action path="/modify" type="com.example.UserMapAction" name="dynaForm_2"
scope="request" parameter="modify"></action>
②实现:
public class UserMapAction extends MappingDispatchAction{
public ActionForward add(ActionMapping arg0, ActionForm arg1,
HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
// TODO Auto-generated method stub
PrintWriter out=arg3.getWriter();
out.println("add!!!!!");
return arg0.findForward("add");
}
public ActionForward delete(ActionMapping arg0, ActionForm arg1,
HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
// TODO Auto-generated method stub
PrintWriter out=arg3.getWriter();
out.println("add!!!!!");
return arg0.findForward("delete");
}
public ActionForward modify(ActionMapping arg0, ActionForm arg1,
HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
// TODO Auto-generated method stub
PrintWriter out=arg3.getWriter();
out.println("add!!!!!");
return arg0.findForward("modify");
}
}
③访问方式:
<a href="add.do">add_mapping</a><br>
<a href="delete.do">delete_mapping</a><br>
<a href="modify.do">modify_mapping</a><br>