vbezhenar
vbezhenar

Reputation: 12326

How to prepare view jsp using Liferay MVC Portlet

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.

Upvotes: 2

Views: 3974

Answers (2)

yannicuLar
yannicuLar

Reputation: 3133

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

Upvotes: 1

Parkash Kumar
Parkash Kumar

Reputation: 4730

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{
}

Upvotes: 1

Related Questions