JavaBean the mistake of naming nonstandard

Source: Internet
Author: User
Tags naming convention

"Turn" struts action in the global variables clearly have Getset method, and is automatically generated, but in use, always said to find this property Getset method, not to get the value from the JSP, for the following reasons: variable name: hdeptment, The auto-generated Getset method is: Sethdeptment,gethdeptment. The following is an explanation of the problem.

(1) The JavaBean class must be a public class, and its Access property is set to publicly, such as: Common Class user{...}

(2) The JavaBean class must have an empty constructor: there must be a common constructor with no parameters in the class

(3) A JavaBean class should not have public instance variables, class variables are private, such as: private int id;
(4) Attributes should be accessed through a set of access methods (GETXXX and setxxx), typically the IDE (Eclipse, JBuilder) generates Getter/setter methods for properties





The general JavaBean property starts with a lowercase letter, the hump naming format, and the corresponding Getter/setter method is the name of the property that get/set the first letter of capitalization. For example: The property name is username, and its corresponding Getter/setter method is getusername/setusername.

However, there are some special cases:

1. If the second letter of the property name is capitalized, the property name is used directly as the Get/set in the Getter/setter method, meaning 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 uppercase (the general proper nouns and abbreviations will be capitalized), is also the attribute name is directly used as the Getter/setter method in the latter part of the Get/set. For example, the property name is URL, and the method is Geturl/seturl.

3. If the first letter is capitalized, the attribute name is also used directly as the latter part of the Get/set in the Getter/setter method. For example, the property name name, the method is Getname/setname, this is the worst case, will not find the property error, because the default property name is name.

Therefore, you should be aware of the above naming conventions when you name the global variables and JavaBean of the action.

Encountered an exception:

Javax.servlet.jsp.el.ELException:Unable to find a value for "isActive" in Object of Class "Com.mycompany.domain.User" Usi ng operator "."

Use the "." operator, the value of isactive is not found in the object of the user class.

The error is that in the JSP page I used:

          
1
${user.isactive}

After checking that there is no Getter method to write attribute isactive, well, use IntelliJ idea's auto-generated code function to fill in:

            1            2            3            
public Boolean isActive () {            return isActive;            }

The JavaBean naming convention specifies that for primitive and custom class type property (lowercase), getter and setter methods are GetProperty and SetProperty (the first letter becomes uppercase, Before adding a get or set). For properties of type Boolean, the Getter method can also be written as Isproperty (GetProperty is still available), and, if the first two letters of the property are is (two lowercase), The getter is exactly the same as the property (for example, the getter of the above attribute IsActive is IsActive ()).

If there is a Boolean property called Isisactive, what about its getter? The answer is still isisactive ().

So, whether the property name is IsActive or active,getter can only be isactive () or getactive (), JSP can only be accessed with ${user.active} (note case).

The best thing to do is not to use "is" as the beginning to name a Boolean-type property to avoid confusion.

Split Line ====================================================================================

In short, in JavaBean, to set or get the value of a property, you need the corresponding get and set method, for the Boolean type, you can use the Get/set way to handle The Boolean, you can use is to replace get;

Furthermore, be sure to pay attention to the naming conventions in JavaBean!

JavaBean the mistake of naming nonstandard

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.