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 Accessing javabean in JSP which is instantiated in Action class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Accessing javabean in JSP which is instantiated in Action class" Watch "Accessing javabean in JSP which is instantiated in Action class" New topic
Author

Accessing javabean in JSP which is instantiated in Action class

Dilip kumar
Ranch Hand

Joined: Oct 16, 2000
Posts: 360
Hello All -


MVC architecture suggests having business logic/queries in javabeans and JSPs should access javabeans methods/variables to display results.

But I still don't understand what's the best place to instance the bean. Action class or JSP itself ?

I'm familiar with how to instantiate javabean from the JSP

(jsp:useBean id="db" scope="request" class="com.test.common.ReportBean" /)

But if I instantiate it from Action class how do I access the same instance from JSP ?

Really appreciate any guidance.

Thanks
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Best place is instantiate model in action class. jsps are for presenting the contents. It has only presentation logic.

create an instance of your bean in action and set it in request scope. Finally in jsp, retrieve the object which is bound in request.


Naseem


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Here's an example of what Naseem is talking about:

in your action class:

MyBean myBean = new MyBean();
myBean.setFoo("xyz");
myBean.setBar("abc");
request.setAttribute("myBean", myBean);

in your JSP:

<bean:write name="myBean" property="foo" />
<bean:write name="myBean" property="bar" />


Merrill
Consultant, Sima Solutions
Dilip kumar
Ranch Hand

Joined: Oct 16, 2000
Posts: 360
Thanks much for your response.

Due to some reason(s) I'm not getting the java bean value in my JSP. If I understood correctly property="reportName"
will call getreportName() method the bean. But it doesn't look like it is going into that method.


Action Class



Java Bean


JSP
R Goel
Greenhorn

Joined: Aug 02, 2006
Posts: 1
GetReportName - "R" should be in caps as per naming standard in the method only. The variable name remains as reportName.
2. This often happens because the variable name in the bean and the name defined in jsp do not match. Check for spelling mistakes in naming variables or other such errors which may lead to a mismatch.
Samuel Cox
Ranch Hand

Joined: Nov 16, 2004
Posts: 96

change your getter method name to



Edit: *rushes onto the easy ones*
[ August 02, 2006: Message edited by: Samuel Cox ]
Dilip kumar
Ranch Hand

Joined: Oct 16, 2000
Posts: 360
Thank you guys.

I have added 3 methods including getReportName() in the bean. But I don't see JSP reaching any of these methods. It is possible that there could be some scope issue ?
Samuel Cox
Ranch Hand

Joined: Nov 16, 2004
Posts: 96

Are you sure you are looking at where those println statements are actually writing?

Can you change set reportName to "myReportName" and see if it displays on the page?

As an aside, you might want to look at incorporating logging (log4j or something similar).
Dilip kumar
Ranch Hand

Joined: Oct 16, 2000
Posts: 360
Silly mistake.

I was missing
(%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %)
in my JSP.

It is working now.

Thank you all for your time.
 
Don't get me started about those stupid light bulbs.
 
subject: Accessing javabean in JSP which is instantiated in Action class
 
Similar Threads
how to get ServletContext from ActionMapping
using bean:define with collection
how many instances of java bean in jsp?
!!My JSP Doubts!!
question on el and standard action