• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Struts2 test app - Problem displaying bean property to JSP.

 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<s:property value="name"/> comes back blank as well.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No; using the "property-style" OGNL is the standard usage--that's what it's for :)
 
Uh oh, we're definitely being carded. Here, show him this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic