Foros de discusión

how to pass an object from jsp to controller in portlet

juan meza, modificado hace 9 años.

how to pass an object from jsp to controller in portlet

Regular Member Mensajes: 228 Fecha de incorporación: 6/01/14 Mensajes recientes
Hi, i have liferay 6.2 GA2

i want to pass an object from the jsp to the controller, in a portlet...

i've tried with:
request.setAttribute("obj", obj);

and then in the controller:
Obj obj = (Obj)request.getAttribute("obj");


but it doesnt work

in the controller its a proccessAction method... so i have the ActionRequest

i also tried converting action request to http request, like this:
HttpServletRequest request1 = PortalUtil.getHttpServletRequest(request);
HttpServletRequest originalRequest = PortalUtil.getOriginalServletRequest(request1);

and then get the attribute from originalRequest... but that didnt work either


any ideas?

thank you!
thumbnail
David H Nebinger, modificado hace 9 años.

RE: how to pass an object from jsp to controller in portlet

Liferay Legend Mensajes: 14914 Fecha de incorporación: 2/09/06 Mensajes recientes
JSP is usually part of the render phase, so the "request" is probably a RenderRequest, not an ActionRequest that the portal uses. And neither one of those are the HttpServletRequest.

So normally a request goes to the controller which then directs to the JSP for presentation; it doesn't go backwards. The JSP generates the HTML that is presented in the client browser, and the user clicks something on the page which redirects back to the controller. That part will not allow for objects at all, as it's a submission from the client browser.
Oliver Bayer, modificado hace 9 años.

RE: how to pass an object from jsp to controller in portlet

Liferay Master Mensajes: 894 Fecha de incorporación: 18/02/09 Mensajes recientes
Hi Juan,

you can use the portletSession to pass object from and to the controller.

HTH Oli
juan meza, modificado hace 9 años.

RE: how to pass an object from jsp to controller in portlet

Regular Member Mensajes: 228 Fecha de incorporación: 6/01/14 Mensajes recientes
thank you all emoticon

i managed to do it by session as Oliver sugested... here is the code, hope it helps someone

in jsp
session.setAttribute("obj", obj);



in controller, process action method:
HttpServletRequest request1 = PortalUtil.getHttpServletRequest(request);
Obj obj = (Obj)request1.getSession().getAttribute("obj");


request in the first line is
ActionRequest request
from the processAction parameters

thanks!!
thumbnail
David H Nebinger, modificado hace 9 años.

RE: how to pass an object from jsp to controller in portlet

Liferay Legend Mensajes: 14914 Fecha de incorporación: 2/09/06 Mensajes recientes
You should not be using the http session. If you have an instantiable portlet, the values will collide.

Instead in your jsp page you should be using portletSession.setAttribute() and the session object available from the action request.
juan meza, modificado hace 9 años.

RE: how to pass an object from jsp to controller in portlet

Regular Member Mensajes: 228 Fecha de incorporación: 6/01/14 Mensajes recientes
thank you David! ill take a look at it!!
thumbnail
Enrique Valdes Lacasa, modificado hace 9 años.

RE: how to pass an object from jsp to controller in portlet

Junior Member Mensajes: 92 Fecha de incorporación: 29/07/14 Mensajes recientes
Thank you Juan and David, this was a useful thread for me. I was wondering how to pass objects from JSPs to the Portlet class action phase.

Since the JSP is somehow in charge of displaying the view, I didn't feel I was doing the right thing adding code in the JSP to process an object to be sent to the back end later. In fact, it seems that there are so many threads talking about passing parameters and objects from the action and render phase to the JSP, and not the other way around. So I am not sure until what extend what I did is a good practice, but in my case separating the code between the JSP and the Java class avoided all the processing time to happen at once.

Following your comments and suggestions, I implemented this to summarize your discussion:

1) In my JSP, being obj the object that I want to use in my processAction method :

portletSession.setAttribute("myObject", obj);


2) In my portlet class, MyPortlet.java, to retreive the object within my processAction method:

PortletSession portletSession = actionRequest.getPortletSession();
Obj obj = portletSession.getAttribute("myObject");
// i then remove the object from the portlet session after being retrieved
portletSession.removeAttribute("myObject");
thumbnail
David H Nebinger, modificado hace 9 años.

RE: how to pass an object from jsp to controller in portlet

Liferay Legend Mensajes: 14914 Fecha de incorporación: 2/09/06 Mensajes recientes
JSP is processed during the render phase, but on the server. You are not sending objects from the browser back to the server, it's just caching an object you create while generating the html fragment when rendering the portlet.

The bad part about using the render phase, it can be called many times while a portal page is in front of the user. If your portlet is sitting on a page w/ other portlets, when they are processing the action phase your portlet's render will be invoked again and again.

Another thing to remember is that your action phase might never get invoked again, if the user chooses not to. So you are instantiating an object and storing it in the session (thus consuming resources on the server), for possibly no good reason.

Typically the best approach is to include something in the action request parameters so the action phase can restore the object on demand and use it.

Storing anything in the session is (for the most part) typically a bad shortcut for crossing the action/render boundary, and really should be avoided at all costs. All they do is ratchet up the resource requirements on the server side and limit your scaling options. It's much better (and often times pretty easy) to use parameters appropriately.

Just as in servlets, the only time you should store data in the session is when the data must be passed from action to action, and even then it can be avoided in many cases using parameters.

The point is to use the session with care (either the http or the portlet session) as it can affect your server side resource consumption, can impact your cluster configuration (whether you need session replication or not), etc. You should not blindly stuff things in the session w/o understanding what the consequences are.
thumbnail
Enrique Valdes Lacasa, modificado hace 9 años.

RE: how to pass an object from jsp to controller in portlet

Junior Member Mensajes: 92 Fecha de incorporación: 29/07/14 Mensajes recientes
Thank you for the complete response David. Your answer covered what I needed to know. That was one of the dilemmas I had before developing the portlet, using portlet session vs. passing a couple of parameters between the render and action phase. I will choose passing the parameters as a better option, unless I run into the scenarios that you mentioned or I am familiar with the session objects behavior.
thumbnail
Jayaram pokuri, modificado hace 9 años.

RE: how to pass an object from jsp to controller in portlet

New Member Mensajes: 22 Fecha de incorporación: 3/07/13 Mensajes recientes
Thanks David for giving detailed info on this. We can use PortletSession with scope as Application to share data between portlets. Is this recommended approach? What could be the impact if all the portlets that uses the portletSession are instanceable ?

PortletSession session = actionRequest.getPortletSession();
 session.setAttribute("commonData", commonData,PortletSession.APPLICATION_SCOPE);


Thanks,
Jayaram
thumbnail
David H Nebinger, modificado hace 9 años.

RE: how to pass an object from jsp to controller in portlet

Liferay Legend Mensajes: 14914 Fecha de incorporación: 2/09/06 Mensajes recientes
App scope is a temporary store for otherwise persisted information. It too has a memory impact. It can be useful when permanently caching info from a database to alleviate database fetching, but I question whether it is better to use app scope vs a well-defined ehcache setting for the data.

But I would be reluctant to suggest storing volatile data in the app scope. First it will not get replicated in a cluster unless you're using session replication, and replication has issues of it's own and obvious reasons to avoid it.

Also there is risk involved with app scope storage such as not participating as part of a transaction, it is not thread safe so multiple threads can try updating the same attribute at the same time, a crash of the server results in the loss of that data, ...

There are lots of reasons not to use app scope, and few reasons when it makes sense.
thumbnail
Jayaram pokuri, modificado hace 9 años.

RE: how to pass an object from jsp to controller in portlet

New Member Mensajes: 22 Fecha de incorporación: 3/07/13 Mensajes recientes
Thanks for the info. In general, all applications are configured with cluster, session replication with sticky session, I will avoid portletSession with application scope while developing portlets.
thumbnail
Eduardo Pantoja Ramírez, modificado hace 7 años.

RE: how to pass an object from jsp to controller in portlet

New Member Mensajes: 8 Fecha de incorporación: 24/08/13 Mensajes recientes
Hi, i can see that this thread is old an all so i hope that this helps some other guy with the same question. As David correctly points in a message up here, the cleaner and therefor best way to avoid using the session due issues of performance, reusability of the variables concurrence of multi instanceable portlets etc, is to pass data from the view to the controller in form of parameters attached to the URL of the Action or Resource URL which you are using to call your controller, to do this is as simple of adding the following to your jsp:

<portlet:actionurl var="someVar">
    <portlet:param name="someParameter" value="someStringValue" />
</portlet:actionurl>
//Some code here and there...

<aui:form action="<%= someVar %>" enctype="multipart/form-data" method="post">
// Some data on the form...
</aui:form>


This will encrypt the variable someParameter on the URL to you Process Action in the controller and to retrieve this variable you only need to writ the following inside the processAction method of your cntroller:


String anyString = actionRequest.getParameter("someParameter");
// do what you need with the value of the string...


you don't need to explicitly use a aui:form, you can use an ajax call to the url if you are using portlet:resourceURL instead of portlet:actionURL you just need to retrieve the parameter fom the serveResource method on your controller...

Hope this helps! emoticon