Foren

Home » Liferay Portal » English » 3. Development

Kombinierte Ansicht Flache Ansicht Baumansicht
Threads [ Zurück | Nächste ]
toggle
Marc Heimann
jsp:useBean only works partially
30. April 2013 07:57
Antwort

Marc Heimann

Rang: New Member

Nachrichten: 21

Eintrittsdatum: 19. Februar 2013

Neue Beiträge

Hello,

I'm still a beginner and this is what I try to do: I want to pass data to a jsp using a custom java class i.e. a class that is not created with the help of service builder but by myself. The reason is that I have javascript code that dynamically creates a table (varialbe height, width, content). Now I need to be able to load saved data. I've written a small portlet (attached) which produces the same behavior as the original project.

custom class
1
2public class SaveData
3{
4    String dataStr;
5    public void saveData( String d ) { this.dataStr = d; }
6    public String getData( String location ) { return location + ": " + this.dataStr; }
7};


part of view.jsp
1
2<jsp:useBean id="testBean" class="com.portlet.SaveData" scope="page">
3    <%
4        out.println( testBean.getData( "jsp" ));
5    %>
6</jsp:useBean>


part of MVC portlet
 1
 2public void saveFormData( ActionRequest request, ActionResponse response )
 3throws IOException, PortletException
 4{
 5    SaveData sd = new SaveData();
 6    sd.saveData( ParamUtil.getString( request, "someFormData" ) );
 7    System.out.println( sd.getData( "portlet" ) );
 8    request.setAttribute( "testBean", sd );
 9}


The strange thing is that I'm able to call the getData() method in view.jsp, but the saved data is lost showing me "jsp: null". Yet, if I typed in "abc", the System.out.println will correctly bring up "portlet: abc". My guess is that I don't use the id "testBean" correctly and a new instance is created.
Anhänge: testbean-portlet-6.1.10.1.war (20,3k)
Mika Koivisto
RE: jsp:useBean only works partially
30. April 2013 12:19
Antwort

Mika Koivisto

LIFERAY STAFF

Rang: Liferay Legend

Nachrichten: 1498

Eintrittsdatum: 7. August 2006

Neue Beiträge

The problem is you have scope set to page. That means that it's only limited to that jsp. Try using request as scope.
Marc Heimann
RE: jsp:useBean only works partially
2. Mai 2013 23:50
Antwort

Marc Heimann

Rang: New Member

Nachrichten: 21

Eintrittsdatum: 19. Februar 2013

Neue Beiträge

Ok, I've done a little search on this topic because <jsp:useBean> will disappear after submitting when used with scope="request".All three options are listed below:

 1
 2<jsp:useBean id="testBean" class="com.portlet.SaveData" scope="request">
 3    <%
 4        out.println( testBean.getData( "jsp bean" ));
 5    %>
 6</jsp:useBean>
 7<br>
 8${testBean.getData( "jsp EL" )}
 9<br>
10<%
11    com.portlet.SaveData tb = (com.portlet.SaveData) pageContext.getAttribute( "testBean", PageContext.REQUEST_SCOPE );
12    if (tb == null) out.println( "jsp pageContext null" );
13    else out.println( tb.getData( "jsp pageContext" ));
14%>


The second option is using expression language which is completely new to me. You can find a quick intro at http://stackoverflow.com/tags/el/info. However, I didn't want to go any further because all of a sudden i found myself in a struggle between expression language and scriplets where it seems impossible to just get your data into a jsp String.

The third option also came from the above link where I tried to just do the same as expression language. When having a closer look at the MVCPortlet API, you'll see that getRequestDispatcher() is already being used. The only thing you have to do is use pageContext.getAttribute() with scope request or else you'll only get null. Therefore Mika's answer is indeed correct.

However, I won't stick to this solution, if it is giving me any more trouble. On the portlet side I wrote a String parser for the dynamically created data which in case of trouble will be ported to javascript.