The Java Web struts2 framework uses-action parameters for automatic encapsulation and type conversion

Source: Internet
Author: User
Tags java web

The structs can be automatically encapsulated for parameters, which is also simple.

One, the action parameter is automatically encapsulated:

1, you can declare properties of public, accept parameters directly in the action class.

2, the attribute is also private, if it is private, you need to provide a setter method, you can also provide getter method as needed.

3,struts can automatically convert types, typically we pass string-type arguments, and struts can be converted to the default declared variable type

4, when invoking the API, the parameter name passed must be the same as the name declared in the action class.

public class Useraction extends Actionsupport {private static final long Serialversionuid = 1l;public String username;publ IC String pw;public Integer age;public Date birthday;private Double balance;public void setbalance (double balance) {this.b Alance = balance;} Public String Login () {//Get all Parameters System.out.println ("Login with Username:" + username + ", password:" + PW + ", Age:" + AG E + ", Birthday:" + birthday + ", Balance:" + balance); return SUCCESS;}}

  

Second, the type conversion of the parameter

From the above, the default struts2 can be automatically class-Simple conversion (eight basic types), but sometimes we want to convert to complex effects, this time, we need to write the converter.

Two ways: one is to implement the interface, and one is to inherit the class Defaulttypeconverter

Do not want to paste the code, reference:

8018419

The Java Web struts2 framework uses-action parameters for automatic encapsulation and type conversion

Related Article

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.