2
votes

I have to write a portlet for Liferay Portal. Liferay provides convenient class MVCPortlet which allows to use simple routing for action phase using "name" attribute of <portlet:actionURL> tag and simple routing for view phase using mvcPath parameter for <renderURL> tag. What I miss, though, is the ability to prepare view for render phase. For example load some entities from database for referencing from the JSP.

I understand that I can include Java code in the JSP, but that's considered bad practice.

I use Liferay 6.1.

2
you get database entities in render method or in action method and set it in request attribute and get them in jsp and showPankaj Kathiriya

2 Answers

1
votes

You need to define your different views (jsp) in portlet.xml's init-param tag as following:

<init-param>
    <name>view-jsp</name>
    <value>/jsp/view.jsp</value>
</init-param>

<init-param>
    <name>edit-jsp</name>
    <value>/jsp/edit.jsp</value>
</init-param>

<init-param>
    <name>search-jsp</name>
    <value>/jsp/search.jsp</value>
</init-param>

Send extra parameter from your portlet:renderURL / portlet:actionURL to indentify your action-type as

<portlet:actionURL var="editPageURL">
<portlet:param name="action" value="edit"/>
</portlet:actionURL>

Then in your (Java file), set global variable that is accessible in both phases (render / action) and filter action through type parameter as:

String action = ParamUtil.getString(request, "action", "view");
if(action.equals("edit")){
    // do your work here
}else if(action.equals("search")){
}else{
}
1
votes

There's more than 1 ways to do what you want.

  1. Make sure that a specific action is called before rendering a specific page. Fetch your resources there, and pass them as render parameters, so the jsp can get them. practically, you'll use actions to redirect to different pages
  2. Override your render function, check the page that will be loaded, and fetch your resources if needed for the rendered jsp
  3. use the resource phase in your jsp to call for the required resources.

Also make sure to understand the 2-phase (3-phase actually if you count the resource phase too) architecture of the MVCPortlet.

Now, about keeping the jsp code clean.. I'm not sure that the MVCPortet helps you do that. I've never seen an mvcPortlet project that doesn't have injected java snippets inside the jsp code here and there. For example, you'll have to use java code to read the request attributes