留言板

jsp:useBean only works partially

Marc Heimann,修改在10 年前。

jsp:useBean only works partially

New Member 帖子: 21 加入日期: 13-2-20 最近的帖子
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

public class SaveData
{
	String dataStr;
	public void saveData( String d ) { this.dataStr = d; }
	public String getData( String location ) { return location + ": " + this.dataStr; }
};


part of view.jsp

<jsp:usebean id="testBean" class="com.portlet.SaveData" scope="page">
	&lt;%
		out.println( testBean.getData( "jsp" ));
	%&gt;
</jsp:usebean>


part of MVC portlet

public void saveFormData( ActionRequest request, ActionResponse response )
throws IOException, PortletException
{
	SaveData sd = new SaveData();
	sd.saveData( ParamUtil.getString( request, "someFormData" ) );
	System.out.println( sd.getData( "portlet" ) );
	request.setAttribute( "testBean", sd );
}


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.
thumbnail
Mika Koivisto,修改在10 年前。

RE: jsp:useBean only works partially (答复)

Liferay Legend 帖子: 1519 加入日期: 06-8-7 最近的帖子
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,修改在10 年前。

RE: jsp:useBean only works partially

New Member 帖子: 21 加入日期: 13-2-20 最近的帖子
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:


<jsp:usebean id="testBean" class="com.portlet.SaveData" scope="request">
	&lt;%
		out.println( testBean.getData( "jsp bean" ));
	%&gt;
</jsp:usebean>
<br>
${testBean.getData( "jsp EL" )}
<br>
&lt;%
	com.portlet.SaveData tb = (com.portlet.SaveData) pageContext.getAttribute( "testBean", PageContext.REQUEST_SCOPE );
	if (tb == null) out.println( "jsp pageContext null" );
	else out.println( tb.getData( "jsp pageContext" ));
%&gt;


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.