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