The use of the Modeldriven of Struts2

Source: Internet
Author: User

We can divide it into two categories depending on the Action property: Field-driven (Property-driven) action and Model-driven (model-driven) action.

First, Field-driven (property-driven) Action,action has its own properties, which are generally the basic types of java. The form fields correspond directly to the properties of the action.
Second, the implementation of the Modeldriven interface can be directly in the action of the user object, for example, it will Objectgetmodel () to get the user into the valuestack. It can be understood to append the user's attribute to the action. Its main function is to achieve the Formbean function similar to struts.

In Struts2, a way of directly using domain objects is provided, is to have the action implement the Com.opensymphony.xwork2.ModelDriven interface, Modeldriven allows you to manipulate the domain objects in your application directly, allowing you to use the same objects at the web and business levels.

There is only one method for the Modeldriven interface

Public Object Getmodel () {
Returnnull;
}

The method returns an object model for receiving user input data, and the properties in this model object can be accessed directly through the (attribute name) Username. Instead of using (object name. Property name) User.username This format to access, the action also does not need to provide getter and setter methods to the object, but must be in action in the new operation
As follows

1//modeldriven to use generics Oh  2  3 public class Loginaction extends Actionsupport implements modeldriven<user>{  4  5 private static final long serialversionuid=-6434128483294080524l;  6  7//This must be new  8 private user user=new user ();  

So a modeldriven is done.

There is a big difference between a property-driven action and a list of the following:

(1) The model-driven action must implement the Modeldriven interface, and to provide the corresponding generics, here is of course the specific use of the JavaBean.

(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.

Is the property driven and the model driven?

(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.

The use of the Modeldriven of Struts2

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.