Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Struts2 test app - Problem displaying bean property to JSP.

 
Kevin P Smith
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wondering if anyone could help with this 'simple' Struts2 test I'm trying to get to work.
Quite simply trying to click on the action in index.jsp, which runs the ExampleAction, gets the Employee bean and populates 'name' with "Homer Simpson", then opens the example3.jsp and displays the property.

Simple!

No, the value is there in the bean, but the page comes back blank and the 'name' property set back to Null.

I'm trying to do this with Annotations using Struts 2.1.6, but the examples and tutorials on the Internet are to say 'dodgy', many errors and missing bits. I seems to have to try to mash them together to fill in teh blanks.

I must be missing something stupid, but I am getting knowhere with this.
Is it simply that the Bean (Employee) is not stored in my session, because my understanding was that Struts would atomatically do this (again, based on a tutorial).

Thanks in advance.

Index.jsp


example3.jsp


ExampleAction.class


Employee.class


web.xml


struts.xml
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As stated in the <s:bean> documentation, the tag *instantiates* an instance of the named class.

To display a property of the *action* just use <s:property value="nameOfProperty"/>. Or if you prefer just use JSP EL notation: ${nameOfProperty}. I haven't seen much documentation that makes this dodgy or complicated.
 
Kevin P Smith
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<s:property value="name"/> comes back blank as well.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The action you posted doesn't have a "name" property; it has an "employee" property that has a "name" property.

(Actually, it doesn't have an "employee" property, either, unless you left out its public modifier (non-JavaBean, but S2.1/OGNL accessible) or its getter (recommended JavaBean-centric approach).
 
Kevin P Smith
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it was meant to be <s:property value="employee.name"/>

But it was not having
public Employee employee = new Employee();
that was the problem!

So just for record, is 'employee.name' considered bad practice over 'employee.getName()'?

Cheers for the help

KS
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No; using the "property-style" OGNL is the standard usage--that's what it's for :)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic