Naming rules for get/set in action and get/set in JavaBean in struts

Source: Internet
Author: User
Recently, the struts project encountered the following problem: the global variables in my action obviously have the GetSet method and are automatically generated. However, when using this method, it is always said that the GetSet method of this attribute cannot be found and the value sent from JSP cannot be obtained. The reason is as follows: variable name: hdeptment. The GetSet method automatically generated is sethdeptment and gethdeptment. The following explains the problem.

(1) the JavaBean class must be a public class and set its access attribute to public, such as: public class user {......}

(2) the JavaBean class must have an empty constructor: the class must have a public constructor without parameters.

(3) A JavaBean class should not have public instance variables. All class variables are private, for example, private int ID;
(4) attributes should be accessed through a set of access methods (getxxx and setxxx). Generally, IDE (eclipse and JBuilder) generates the getter/setter method for the attributes.

 

 

Generally, the JavaBean attribute starts with a lower-case letter and is named in the hump format. The corresponding getter/setter method is to connect get/set to the upper-case attribute name of the first letter. For example, the property name is username, and the corresponding getter/setter method is GetUserName/setusername.

However, there are some special cases:

1. If the second letter of the attribute name is in uppercase, the name is directly used as the last part of the get/set in the getter/setter method, that is, the case is not changed. For example, the property name is uname and the method is getuname/setuname.

2. If the first two letters are in upper case (generally, both nouns and acronyms are in upper case), the attribute name is directly used as the last part of the get/Set Method in the getter/setter method. For example, the property name is URL and the method is geturl/seturl.

3. If the first letter is in upper case, it is also the last part of the get/set attribute name in the getter/setter method. For example, the property name is "name" and the method is "getname/setname". This is the worst case. An error occurs because the default property name is "name.

Therefore, when naming the global variables and JavaBean of action, you must comply with the preceding naming rules.

 

See the following table:

--------------------------------------------------------

Attribute name/type | getter method | setter Method

------------ | ------------------ | --------------------------
Xcoordinate/double | public double getxcoordinate () | public void setxcoordinate (double newvalue)

------------ | ------------------ | --------------------------
Xcoordinate/double | public double getxcoordinate () | public void setxcoordinate (double newvalue)

------------ | ------------------ | --------------------------
Xcoordinate/double | public double getxcoordinate () | public void setxcoordinate (double newvalue)

------------ | ------------------ | --------------------------
Xcoordinate/double | not allowed

------------ | ------------------ | --------------------------
 

 

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.