Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Hari Dhanakoti
Send object from action method to jsp
July 26, 2012 9:57 AM
Answer

Hari Dhanakoti

Rank: New Member

Posts: 14

Join Date: July 21, 2012

Recent Posts

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.
Pinkesh Gandhi
RE: Send object from action method to jsp
July 26, 2012 11:57 AM
Answer

Pinkesh Gandhi

Rank: Regular Member

Posts: 100

Join Date: January 26, 2012

Recent Posts

Hi Hari,

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

In your MVCPortlet,

1
2actionRequest.setAttribute("socialactivities",socialActivities);


And at jsp side,

1
2<%
3    List socialActivities = (List) renderRequest.getAttribute("socialactivities");
4 %>


I hope it may resolves your problem.
Hari Dhanakoti
RE: Send object from action method to jsp
July 26, 2012 10:12 PM
Answer

Hari Dhanakoti

Rank: New Member

Posts: 14

Join Date: July 21, 2012

Recent Posts

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?
Jignesh Vachhani
RE: Send object from action method to jsp
July 26, 2012 10:32 PM
Answer

Jignesh Vachhani

Rank: Liferay Master

Posts: 785

Join Date: March 10, 2008

Recent Posts

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");
Rewati Raman
RE: Send object from action method to jsp
July 26, 2012 10:54 PM
Answer

Rewati Raman

Rank: Junior Member

Posts: 97

Join Date: February 24, 2012

Recent Posts

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
1
2actionRequest.setAttribute("Object",ArrayListObject);
3actionResponse.setRenderParameter("jspPage","/html/reference.jsp");


at the respective Jsp you need to fetch the object
1
2List myArrayList=new ArrayList();
3myArrayList=(List)request.getAttribute("Object");


now you can use your ArrayList

Thanks
Rewati Raman
Hari Dhanakoti
RE: Send object from action method to jsp
July 27, 2012 2:01 AM
Answer

Hari Dhanakoti

Rank: New Member

Posts: 14

Join Date: July 21, 2012

Recent Posts

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
Jitendra Rajput
RE: Send object from action method to jsp
July 27, 2012 2:18 AM
Answer

Jitendra Rajput

Rank: Liferay Master

Posts: 858

Join Date: January 7, 2011

Recent Posts

Are you using sendRedirect() at the end of your processAction ?
Hari Dhanakoti
RE: Send object from action method to jsp
July 27, 2012 2:27 AM
Answer

Hari Dhanakoti

Rank: New Member

Posts: 14

Join Date: July 21, 2012

Recent Posts

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

}
Harish Kumar
RE: Send object from action method to jsp
July 27, 2012 2:43 AM
Answer

Harish Kumar

Rank: Expert

Posts: 471

Join Date: July 31, 2010

Recent Posts

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,
Rewati Raman
RE: Send object from action method to jsp
July 27, 2012 7:58 AM
Answer

Rewati Raman

Rank: Junior Member

Posts: 97

Join Date: February 24, 2012

Recent Posts

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

1
2actionRequest.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
N P
RE: Send object from action method to jsp
December 5, 2012 3:20 PM
Answer

N P

Rank: New Member

Posts: 1

Join Date: December 5, 2012

Recent Posts

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.