Struts2 System Training of Action 6 three methods of receiving parameters of action

Source: Internet
Author: User

Struts2 System Training of Action 6 three methods of receiving parameters of action

We know that action plays a controller role in web development. By receiving parameters from the client, different modules are executed to implement the operation. Therefore, receiving parameters is an important part, only front-end parameters can be received before database operations and other background business logic can be executed. Then, let's start to display these three methods one by one (just look at the compilation of the action class)

1. Method 1: UserAction. java

package com.bjsxt.struts2.user.action;import com.opensymphony.xwork2.ActionSupport;public class UserAction extends ActionSupport {private String name;private int age;public String add() {System.out.println("name=" + name);System.out.println("age=" + age);return SUCCESS;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}
In UserAction. java defines the attributes and corresponding methods of actions to receive front-end parameters. This is the simplest and most intuitive, but the premise is that you know what the parameters passed by the front-end are, lack of flexibility, so there is a second method:

1UserAction. the java code is

package com.bjsxt.struts2.user.action;import com.bjsxt.struts2.user.model.User;import com.opensymphony.xwork2.ActionSupport;public class UserAction extends ActionSupport {private User user;//private UserDTO userDTO;public String add() {System.out.println("name=" + user.getName());System.out.println("age=" + user.getAge());return SUCCESS;}public User getUser() {return user;}public void setUser(User user) {this.user = user;}}
UserDTO. java code:

package com.bjsxt.struts2.user.dto;public class UserDTO {private String name;private String password;private String confirmingPassword;}

User. java code:

package com.bjsxt.struts2.user.model;public class User {private String name;private int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}
This method writes the steps for receiving parameters separately, defines attributes in UserDTO, and defines attributes in User. Two classes are introduced through import in action to receive data. This method was first introduced in my work. I felt that this method was particularly troublesome. Why should I write it separately? Later I understood it slowly, which not only reduced the coupling of the program, well planned program expansion and other operations. However, some people prefer the third method. Due to limited space, I will analyze it in detail in the next article and look forward to it...






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.