• 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

Accessing javabean in JSP which is instantiated in Action class

 
Ranch Hand
Posts: 360
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 96
Scala VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic