Struts automatic type conversion based on OGNL need to be aware of the place

Source: Internet
Author: User

Well, the bumpy process I will not say, directly on the conclusion:

When you use OGNL-based automatic type conversions in Struts2, the object properties in the action must add the Get/set method at the same time.

For example:

Client forms:

<S:formAction= "Registpro">    <S:textfieldname= "User.ID"label= "id"/>    <S:textfieldname= "User.Name"label= "User name"/>    <S:textfieldname= "User.pass"label= "Password"/>    <S:textfieldname= "User.age"label= "Age"/>    <S:textfieldname= "User.birth"label= "Birthday"/>    <S:submitvalue= "Register"/></S:form>

Action Code:

 Public classRegistactionextendsActionsupport {Privateuser User; @Override PublicString Execute ()throwsException {System.out.println ("Age:" +user.getage ()); returnSUCCESS; }     Public voidsetUser (user user) {System.out.println ("In SetUser:" +user.getage ());  This. user =user; }     PublicUser GetUser () { //must add a Get method, otherwise no properties of the User object can be obtained in the Execute method. returnuser; }}

At first I thought that as long as the set method was OK, it was assumed that only the set method was written. I think when struts gets the form parameters assembled into a user object, the assembled user object is set into action using the set method.

But in the end, when I print the value of the user object passed in in the Set method, I find all the properties are the default values, and of course the user's value cannot be obtained in the Execute method.

After this bug I guess (just guessing, not looking at the source code, if any big God knows please tell me) the call to the set method is just an empty object, and when the user property value is called directly in the Execute method, the Get method is called to get the value of the user object.

Depressed for nearly a day, there is no debug source code (because MyEclipse binding Sun.reflect package source code when there is a point problem, so there is no debugging success, fortunately no debugging, otherwise more dizzy!!! )。

I hope my little friends don't make such a mistake again.

Struts automatic type conversion based on OGNL need to be aware of the place

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.