The use of DTOs in STRUTS2 is to pass an object directly from the page (such as a class that you write) to the action
JSP can only pass basic types, struts dto more convenient
Two methods of use:
One, if the form, the individual attributes are declared the name of the instance
<form action= "w.action" method = "POST" > name: <input name= "W.name" type= "text" value= "admin"/>< br/> id:<input name= "w.id" type= "password" value= "Hellow" ><br/> <input type= "Submit" Value= "Add"/> </form>
Follow these steps:
The action's Java file needs to be instantiated, assuming that the instance name is W, then the action name in the corresponding struts.xml must be w, as well as the form's W
Struts.xml
<action name= "W" class= "com.m2dto.action.WorkerAction" ><result name= "Success" >/ok.jsp</result> </action>
Action.java
public class Workeraction extends Actionsupport {worker w = new Worker ();p ublic worker getw () {return w;} public void Setw (Worker w) {this.w = W;} @Overridepublic String Execute () throws Exception {System.out.println (W.getname ()); System.out.println (W.getid ()); return SUCCESS;}}
Second, if the form, the individual attributes do not declare the name of the instance, as follows
<form action= "u.action" method = "POST" > user name: <input name= "name" type= "text" value= "admin"/> <br/> userid:<input name= "id" type= "password" value= "Hellow" ><br/> <input type= " Submit "value=" Add "/> </form>
Then the Java class of action needs to be implements in addition to extends Actionsupport modeldriven<t>
T is the type of class that the action needs to accept
In addition to this, you will need:
(1) Define an instance and add a Get Set method
(2) plus the override Getmodel method
Here's an example:
public class AddUser extends Actionsupport implements modeldriven<worker> {private worker u = new Worker (); @Override Public worker Getmodel () {if (U = = null) {u = new Worker ();} return u;} Public Worker Getu () {return u;} public void SetU (Worker u) {this.u = u;} @Overridepublic String Execute () throws Exception {System.out.println ("user" + U.getname ()); System.out.println ("User" + U.getid ()); return SUCCESS;}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
SSH Learning a STRUTS2 DTO