aspose file tools*
The moose likes Struts and the fly likes struts 2: Stuck up with simple issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "struts 2: Stuck up with simple issue" Watch "struts 2: Stuck up with simple issue" New topic
Author

struts 2: Stuck up with simple issue

Nandish Sri
Ranch Hand

Joined: Jan 19, 2004
Posts: 45
I am trying with sample application to display values from object set in Action and trying to populate the same object values in jsp. But it is not populating in JSP file
Please help me, how to populate object values in jsp....

Here is my struts application code,


userList.java is normal java bean with getter and setter methods




JSP Page to access userList object values,



Nandish
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

There are two problems with your code. One of them is logical and very small, since you know that you'll get only one row from your SQL statement, so you should use if condition instead of while loop i.e. if(result.next()) instead of while(result.next()). Now the id/var attribute of the s:bean tag is used to give the name of a bean on the value stack, but userList is a local variable not a field in your action class. If you move the declaration of userList outside of the execute method, your code should work...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Nandish Sri
Ranch Hand

Joined: Jan 19, 2004
Posts: 45
Thanks for your quick response. I moved creation of userList object outside the execute method, but still nothing is displayed in the customerList.jsp.
Is there any other way, other than s:bean usage to access object in jsp like need to configure form-bean in struts.xml[I dont think it is required in struts 2.0]
I am not able to understand how to access object members from jsp page. Please let me know the solution.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

Is there any other way, other than s:bean usage to access object in jsp

Yes, you can do it without using the s:bean tag like this

I actually didn't know the purpose of s:bean tag before this, it actually instantiates a new object and pushes it into the value stack, so your code won't work when you use s:bean tag even if you moved userList out of the execute method (as you already did)...
Nandish Sri
Ranch Hand

Joined: Jan 19, 2004
Posts: 45
I tried with these below code options, but still not getting userList object members.



sarada bokka
Ranch Hand

Joined: Jan 13, 2010
Posts: 80
Did your Action class has setter & getter for UserList class?
Nandish Sri
Ranch Hand

Joined: Jan 19, 2004
Posts: 45
Here is my userList.java file



And here is my Action class


And I tried with even by extends ActionSupport also. Not getting any value in jsp.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

sarada is spot on, you need to add setUserList and getUserList in your action class (at least getUserList is required)...
Nandish Sri
Ranch Hand

Joined: Jan 19, 2004
Posts: 45
He he... Thanks for your responses. Now I added getUserList in my Action class. Able to retrieve object members in jsp file.
Thanks a lot
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: struts 2: Stuck up with simple issue
 
Similar Threads
struts2: howto correctly deligate actions on the struts.xml's include files
Error while forwarding from struts.xml
Unable to display List values in jsp - Tried all possiblities without success
Struts 2 error - Unable to load configuration
working with hashmap using struts2