Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Rick Herrick
Accessing RenderResponse and ActionResponse (and so on in Velocity template
May 2, 2011 12:41 PM
Answer

Rick Herrick

Rank: New Member

Posts: 10

Join Date: April 27, 2011

Recent Posts

Now that I've gotten Velocity templates working properly with Liferay, I need to be able to create URLs within the portal. I understand how to do that, e.g.:

1#set( $showCreateProjectUrl = $response.createRenderURL() )
2$showCreateProjectUrl.setParameter("myaction", "createProjectForm")


The problem is that I don't know how to get at the response object in the default Velocity environment. I'm assuming that there's some way to do this, similar to how you can just reference the request and response objects in JSP pages:

1<%= response.serverName %>


I've worked around it for now by explicitly adding the renderResponse object to the model data in my portlet controller:

1@RenderMapping
2public ModelAndView show(RenderResponse response) {
3    ModelMap map = new ModelMap();
4    map.put("response", response);
5    return new ModelAndView("home", map);
6}


But I'd really rather not have to do this in every single one of my controllers. Is there a way to get at this stuff through Velocity and/or some type of context or configuration that's automatically set up in the template?
Corné Aussems
RE: Accessing RenderResponse and ActionResponse (and so on in Velocity temp
May 2, 2011 12:57 PM
Answer

Corné Aussems

Rank: Liferay Legend

Posts: 1183

Join Date: October 3, 2006

Recent Posts

According VelocityVariablesImpl it should be exposed by renderResponse;

1
2        if (portletResponse != null) {
3            if (portletResponse instanceof RenderResponse) {
4                velocityContext.put("renderResponse", portletResponse);
5            }
6        }
Rick Herrick
RE: Accessing RenderResponse and ActionResponse (and so on in Velocity temp
May 2, 2011 1:14 PM
Answer

Rick Herrick

Rank: New Member

Posts: 10

Join Date: April 27, 2011

Recent Posts

Thanks Corné,

I had seen that elsewhere (not sure where) and tried to use that in my template. To test it, I put this into my template:

1#if ($renderResponse)
2    I found the renderResponse.
3#else
4    There's no renderResponse.
5#end


This gives me this output:

1There's no renderResponse.


Am I maybe missing some kind of activation or configuration that will make this work?

Also, it's worth noting that I'm not going through the Liferay Velocity stuff, which I mostly see referenced in regard to theming, but instead through the Spring Framework VelocityViewResolver. I'm guessing that's affecting it, but I'm not sure how to bring those two worlds together.
Corné Aussems
RE: Accessing RenderResponse and ActionResponse (and so on in Velocity temp
May 2, 2011 2:07 PM
Answer

Corné Aussems

Rank: Liferay Legend

Posts: 1183

Join Date: October 3, 2006

Recent Posts

Sorry i'm mistaken;

There are 4 different Variable insert functions;
insertHelperUtilities(VelocityContext, String[])
insertHelperUtility(VelocityContext, String[], String, Object)
insertTilesVariables(VelocityContext, HttpServletRequest)
insertVariables(VelocityContext, HttpServletRequest)

This is helpful;
http://docs.liferay.com/portal/5.2/javadocs/portal-impl/com/liferay/portal/velocity/VelocityVariables.java.html
explore velocity variables
Sharana Basavaraj Ballari
RE: Accessing RenderResponse and ActionResponse (and so on in Velocity temp
May 3, 2011 2:57 AM
Answer

Sharana Basavaraj Ballari

Rank: Regular Member

Posts: 128

Join Date: September 9, 2007

Recent Posts

Hi Rick,

The way we use request and response in JSP's is similar in Velocity as well. Just the syntax differs

for eg:

1#set ($myRequestVariable = $request.getAttribute("sharan"))
2
3you can get session  - $request.getSession()


So coming to your example,

You can always set Model attributes map and get the object,

1@RenderMapping
2public ModelAndView show(RenderResponse response) {
3    ModelMap map = new ModelMap();
4    map.put("response", response);
5    return new ModelAndView("home", map);
6}


here you have added response object to the map - Request and Response are implicit objects for any server side templating mechanisms. You need not pass it. You have them available all the time in your velocity context

Think that you will be adding some other object say <b>userDetails</b> to the Model Map.

Now it is easier to access userDetails in Velocity like below

#set($first_name = $userDetails.getFirstName())

#set($last_name = $userDetails.getLastName())

Spring will make the things happen in the background.

HTH,
Sharan
Rick Herrick
RE: Accessing RenderResponse and ActionResponse (and so on in Velocity temp
May 3, 2011 8:22 AM
Answer

Rick Herrick

Rank: New Member

Posts: 10

Join Date: April 27, 2011

Recent Posts

Sharana Basavaraj Ballari:
here you have added response object to the map - Request and Response are implicit objects for any server side templating mechanisms. You need not pass it. You have them available all the time in your velocity context


That's what I would expect, but that's not what I'm seeing unfortunately. I've attached a sample project that tries to access the response through both $response and $renderResponse. Here's the code in the Velocity template:

 1#set( $project = "Liferay Maven Project" )
 2<p>This is a $project using Velocity templates.</p>
 3
 4<ul>
 5#if($renderResponse)
 6    <li>I found the renderResponse.</li>
 7#else
 8    <li>There is no renderResponse.</li>
 9#end
10
11#if($response)
12    <li>I found the response.</li>
13#else
14    <li>There is no response.</li>
15#end
16</ul>


The results of this are:

1This is a Liferay Maven Project using Velocity templates.
2
3There is no renderResponse.
4There is no response.


Sharana Basavaraj Ballari:
Think that you will be adding some other object say <b>userDetails</b> to the Model Map.

Now it is easier to access userDetails in Velocity like below

#set($first_name = $userDetails.getFirstName())

#set($last_name = $userDetails.getLastName())


Right and that's basically what works if I explicitly add the response to the model map returning from the controller, but like you say I shouldn't need to do that.

Thanks for the help on this, I do appreciate it!
Attachments: LiferayMavenProject-portlet.zip (11.2k)