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
------------ | ------------------ | --------------------------