File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Struts2 test app - Problem displaying bean property to JSP. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts2 test app - Problem displaying bean property to JSP." Watch "Struts2 test app - Problem displaying bean property to JSP." New topic
Author

Struts2 test app - Problem displaying bean property to JSP.

Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Feb 18, 2005
Posts: 362
<s:property value="name"/> comes back blank as well.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Feb 18, 2005
Posts: 362
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

Joined: Sep 29, 2008
Posts: 12617

No; using the "property-style" OGNL is the standard usage--that's what it's for :)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struts2 test app - Problem displaying bean property to JSP.
 
Similar Threads
Need Help - Struts2 s:select issue - Urgent
HTTP Status 404 - /Struts2Application1/clientAction + Please help
The Struts dispatcher cannot be found Error
WARNING: Could not find action or result
Struts-tags.tld