cancel
Showing results for 
Search instead for 
Did you mean: 

getting data into a bean / sharing a bean with JSPs

Former Member
0 Kudos
321

Hi there,

I am using SAP NetWeaver Developer Studio 2.0.9 to develop a special iView (I do not have a PDK on my portal). This iView should present data in a tabular form, so I used HTMLB in a JSP to define a TableView.

Then I made a bean (simple Java class) that has a TableViewModel as a private member with according getter and setter methods

The Developer Studio wizard generated a JSPDynPage with a doProcessBeforeOutput() method that looked like this:

public void doProcessBeforeOutput() throws PageException {
  //Form myForm = this.getForm(); // get the form from DynPage
  RackDataBean myTableViewBean = new RackDataBean();
  ((IPortalComponentRequest)getRequest()).getServletRequest().
    setAttribute("myTableViewBean", myTableViewBean);
  // fill bean with data here
  this.setJspName("RackDataJSP.jsp");

But how? My data does not come from an R/3 system or anything. It needs to be pushed in from the outside. The data does not have to be persistent, it is just for a little demo. So my idea was to probably have a JSP (maybe another one) taking it's request parameters and putting it into the bean. With a forced refresh of the iView the data should show up.

As a test, I tried to append this to the doProcessBeforeOutput() method:

  String event = ((IPortalComponentRequest)getRequest()).
    getServletRequest().getParameter("event");
  String date = ((IPortalComponentRequest)getRequest()).
    getServletRequest().getParameter("date");
  String time = ((IPortalComponentRequest)getRequest()).
    getServletRequest().getParameter("time");
  String sever = ((IPortalComponentRequest)getRequest()).
    getServletRequest().getParameter("severity");
  String[][] str = {{event, date, time, severity}};
  myTableViewBean.setModel(
    new DefaultTableViewModel(str, RackDataBean.colnames));

But it does not work. The iView works fine and I see my table view with the right columns, but no data in it, so the JSP at least seems to be correct. What would be the right way to do this?

Thanks,

Ingmar

Message was edited by: Ingmar Frank

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Ingmar,

Looks you are not getting data from servletrequest. I think you have to get the data in following way because portal component is passing the values back to a portal component. Not the servlet request.

IPortalComponentRequest request =(IPortalComponentRequest) this.getRequest();
String event = reqiest.getParameter("event");

Former Member
0 Kudos

Thanks for the hint! Look somewhat logical to me.

Unfortunately, it also does not work. Where do you suppose would that code have to be inserted? To me it looks like the JSP is constructed once, and right then the bean is also constructed. Afterwards, I can not change the bean. Because if the code would run as expected, I would at least see "null" in every column, right?

Another reason might be that I do not yet refresh the iView. I have not figured out how to do this. Maybe by brute force, writing a Javascript snippet into the response object?

Former Member
0 Kudos

Ingmar,

I still don't understand how you have coded your portal component. Can you post your code and i will tell where you are going wrong. Post your JSP as well. thank you.

Former Member
0 Kudos

Thank you so much, I am really stuck. This will be a lot of code, but it is not too difficult, I hope.

Here is the JSP:

<jsp:useBean id="myTableViewBean" scope="session" class="com.sap.hpdemo.RackDataBean" />
<hbj:content id="myContext" >
  <hbj:page title="RackDataPageTitle">
   <hbj:form id="myFormId" >
    <hbj:tableView
		id="myTableView1"
		model="myTableViewBean.model"
		design="ALTERNATING"
		headerVisible="true"
		footerVisible="true"
		fillUpEmptyRows="true"
		navigationMode="BYLINE"
		selectionMode="MULTISELECT"
		headerText="Rack Events View"
		onNavigate="myOnNavigate"
		visibleFirstRow="1"
		visibleRowCount="5"
		width="100%">
    </hbj:tableView>
   </hbj:form>
  </hbj:page>
</hbj:content>

The bean class is this one:

public class RackDataBean implements Serializable
{
    private TableViewModel model;

    static private String[][] strInit =
        { { "door opened", "04/11/05", "10:23 pm", "high" }
    };
    static private String[][] strEmpty = { { "", "", "", "" }
    };
    static public String[] colnames = { "Event", "Date", "Time", "Severity" };

    /**
     * Constructor.
     */
    public RackDataBean()
    {
        //model = new DefaultTableViewModel();
        model = new DefaultTableViewModel(/*strEmpty,*/
        colnames);
    }

    /**
     * @return
     */
    public TableViewModel getModel()
    {
        return model;
    }

    /**
     * @param model
     */
    public void setModel(DefaultTableViewModel model)
    {
        this.model = model;
    }
}

And finally, the PageProcessorComponent:

public class RackDataPage extends PageProcessorComponent
{
    public DynPage getPage()
    {
        return new RackDataPageDynPage();
    }

    public static class RackDataPageDynPage extends JSPDynPage
    {
        /**
         * Initialization code executed once per user.
         */
        public void doInitialization()
        {
        }

        /**
         * Input handling code. In general called the first time with the second page request from the user.
         */
        public void doProcessAfterInput() throws PageException
        {
        }

        /**
         * Create output. Called once per request.
         */
        public void doProcessBeforeOutput() throws PageException
        {
            //Form myForm = this.getForm(); // get the form from DynPage
            RackDataBean myTableViewBean = new RackDataBean();
            ((IPortalComponentRequest) getRequest())
                .getServletRequest()
                .setAttribute(
                "myTableViewBean",
                myTableViewBean);

            // fill bean with data here
            // BUT HOW?
            IPortalComponentRequest request =
                (IPortalComponentRequest) this.getRequest();
            String event = request.getParameter("event");
            String date = request.getParameter("date");
            String time = request.getParameter("time");
            String sever = request.getParameter("sever");
            String[][] str = { { event, date, time, sever }
            };
            myTableViewBean.setModel(
                new DefaultTableViewModel(str, RackDataBean.colnames));

            this.setJspName("RackDataJSP.jsp");
        }
    }
}

My problem is: how to get data into the bean, then refresh the iView and keep this going on in a loop? As I said, the data will most likely be pushed in from the outside. I am not sure how to do this, I hope an additional JSP will do. It should also access the bean. I hope that I understood this concept right.

Thanks, Ingmar

Former Member
0 Kudos

i am about to catch a fligh. I will respond to you when i get back home.

Former Member
0 Kudos

Change your code to following.Run the following code via url.

example:

http://localhost:50000/irj/servlet/prt/portal/prtroot/com.sap.hpdemo2.RackDataPage?event=test&date=1...

JSP Page:

<%@ taglib uri= "tagLib" prefix="hbj" %>
<jsp:useBean id="myTableViewBean" scope="application"  class="com.sap.hpdemo.RackDataBean" />
<hbj:content id="myContext" >
  <hbj:page title="RackDataPageTitle">
   <hbj:form id="myFormId" >
    <hbj:tableView
		id="myTableView1"
		model="myTableViewBean.model"
		design="ALTERNATING"
		headerVisible="true"
		footerVisible="true"
		fillUpEmptyRows="true"
		navigationMode="BYLINE"
		selectionMode="MULTISELECT"
		headerText="Rack Events View"
		onNavigate="myOnNavigate"
		visibleFirstRow="1"
		visibleRowCount="5"
		width="100%">
    </hbj:tableView>
   </hbj:form>
  </hbj:page>
</hbj:content>

<b>Bean:</b>

package com.sap.hpdemo;
import java.io.Serializable;
import com.sapportals.htmlb.table.DefaultTableViewModel;
import com.sapportals.htmlb.table.TableViewModel;

public class RackDataBean implements Serializable {
	
	   private TableViewModel model;
 
	
		static public String[] colnames = { "Event", "Date", "Time", "Severity" };
 
		/**
		 * Constructor.
		 */
		public RackDataBean(String[][] str )
		{
		
			model = new DefaultTableViewModel(str,colnames);
		}
		/**
		 * @return
		 */
		public TableViewModel getModel()
		{
			return model;
		}
 
		/**
		 * @param model
		 */
		public void setModel( DefaultTableViewModel model)
		{
			this.model = model;
		}

	

}

<b>JSP DynPage:</b>

package com.sap.hpdemo;

import com.sapportals.htmlb.page.DynPage;
import com.sapportals.htmlb.page.PageException;
import com.sapportals.htmlb.table.DefaultTableViewModel;
import com.sapportals.portal.htmlb.page.JSPDynPage;
import com.sapportals.portal.htmlb.page.PageProcessorComponent;
import com.sapportals.portal.prt.component.IPortalComponentContext;
import com.sapportals.portal.prt.component.IPortalComponentRequest;
import com.sapportals.portal.prt.component.IPortalComponentResponse;

public class RackDataPage extends PageProcessorComponent {

	public DynPage getPage() {
		return new RackDataPageDynPage();
	}

	public static class RackDataPageDynPage extends JSPDynPage {

		RackDataBean myBean;

		public void doInitialization() {
			
			// fill your bean with data here...
			IPortalComponentRequest request = (IPortalComponentRequest) this.getRequest();
			IPortalComponentResponse response = (IPortalComponentResponse) this.getResponse();
			IPortalComponentContext myContext = request.getComponentContext();
			
			String event = request.getParameter("event");
			String date = request.getParameter("date");
			String time = request.getParameter("time");
			String sever = request.getParameter("sever");
			String[][] str = { { event, date, time, sever }};
			
			Object o = myContext.getValue("myTableViewBean");
			if (o == null || !(o instanceof RackDataBean)) {
				  response.write("new bean");
					myBean = new RackDataBean(str);			
			} else {
				myBean = (RackDataBean) o;
				response.write("i found the bean");
				myBean.setModel(new DefaultTableViewModel(str,RackDataBean.colnames));
			}
			myContext.putValue("myTableViewBean",myBean);

		}

		public void doProcessAfterInput() throws PageException {
		}

		public void doProcessBeforeOutput() throws PageException {
			this.setJspName("RackDataJSP.jsp");
		}
	}
}

I still don't understand the question from you about running this in loop. You can write another component that calls this component in a loop and passes the data via url. It's not possible by just adding another JSP in the same component.

Former Member
0 Kudos

Thanks! One thing that I overlooked for sure was the scope.

I am sorry to say that it still does not work. I should see "new bean" or "i found the bean" on the page, right? But it does not show up. I'm not sure why this is so.

Anyway, it looks like the right direction. If you have any other idea, please let me know.

Former Member
0 Kudos

I've tried the following: made a new portal component (extends AbstractPortalComponent), took the code that you've written into doInitialization() and put it into my new doContent() method. Then it works! But why?

So now I still need to figure out whether I can access the bean from another component at the same time, and therefore adding more data to the table.

Former Member
0 Kudos

I still don't know why it didn't work for you in JSPDynpage. Ingmar, you can try export the jspdynpage project into a different name and then try executing it. Two components can access the same bean. Put the bean in IPortalComponentSession.

request.getServletRequest().getSession();

Former Member
0 Kudos

Maybe my Portal is just too old, I believe it is an installation from two years ago. Could that be a problem?

Okay, I tried to access the bean from two components, but I did not know that I had to put it into the session.

Thank you very much, you are very helpful! I keep awarding you

Former Member
0 Kudos

Hm, somehow this does not seem right. I have tried different variants now: IPortalComponentSession, HttpServletRequest (via IPortalComponentRequest.getServletRequest) and HttpSession. None of it works as expected.

My current structure is: I have two AbstractPortalComponents. One fills the bean by getting the parameter values from the request, and the other one tries to just set the value (putValue, setAttribute, whatever appropriate) to the same bean. But the "view component" does not find the bean. Did I misunderstand something? I read this document:

https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.km.cm.docs/library/ep/usage of beans with portal and htmlb.pdf

Any little hint is greatly appreciated!

Former Member
0 Kudos

Hi,

Not sure if it helps but have you checked your portalapp.xml file is correctly edited? I have a simple application similar to yours and my portalapp.xml file looks something like this -

<?xml version="1.0" encoding="utf-8"?>
<application>
  <application-config>
    <property name="PrivateSharingReference" value="com.sap.portal.htmlb"/>
  </application-config>
  <components>
    <component name="TestReport">
      <component-config>
        <property name="ClassName" value="com.atosorigin.component.TestReportComponent"/>
        <property name="SecurityZone" value="com.atosorigin.component/low_safety"/>
      </component-config>
      <component-profile/>
    </component>
    <component name="PopUpComponent">
      <component-config>
        <property name="ClassName" value="com.atosorigin.component.PopUpComponent"/>
        <property name="SecurityZone" value="com.atosorigin.component/low_safety"/>
      </component-config>
      <component-profile>
        <property name="tagLib" value="/SERVICE/htmlb/taglib/htmlb.tld"/>
      </component-profile>
    </component>
  </components>
  <services/>
</application>

Hope this helps!

Answers (0)