File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing javabean in JSP which is instantiated in Action class

 
Dilip kumar
Ranch Hand
Posts: 360
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 809
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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" />
 
Dilip kumar
Ranch Hand
Posts: 360
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
Scala Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
change your getter method name to



Edit: *rushes onto the easy ones*
[ August 02, 2006: Message edited by: Samuel Cox ]
 
Dilip kumar
Ranch Hand
Posts: 360
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
Scala Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 360
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic