Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

struts 2 -- name matching when we retrieve attributes from Action class

 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If an action class has

************
private String name;

public String getMyName() {

return name;
}

// note: there is NOT a "getName()" method in this action class.

..
****************

If this action forwards to a JSP page, then in the JSP page, shall I use ${myName} or ${name} to retrieve the variable "name" defined in the action class ? Again, suppose there is not a "getName()" method in the class. Does struts 2 go by the attribute name or getter method name ?

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems like it would have been quicker to try it.

Since the action property is private you access it via "myName".
 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:Seems like it would have been quicker to try it.

Since the action property is private you access it via "myName".


That's what I found, "myName" should work. But, since there is no such an attribute called "myName" in this class, is it struts 2 snart enough to parse each getter method to find out ? e.g. it "parses" the "getMyName" method name and takes away "get" and keeps "MyName" string and then change the first letter to lower case ? please let me know how things work behind the scene if you can.

Thanks.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...

OGNL follows JavaBean conventions. If there's a getter named getMyName then "myName" will call the getter. An OGNL statement and a log statement would have shown this.
 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks David
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic