The difference between Struts2 's property-driven and model-driven

Source: Internet
Author: User

The 1.struts2 property driver.

The Struts2 property driver refers to the name in each form of the JSP page in action that corresponds to a property in the action. Look at the following code snippet:


<form action= "register.do" name= "Registerform" method= "POST" >

Username:<input type= "text" name= "uname" ><br>
Password:<input type= "Password" name= "UPass" ><br>
Confirm: <input type= "password" name= "Rpass" ><br>
<input type= "Submit" value= "Submit" >

</form>


This is the most basic user registration form, it has three data to submit: Uname, UPass, Rpass, then the corresponding action should have three attributes (actually three property setter), see register.do The actual action definition class:


Package org.abu.csdn.action.user;

Import Com.opensymphony.xwork2.ActionSupport;

/**
* Struts2 property to drive demo action
* @author Abu
*
*/
public class Registeraction extends Actionsupport {

Private String uname;
Private String UPass;
Private String Rpass;

Public String Getuname () {
return uname;
}

public void Setuname (String uname) {
This.uname = uname;
}

Public String Getupass () {
return upass;
}

public void Setupass (String upass) {
This.upass = UPass;
}

Public String Getrpass () {
return rpass;
}

public void Setrpass (String rpass) {
This.rpass = Rpass;
}

@Override
Public String Execute () throws Exception {
return actionsupport.success;
}

}

You'll see that the three properties in Registeraction are exactly the same as the Name property in the Registerform form, yes, this is the property driver for Struts2, and when the form is submitted to registeraction, STRUTS2 will automatically assign values based on the Name property of the form to invoke the corresponding property setter in the action.


2.STRUTS2 Model-driven

STRUTS2 model-driven is somewhat similar to the actionform in struts1.x, where each action in struts1.x must have a actionform corresponding to it, and Struts2.0, each action also needs to provide a Pojo object, used to seal To install the form properties, look at the code:


<form action= "register.do" name= "Registerform" method= "POST" >

Username:<input type= "text" name= "uname" ><br>
Password:<input type= "Password" name= "UPass" ><br>
Confirm: <input type= "password" name= "Rpass" ><br>
<input type= "Submit" value= "Submit" >

</form>


The code of this form is exactly the same as above. Next look at the Pojo code, is actually the ordinary Java Bean:


Package org.abu.csdn.action.user;

Import Com.opensymphony.xwork2.ActionSupport;

/**
* Struts2 Property driver Java Bean for demo
* @author Abu
*
*/
public class User {

Private String uname;
Private String UPass;
Private String Rpass;

Public String Getuname () {
return uname;
}

public void Setuname (String uname) {
This.uname = uname;
}

Public String Getupass () {
return upass;
}

public void Setupass (String upass) {
This.upass = UPass;
}

Public String Getrpass () {
return rpass;
}

public void Setrpass (String rpass) {
This.rpass = Rpass;
}

}


I'm sorry, as in the case of the attribute driver, it is very simple, because the demo is the same example, but the method is different, but the next is not the same, see Registeraction code:


Package org.abu.csdn.action.user;

Import Org.abu.csdn.dto.User;

Import Com.opensymphony.xwork2.ActionSupport;
Import Com.opensymphony.xwork2.ModelDriven;

/**
* STRUTS2 Model-driven demo action
*
* @author Abu
*
*/
public class Registeraction extends Actionsupport implements modeldriven<user> {

private user user;

Public User GetUser () {
return user;
}

public void SetUser (user user) {
This.user = user;
}

@Override
Public String Execute () throws Exception {
return actionsupport.success;
}

Model-driven methods that must be implemented, as well as the only method in the Modeldriven interface
Public User Getmodel () {

return user;
}

}

See, there's a big difference between the property-driven action and the following list:

(1) The model-driven action must implement the Modeldriven interface, and to provide the corresponding generics, here is of course the Java bean used specifically.

(2) The implementation of the Getmodel method of Modeldriven is actually a simple return to the generic type of an object.

(3) The action provides a generic private object, here is the user object that defines a user, and provides the appropriate getter and setter.

Well, when the top three things are done, the action will automatically call the user's setter to assign the value of the Name property in the form to the property in user. The user object can be used by the servlet when the action's subsequent processing of the JSP page is the latter.

3. Is the property driven and model driven?

This problem bothers a lot of Struts2 beginners, and I offer some suggestions here:

(1) Ask you to unify the drive model that is used by the action in the entire system, either by attribute-driven or by model-driven.

(2) If the persistence Layer object in your db corresponds to one by one of the properties in the form, then use the model driver, after all, it looks like the code is much neater.

(3) If the form's properties are not one by one corresponding, then you should use attribute-driven, otherwise, your system must provide two beans, one corresponding to the form submitted data, the other with the persistence layer.


Look at the above example, in fact, the password confirmation rpass does not need to be put into the DB, but only for verifying the password, is not it? So if you use model-driven, that's the problem, and the use of attribute-driven words is a bit cumbersome, now let's adjust a look at my solution:


4. A complete example

(1) jsp page submitted by Form index.jsp

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "UTF-8"%>
<%@ taglib uri= "/struts-tags" prefix= "s"%>
<%
String path = Request.getcontextpath ();
String basepath = request.getscheme () + "://"
+ request.getservername () + ":" + request.getserverport ()
+ path + "/";
%>

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<base href= "<%=basePath%>" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
&LT;TITLE&GT;CSDN Home </title>
<body>
<s:a href= "usermanager/userregister.jsp" > User registration </s:a>

<form action= "register.do" name= "Sampleform" method= "POST" >
Username:<input type= "text" name= "uname" ><br>
Password:<input type= "Password" name= "UPass" ><br>
Confirm: <input type= "password" name= "Rpass" ><br>
<input type= "Submit" value= "Submit" >
</form>
</body>


(2) Definition of user


Package org.abu.csdn.action.user;

Import Com.opensymphony.xwork2.ActionSupport;

/**
* Struts2 Property driver Java Bean for demo
* @author Abu
*
*/
public class User {

Private String uname;
Private String UPass;

Public String Getuname () {
return uname;
}

public void Setuname (String uname) {
This.uname = uname;
}

Public String Getupass () {
return upass;
}

public void Setupass (String upass) {
This.upass = UPass;
}

}


(3) Definition of registeraction


Package org.abu.csdn.action.user;

Import Com.opensymphony.xwork2.ActionSupport;

/**
* Struts2 property to drive demo action
* @author Abu
*
*/
public class Registeraction extends Actionsupport {

Used to inject and copy the attributes of spring.


private user user;

Private String uname;
Private String UPass;
Private String Rpass;

Public String Getuname () {
return uname;
}

public void Setuname (String uname) {
This.uname = uname;
}

Public String Getupass () {
return upass;
}

public void Setupass (String upass) {
This.upass = UPass;
}

Public String Getrpass () {
return rpass;
}

public void Setrpass (String rpass) {
This.rpass = Rpass;
}

@Override
Public String Execute () throws Exception {

Invoking a method to copy a property to the user to facilitate subsequent service methods


Copyattribute ();

TODO: Write your own user business code
return actionsupport.success;
}


/**
* Note that this is an action global checksum
*/
@Override
public void Validate () {
To verify the password
if (! ( Upass.trim ()). Equals ((Rpass.trim ()))) {
Note that the fieldname parameter of Addfielderror here must be enclosed in quotation marks.
Cannot use Addfielderror (Rpass, "XXXXXX"), but must be Addfielderror ("Rpass", "XXXXX")
This.addfielderror ("Rpass", this
. GetText ("csdn.action.user.register.validate.verify"));
}
}


/**
* Copy the properties of the form attributes that need to be persisted to the user
* @author Abu
*
*/
private void Copyattributes () {

User.setuname (uname);

User.setupass (UPass);


}


}

(4) Struts.xml configuration

<?xml version= "1.0" encoding= "UTF-8"?
<! DOCTYPE struts public
   -//apache software foundation//dtd struts Configuration 2.1//en
    "Http://struts.apache.org/dtds/struts-2.1.dtd";
<struts>
    <package name= "root" extends= "Struts-default" namespace= "/usermanager";
         <action name= "Userregister" class= "org.abu.csdn.action.user.RegisterAction";
             <result name= "Success" >/index.jsp</result>
            <result name= "Input" >/usermanager/ Userregister.jsp</result>
        </action>                 
    </ Package>
</struts>

Struts2 Property-driven vs. Model-driven differences

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.