SSH Learning a STRUTS2 DTO

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.