Reputation: 12326
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
Reputation: 3133
There's more than 1 ways to do what you want.
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
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