text: 
  Accessing javabean in JSP which is instantiated in Action class
  Dilip kumar
  Ranch Hand
  Posts: 360
  posted 14 years ago
  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
  posted 14 years ago
  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
  Posts: 4864
  posted 14 years ago
  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
  <bean:write
  Merrill
  Consultant,
  Sima Solutions
  Dilip kumar
  Ranch Hand
  Posts: 360
  posted 14 years ago
  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
  ReportBean bean = new ReportBean("Test Report"); request.setAttribute("myBean", bean);
  Java Bean
  public class ReportBean { private String reportName = ""; public ReportBean (String value) { this.reportName = value; } public String getreportName() { System.out.println("In getreportName method"); return this.reportName; } }
  JSP
  <bean:write
  R Goel
  Greenhorn
  Posts: 1
  posted 14 years ago
  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
  I like...
  posted 14 years ago
  change your getter method name to
  getReportName()
  Edit: *rushes onto the easy ones*
  [ August 02, 2006: Message edited by: Samuel Cox ]
  Dilip kumar
  Ranch Hand
  Posts: 360
  posted 14 years ago
  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 ?
  public String getreportName() { System.out.println("In getreportName method"); return this.reportName; } public String getReportName() { System.out.println("In getReportName method"); return this.reportName; } public String GetReportName() { System.out.println("In GetReportName method"); return this.reportName; }
  Samuel Cox
  Ranch Hand
  Posts: 96
  I like...
  posted 14 years ago
  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
  posted 14 years ago
  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.
  World domination requires a hollowed out volcano with good submarine access. Tiny ads are optional.
  Thread Boost feature
  reply
  Bookmark Topic
  Watch Topic
  New Topic
  Boost this thread!
  Similar Threads
  question on el and standard action
  !!My JSP Doubts!!
  how to get ServletContext from ActionMapping
  using bean:define with collection
  how many instances of java bean in jsp?
  More...