Foren

Send object from action method to jsp

Hari Dhanakoti, geändert vor 11 Jahren.

Send object from action method to jsp

New Member Beiträge: 14 Beitrittsdatum: 22.07.12 Neueste Beiträge
Am a new bie with liferay and facing some issues with passing values back and forth.

I have a method "public void getNextComments(ActionRequest actionRequest, ActionResponse actionResponse)". I want to send an ArrayList from the MVCPortlet (liferay) to jsp. How can i retrieve it from the jsp end?

I tried setting up object as attribute but not sure how to retrieve the same on the UI.

actionRequest.setAttribute("socialactivities", socialActivities);

(HTTPServletRequest) request.setAttribute("socialactivities", socialActivities);

Please help me on this.
thumbnail
Pinkesh Gandhi, geändert vor 11 Jahren.

RE: Send object from action method to jsp

Junior Member Beiträge: 99 Beitrittsdatum: 27.01.12 Neueste Beiträge
Hi Hari,

Yes, you can set the list object into actionRequest and then can retrieve it on jsp in following way,

In your MVCPortlet,


actionRequest.setAttribute("socialactivities",socialActivities);


And at jsp side,


<%
    List socialActivities = (List) renderRequest.getAttribute("socialactivities");
 %>


I hope it may resolves your problem.
Hari Dhanakoti, geändert vor 11 Jahren.

RE: Send object from action method to jsp

New Member Beiträge: 14 Beitrittsdatum: 22.07.12 Neueste Beiträge
I tried the same but couldn't get object on the jsp side. Do i miss any other configuration?
Java code:

List<SocialActivity> socialActivities = SocialActivityLocalServiceUtil.getUserActivities(user2Id, end, activitiesPerPage+end);
actionRequest.setAttribute("socialactivities", socialActivities);

JSP code:

List fetchedActivities = (List) renderRequest.getAttribute("socialactivities");
List fetchedActivities = (List) request.getAttribute("socialactivities");

Both of these didn't work any other clues to resolve the same?
thumbnail
Jignesh Vachhani, geändert vor 11 Jahren.

RE: Send object from action method to jsp

Liferay Master Beiträge: 803 Beitrittsdatum: 10.03.08 Neueste Beiträge
Please try this :

in process action :

actionRequest.setAttribute("socialactivities", socialActivities);

in render method :

String socActivit y= renderRequest.getAttribute("socialactivities");
renderRequest.setAttribute("socialactivities", socActivit );

In JSP:

List fetchedActivities = (List) request.getAttribute("socialactivities");
thumbnail
Rewati Raman, geändert vor 11 Jahren.

RE: Send object from action method to jsp

Junior Member Beiträge: 97 Beitrittsdatum: 24.02.12 Neueste Beiträge
Hari Dhanakoti:
Am a new bie with liferay and facing some issues with passing values back and forth.

I have a method "public void getNextComments(ActionRequest actionRequest, ActionResponse actionResponse)". I want to send an ArrayList from the MVCPortlet (liferay) to jsp. How can i retrieve it from the jsp end?

I tried setting up object as attribute but not sure how to retrieve the same on the UI.

actionRequest.setAttribute("socialactivities", socialActivities);

(HTTPServletRequest) request.setAttribute("socialactivities", socialActivities);

Please help me on this.

you just need to do two things
first:
with your actionRequest setAttrribute wat object you want to send with attribute name in double quote
second:
with your actionResponse redirect where you want to use that object

code would be like this
at MVCPortlet

actionRequest.setAttribute("Object",ArrayListObject);
actionResponse.setRenderParameter("jspPage","/html/reference.jsp");


at the respective Jsp you need to fetch the object

List myArrayList=new ArrayList(); 
myArrayList=(List)request.getAttribute("Object");


now you can use your ArrayList

Thanks
Rewati Raman
Hari Dhanakoti, geändert vor 11 Jahren.

RE: Send object from action method to jsp

New Member Beiträge: 14 Beitrittsdatum: 22.07.12 Neueste Beiträge
I tried setting up the parameters in actionReponse and got he following Exception

actionResponse.setRenderParameter("jspPage","/super_wall/view_wall.jspf");
and
actionResponse.setRenderParameter("socialactivities", socialActivities);

08:50:56,629 ERROR [jsp:996] java.lang.IllegalStateException: Set render parameter has already been called
thumbnail
Jitendra Rajput, geändert vor 11 Jahren.

RE: Send object from action method to jsp

Liferay Master Beiträge: 875 Beitrittsdatum: 07.01.11 Neueste Beiträge
Are you using sendRedirect() at the end of your processAction ?
Hari Dhanakoti, geändert vor 11 Jahren.

RE: Send object from action method to jsp

New Member Beiträge: 14 Beitrittsdatum: 22.07.12 Neueste Beiträge
Method definition starts as such

@ProcessAction(name = "getNextComments")
public void getNextComments(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {

actionRequest.setAttribute("socialactivities", socialActivities); / Not working
actionResponse.setParameter("jspPage,"/asdf/asdf..jsp"); / Throws Exception

}
thumbnail
Harish Kumar, geändert vor 11 Jahren.

RE: Send object from action method to jsp

Expert Beiträge: 483 Beitrittsdatum: 31.07.10 Neueste Beiträge
Hi Hari,

Hari Dhanakoti:
Method definition starts as such

@ProcessAction(name = "getNextComments")
public void getNextComments(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {

actionRequest.setAttribute("socialactivities", socialActivities); / Not working
actionResponse.setParameter("jspPage,"/asdf/asdf..jsp"); / Throws Exception

}



1. Set the list in the actionRequest as

actionRequest.setAttribute("socialactivities", socialActivities);

2. In jsp use the request (not renderRequest) object to get the attribute as

List<String> list = (List<String>)request.getAttribute("socialactivities");

Here I assume that list is of type String. modify according to your list type.


Instead of setting the jsp page in action method as you are doing -

actionResponse.setParameter("jspPage,"/asdf/asdf..jsp"); / Throws Exception

Set a renderParameter in action method and fetch that value in render method. based on this value redirect the user to desired page.


Regards,
thumbnail
Rewati Raman, geändert vor 11 Jahren.

RE: Send object from action method to jsp

Junior Member Beiträge: 97 Beitrittsdatum: 24.02.12 Neueste Beiträge
Hari Dhanakoti:
I tried setting up the parameters in actionReponse and got he following Exception

actionResponse.setRenderParameter("jspPage","/super_wall/view_wall.jspf");
and
actionResponse.setRenderParameter("socialactivities", socialActivities);

08:50:56,629 ERROR [jsp:996] java.lang.IllegalStateException: Set render parameter has already been called



you cannot use twice setRenderParameter

setRenderParameter is used for page redirection

so before setting setRenderParameter you need to use


actionRequest.setAttribute("ArrayListName",ArrayList);


but you were using setRenderParameter with actionResponse for setting attribute that is why you are getting that exception

use the above code instead your problem will get resolved

Regards,
Rewati Raman
Natasha Pelovska, geändert vor 11 Jahren.

RE: Send object from action method to jsp

New Member Beitrag: 1 Beitrittsdatum: 05.12.12 Neueste Beiträge
Hello, I am having the same problem and i tried everything from above but it doesn't work.
The method in my portlet i have:
request.setAttribute("subject",subject);
response.setRenderParameter("jspPage","/html/display/second.jsp");
where subject is a database object
then in second.jsp i am trying to get subject
Subject subject = (Subject)renderRequest.getAttribute("subject");
i tried like this also
Subject subject=new Subject();
subject=(Subject)request.getAttribute("subject");
but it always gives me null pointer exception
btw in my first jsp i already have set action of the form to corresponding url.