Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing a Bean from a Java Portlet class to JSP

Status
Not open for further replies.

HornedToad

Programmer
Oct 17, 2006
2
US
Hi,

I am trying to pass a javabean class from the Java portlet file into the jsp file. Basically what is happening is that I want to set various cluster values in the JavaBean when the portlet is initialized in my portlet code and then have the jsp file retrieve those values and display them in a listbox.

It feels like the jsp and the portlet class are pointing at different instances of the ClusterBoxBean class but I don't know enough to fix the problem.

Currently the output of the javabean is either a "null" value in the listbox or an error if the name of my getMethod is getCluster() instead of getNextCluster(). Then instead of returning the value "null" it throws an error in the window. Similarly I tried to create just a dummy variable called value, set it inside the constructor and had a getValue() method try to call it in the jsp that also generated an error.

The final few error lines of the stack trace(from using getCluster() instead of getNextCluster() were as follows:
javax.portlet.PortletException at org.gridlab.gridsphere.portlet.jsrimpl.PortletRequestDispatcherImpl.include(PortletRequestDispatcherImpl.java:77) at org.gridlab.gridsphere.provider.portlet.jsr.ActionPortlet.doViewJSP(ActionPortlet.java:305) at org.gridlab.gridsphere.provider.portlet.jsr.ActionPortlet.doMode(ActionPortlet.java:362) at
etc.

Here's a little more code segment from what I am doing if it helps.

//Portlet Class:
Code:
public class Nws extends ActionPortlet {
      
    private String VIEW_PAGE = "nws.jsp";    
   
public void init(PortletConfig config) throws PortletException {    
        super.init(config);
        setupClusters();
        DEFAULT_VIEW_PAGE = "prepare";
    }
 
public void prepare(RenderFormEvent event) throws PortletException {    
        showForm(event.getRenderRequest());
        setNextState(event.getRenderRequest(), VIEW_PAGE);
    }
 
   private void setupClusters() {      
        ClusterBoxBean clusterBean = ClusterBoxBean.getInstance();
        clusterBean.addNextCluster("testCluster");
}
//ClusterBean Class:
Code:
public class ClusterBoxBean {
    
  private String [] cluster;
  private static int MAX_CLUSTERS = 25;
  private int currentClusterSize; 
  int value;
  private static ClusterBoxBean ourInstance = new ClusterBoxBean();
  public ClusterBoxBean() {
        super();
        this.cluster = new String[MAX_CLUSTERS];
        this.currentClusterSize = 0;
        this.value = 0;
  }
 
  public static ClusterBoxBean getInstance() {
    return ourInstance;   
  }
  
  public int getCurrentClusterSize() {
     return currentClusterSize;
  }
  //test purposes only
  public int getValue()
  {
    return value;   
  }
  
  public String getNextCluster(int i) {
    return cluster[i];   
  }
    
  public void addNextCluster(String clusterName) {
     //clusters[currentClusterSize] = clusterName;
     cluster[0] = clusterName;
     currentClusterSize++;
  } 
}
//nws.jsp
Code:
<%@ page import="org.gridlab.gridsphere.provider.portletui.beans.MessageStyle"%>
<%@ taglib uri="/portletUI" prefix="ui" %>
<%@ taglib uri="[URL unfurl="true"]http://java.sun.com/portlet"[/URL] prefix="portlet" %>
<jsp:useBean id="clusterBean" class="org.gridsphere.nws.portlets.ClusterBoxBean" scope="session"/>
 
<ui:form>
    <ui:table cellpadding="4">
        <ui:tablerow>     
        <ui:tablecell> <ui:text key="Cluster:"/> </ui:tablecell>
        <ui:tablecell></ui:tablecell>     
        <ui:tablecell>
            <ui:listbox beanId="clusterBox">
            <ui:listboxitem value ="<%=clusterBean.getNextCluster(0)%>" selected = "true"/>
            </ui:listbox>
       </ui:tablecell>
     </ui:tablerow>


Thanks alot for any help you can offer.

HornedToad
 
retract the question. Figured it out elsewhere.

Had an incorrect path in the jsp:useBean and you need to store the bean as a request/session attribute before calling the jsp. i.e. request.setAttribute("clusterBean", clusterBean);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top