Reputation: 830
I have big trouble how to use action and rendering phase properly in Liferay.
I am showing different links out of a list of objects using a for-loop. Now I want to click on one of the link and go to the next .jsp. The content of the new .jsp depends on an id connected to object/link.
How do I do that?
My code looks like this so far:
ePfad.jsp
<% EpfadUtil all = new EpfadUtil();
List pfade = all.getEpfad();
for(int i = 0; i < pfade.size(); i++){
Epfad pfad = (Epfad) pfade.get(i);
//actionResponse.setRenderParameter("id", String.valueOf(pfad.getEpfadId()));
%>
<a href = "<portlet:actionURL name="showById"><portlet:param name="mvcPath" value="/html/wbm/user/ePfadAnmelden.jsp?id=<%=pfad.getEPfadId() %>" /> </portlet:actionURL>"> <%=pfad.getTitle().getName()%> </a>
<% } %>
WBM_Portlet.java
public class WBM_Portlet extends MVCPortlet {
public void showById(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException {
PortletPreferences prefs = actionRequest.getPreferences();
String id = actionRequest.getParameter("id");
if(id != null) {
prefs.setValue("id", id);
prefs.store();
}
}
//[...]
}
My code will not redirect to the right jsp and the id is also not correct.
Thank you and best regards.
Solution:
ePfad.jsp
<portlet:renderURL var="showByIdRenderURL">
<portlet:param name="mvcPath" value="/html/wbm/user/ePfadAnmelden.jsp" />
<portlet:param name="id" value="<%=String.valueOf(pfad.getEPfadId()) %>" />
</portlet:renderURL>
<a href="<%=showByIdRenderURL%>"><%=pfad.getTitle().getName() %></a>
WBM_Portlet.java
public void showById(RenderRequest request,
RenderResponse response) throws IOException, PortletException
{
PortletPreferences prefs = request.getPreferences();
String id = request.getParameter("id");
if(id != null)
{
prefs.setValue("id", id);
prefs.store();
}
}
Upvotes: 0
Views: 249
Reputation: 11698
If you just want to show another JSP then you should use the render phase and not action phase. So here is how you can use the renderURL:
<portlet:renderURL var="showByIdRenderURL">
<portlet:param name="mvcPath" value="/html/wbm/user/ePfadAnmelden.jsp %>" />
<portlet:param name="id" value="<%= String.valueOf(pfad.getEPfadId()) %>" />
</portlet:renderURL>
<a href="<%= showByIdRenderURL %>"><%=pfad.getTitle().getName()%></a>
And in render
method or doView
method you can write your logic to store the preference from the parameter id
(please note how the id
in the above URL is set as a parameter, the way you did it is incorrect)
If you want to still use action phase (not recommended for such things, it is better suited for form-posts) then you can do the following:
<portlet:actionURL name="showById" var="showByIdActionURL">
<portlet:param name="mvcPath" value="/html/wbm/user/ePfadAnmelden.jsp %>" />
<portlet:param name="id" value="<%= String.valueOf(pfad.getEPfadId()) %>" />
</portlet:actionURL>
<a href="<%= showByIdActionURL %>"><%=pfad.getTitle().getName()%></a>
In your action method:
public void showById(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException {
PortletPreferences prefs = actionRequest.getPreferences();
String id = actionRequest.getParameter("id");
if(id != null) {
prefs.setValue("id", id);
prefs.store();
}
String mvcPath = actionRequest.getParameter("mvcPath");
// this would tell the render method to show the required JSP
actionResponse.setRenderParameter("mvcPath", mvcPath);
}
Hope this answers the question.
But if you really want to develop something substantial and learn i would strongly suggest you go through some good tutorials which explains the concepts of the different phases of portlet life-cycle or some good books like Portlets in Action
or Liferay in Action
, even by going through the stackwiki links - liferay and portlet would give you a better idea.
Upvotes: 3