STRUCTS2 JSP page parameters encapsulated into class passed to action

Source: Internet
Author: User

Halfway through the maintenance of a system, and I am a rookie.

System to implement from the front-end JSP page input query criteria, incoming background action processing. According to the original code of the system itself probably understand is the JSP OGNL expression passed parameters, but has not been successful. Baidu all kinds of ways, find the following methods have tried, as if all can not. Looking for the ear to scratch the cheek to find, I ognl expression in the value of the case and action in the class instance name inconsistent, really can not be more two-. -

After aligning the OGNL expression in the JSP with the class instance name in the action, the problem is resolved. I found it. Structs2 JSP parameters to the background action of the method record down, copy once to strengthen memory

Copied from someone else:

There are 3 ways to accept the action in the Struts2 table only son pass the arguments:

1> in the Action defining Form properties in a class

For example, log in form login.jsp:

<formAction= "Login"Method= "POST"name= "form">User:<S:textfieldname= "username"/><BR/>Password:<S:passwordname= "Password"/><BR/><S:submitvalue= "Submit"/></form>

Define two properties in action

Private String username; Private String password;

Provides a Setter,getter method. You can receive the parameters that the table only son over. This method of receiving parameters is convenient and simple, but the structure is not very good, and when the form is passed a lot of parameters, the whole action class is filled with Setter,getter method, the program is not very beautiful.

2. encapsulate the parameters passed by the table only son into a class and invoke the properties.

For example, log in form login.jsp:

<formAction= "Login"Method= "POST"name= "form">User:<S:textfieldname= "User.username"/><BR/>Password:<S:passwordname= "User.password"/><BR/><S:submitvalue= "Submit"/></form>

Defining the Model class

 Public class User {     private  String username;      Private String password; provides the Setter,getter method. }

Defining the model's object in the action class

private user user;

Provides a Setter,getter method.

There are many legends in this way.

3. by implementing the modeldriven interface

Receive form data first the action class must implement the Modeldriven interface, and the same data from the form is encapsulated.

For example, log in form login.jsp:

<formAction= "Login"Method= "POST"name= "form">User:<S:textfieldname= "username"/><BR/>Password:<S:passwordname= "Password"/><BR/><S:submitvalue= "Submit"/></form>

Defining the Model class

 Public class User {     private  String username;      Private String password; provides the Setter,getter method. }

Action class

 Public class extends Implements Modeldriven<user> {       new///  This way must be your own new object        .....  Public Users Getmodel () {return  user;}}

There is no need for getter and setter methods in this way.

STRUCTS2 JSP page parameters encapsulated into class passed to action

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.