Struts2 Domainmodel, Modeldriven receive parameters

Source: Internet
Author: User

First, Domainmodel (domain model)

1.

Application scenario: Generally we receive parameters in the action of STRUTS2 is usually the following way

 PackageCn.orlion.user;ImportCom.opensymphony.xwork2.ActionSupport; Public classUseractionextendsactionsupport{PrivateString username; PrivateString password;  PublicString GetUserName () {returnusername; }     Public voidSetusername (String username) { This. Username =username; }     PublicString GetPassword () {returnpassword; }     Public voidSetPassword (String password) { This. Password =password; }         PublicString Login () {System.out.println ("Username=" +username); System.out.println ("Password=" +password); returnSUCCESS; }}

This creates a problem when we want to get more attributes such as we want to add age, sex, birthday ... Wait, so we're going to write a huge Get\set method. Obviously undesirable, so you can use Domainmodel to receive parameters.

First create a package: Cn.orlion.model, and then create a user class, as follows:

 PackageCn.orlion.model; Public classUser {PrivateString username; PrivateString password; PrivateString age;  PublicString GetUserName () {returnusername; }     Public voidSetusername (String username) { This. Username =username; }     PublicString GetPassword () {returnpassword; }     Public voidSetPassword (String password) { This. Password =password; }     PublicString getage () {returnAge ; }     Public voidsetage (String age) { This. Age =Age ; }}

Change the top useraction to :

 PackageCn.orlion.user;ImportCom.opensymphony.xwork2.ActionSupport;ImportCn.orlion.model.User; Public classUseractionextendsactionsupport{Privateuser User;  PublicString Login () {System.out.println ("Username=" +user.getusername ()); System.out.println ("Password=" +User.getpassword ()); System.out.println ("Age=" +user.getage ()); returnSUCCESS; }         PublicUser GetUser () {returnuser; }         Public voidsetUser (user user) { This. user =user; }}

When accessing

Http://localhost:8080/Struts2Demo1/user/login.action?user.username=1&user.password=2&user.age= 3 o'clock will print out the username=1 on the console.

password=2

Age=3

2. DTO (data transferobject) data Transfer Object

The above code has been a good solution to the problem of too many properties, but there is still a problem: when we have a property can not be added to the user, (such as the registration page will usually have a confirmation password input box to receive Passwordconfim) What to do? This can be solved with a dto. Generates a domain object from a dto.

First create a package cn.orlion.dto, and then create a class userdto as follows:

 Packagecn.orlion.dto; Public classUserdto {PrivateString username; PrivateString password; PrivateString passwordconfirm; PrivateString age;  PublicString getage () {returnAge ; }     Public voidsetage (String age) { This. Age =Age ; }     PublicString GetUserName () {returnusername; }     Public voidSetusername (String username) { This. Username =username; }     PublicString GetPassword () {returnpassword; }     Public voidSetPassword (String password) { This. Password =password; }     PublicString getpasswordconfirm () {returnpasswordconfirm; }     Public voidsetpasswordconfirm (String passwordconfirm) { This. passwordconfirm =passwordconfirm; }}

Then useraction instead:

 PackageCn.orlion.user;ImportCom.opensymphony.xwork2.ActionSupport;ImportCn.orlion.dto.UserDTO;ImportCn.orlion.model.User; Public classUseractionextendsactionsupport{Privateuserdto userdto;  PublicString Login () {string Password=Userdto.getpassword (); String passwordconfirm=userdto.getpasswordconfirm (); SYSTEM.OUT.PRINTLN (Password+ "-" +passwordconfirm); if(!password.equals (passwordconfirm)) {System.out.println ("Two input passwords inconsistent"); returnERROR; } User User=NewUser ();        User.setusername (Userdto.getusername ());        User.setpassword (Userdto.getpassword ());        User.setage (Userdto.getage ()); //Add user Action ...                returnSUCCESS; }     Publicuserdto getuserdto () {returnuserdto; }     Public voidsetuserdto (userdto userdto) { This. Userdto =userdto; }}

Visit http://localhost:8080/struts2demo1/user/login.action?userdto.username=1&userdto.password=2& Userdto.passwordconfirm=2&userdto.age=3

That's all you can do.

Second, Modeldriven

This approach is to have the action class implement the Modeldriven interface:

 PackageCn.orlion.user;ImportCom.opensymphony.xwork2.ModelDriven;ImportCn.orlion.model.User; Public classUseractionImplementsModeldriven<user>{        Privateuser User;  PublicString Login () {System.out.println (User.getusername ());                System.out.println (User.getpassword ()); return"Success"; }         PublicUser GetUser () {returnuser; }         Public voidsetUser (user user) { This. user =user; } @Override
Because implementing an interface generic is specified as user, the return value type is not an object but a user. PublicUser Getmodel () {returnuser; }}

This will print 1, 2 when accessing the http://localhost:8080/Struts2Demo/user/login.action?user.username=1&user.password=2 console.

Struts2 Domainmodel, Modeldriven receive parameters

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.