How to obtain the Action data in Struts2

Source: Internet
Author: User
Tags ocaml

How to obtain the Action data in Struts2
Introduction

When we use struts2, we find that if we implement an Action corresponding to the user form, we can directly use the request parameter in the execute () method, who helped us complete the data stored in the request field and the corresponding data in the Action? And how do they correspond?

For the first question, we may already know that this good guy is the Interceptor. The parameters stored in our request domain have been mapped by the interceptor, next let's take a look at how ing works.

Basic data matching methods

Struts2 provides two ways to correspond page data and actions: FieldDriven and ModelDriven );

There are two types of property drivers: one is the attribute correspondence of the basic data type, and the other is the attribute correspondence of the JavaBean style.

(1) property-driven (attribute corresponding to the basic data type)
That is, we need to create a Java class corresponding to the request domain and have get and set methods.
For example, the logon form is as follows:

 

Then we need to write an Action that corresponds to this form:

Public class UserAction extends ActionSupport {private String submitFlag; private String account; private String password; // Save the get and set Methods}

It can be seen that the data in the form is suitable for the corresponding action. In this case, struts2 will automatically retrieve the data from the request object, and then match the data by name, and automatically set it to the Action attribute.

(2) Property-driven (directly using domain objects)
We can think that when there is a lot of data in our forms, there will be a lot of data in our actions, which is not easy to maintain. This is the property-driven advantage, we can encapsulate these attributes into a bean and then use this bean in action. The details are as follows:
The form is the same as the previous one:
The parameters in bean correspond to those in the form:

Public class UserModel {private String submitFlag; private String account; private String password; // Save the get and set Methods}

Then we can use it in action as follows:

Public class UserAction extends ActionSupport {private UserModel userModelTest = new UserModel (); // The User model is used and the public UserModel getUserModel () {return userModelTest;} corresponding to the form ;} public void setUserModel (UserModel userModelTest) {this. userModelTest = userModelTest ;}}

In this case, our form needs to be changed as follows:

 

(3) model-driven
This method enables Action to implement the ModelDriven interface. This interface implements a getModel method, and this method returns the data model object used by Action.
For example, the parameters in the form are the same as those in the front.

public class UserAction extends ActionSupport implements ModelDriven{    private UserModel userModelTest = new UserModel();    public UserModel getUserModel() {        return userModelTest;    }    public void setUserModel(UserModel userModelTest) {        this.userModelTest = userModelTest;    }    public Object getModel() {        return userModelTest;    }}

In this case, the form does not need the vertices in (2), and changes accordingly, as shown below:

 

So why don't we click it here (more specifically, why don't we need a prefix)? This is because in ModelDriven mode, an Action can only correspond to one Model, therefore, you do not need to add a prefix.

The advantages and disadvantages of the above three methods can be seen directly. The other three methods can be used together.

Input a non-String value

We know that in actual situations, not all input types are of the String type. Fortunately, Struts2 can help users automatically convert the basic types.
It is not difficult to understand here.

How to handle incoming values

For example, when we register, we need to fill in the user's hobbies. One checkbox has multiple values. What should we do?
There are two methods: Define an array of the String type and provide the get and set methods. In addition, define a private set type attribute and provide the corresponding get and set methods.

In addition, if the number of passed values is not sure, we can only use a set for processing.

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.