Struts1 in-depth learning of SSH learning

1. Dynamic ActionForm.

The previous article has implemented a static-based ActionForm, and if every form needs to create an ActionForm, then there are too many ActionForms. Then Struts can use dynamic DynaActionForm to solve the above problems, only need to configure, without creating a new ActionForm.

①Configuration:

<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>
②The value in Action is obtained by forcing the form to DynaActionForm, and the value of the object to be obtained is similar to the key/value of map, getString(name), and name is the attribute 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

If you want to add, delete, modify, and check users, there are two ways:

① Creating 4 Actions to realize functions is very complicated and difficult to manage.

②Using DispathAction, you don't need to rewrite the execute method, but define the Action method you need to implement.

③Configuration: DispathAction has one more parameter attribute than Action in the xml configuration, which customizes the fields to access the DispathAction method.

<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>
④Realization:
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");
	}

}
⑤Access method:
        <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

The same parameter field is used every time when DispathAction is used, which is error-prone, so you can use MappingDispathAction.

①Configuration: The name in parameter must be exactly the same as the name in 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>
②Realization:

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");
	}

}
③Access method:

	<a href="add.do">add_mapping</a><br>
	<a href="delete.do">delete_mapping</a><br>
	<a href="modify.do">modify_mapping</a><br>



Guess you like

Origin blog.csdn.net/u010857795/article/details/50962533