Reputation: 744
I'm trying to write a little piece of code to show in a portlet for Liferay the name of the user logged.
My problem, a part of a programming problem, is that I don't understand how exactly is the connection between a java class and the view.jsp file.
I know and I'm able to show the user name and photo just with the view.jsp file, but I'm not able to achieve using also a java class.
I've tried it with the processAction() method and the doView() method. I guess that we need to save the parameters to show the information in the actionResponse to render it in the portlet, but I'm in fact a little bit lost and messy. One of my question, for example is: Should I write also in the view.jsp file? Or can I do everything only with my java class? I attach the code of my java class.
Any help will be very greatful. Many thanks in advance. Rafa
public class UserInfo extends MVCPortlet{
@Override
public void processAction( ActionRequest actionRequest, ActionResponse actionResponse)
throws IOException, PortletException
{
ThemeDisplay themeDisplay = (ThemeDisplay)
actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
User user = themeDisplay.getUser();
PortletPreferences prefs = actionRequest.getPreferences();
String userName = (String) actionRequest.getParameter("UserInfo");
userName = user.getFullName();
if (userName != null)
{
prefs.setValue("UserInfo", userName);
prefs.store();
}
actionResponse.setRenderParameter("userName", userName);
super.processAction(actionRequest, actionResponse);
}
}
Upvotes: 1
Views: 1359
Reputation: 2715
Since in the life cycle of a Portlet, the render process follows the action process except for the resourceRequest, the doView method would be called after action processing is finished. Even though you can output html stuffs from there, it is not a good idea to do so. A portlet class is better treated as a controller which dispatches render requests to other jsps. This way you won't bloat your portlet class and make your codes easily maintainable.
There could be several ways to do this. If the information you want to pass is a string, you can use ActionResponse to set render parameters - ActionResponse.setRenderParameter(name, value)
which latter will be retrieved by RenderRequest. If there are objects to pass and your portlet server support portlet 2.0 specs, you can set attribute to ActionRequest and read it from your view codes - you need to set actionScopedRequestAttributes to true like this in the portlet.xml file.
<container-runtime-option>
<name>javax.portlet.actionScopedRequestAttributes</name>
<value>true</value>
<container-runtime-option>
In side the doView method, call:
getPortletContext().getRequestDispatcher(jspPage).include(request, response);
to dispatch render request to the jsp page you want.
Upvotes: 0
Reputation: 48057
Keep in mind that you can't write to the output stream from processAction() - it needs to be the render phase of a portlet.
Regarding the place where you generate the actual output of HTML: I strongly recommend anything "above servlet level" - e.g. jsp, jsf or any other technology, even in the simplest cases. Liferay's MVC Portlet provides some nice defaults that you just need to utilize and go to view.jsp.
outputStream.write("my html code"); is not maintainable for anything above this level of sophistication - you better choose the view technology now. It might help to consider the portlet as the controller that needs a view and operates on a model.
Edit (following your comment):
public void doView(
RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {
// Note: This is not HTML-escaped. Just for demo. Don't try this at home
renderResponse.getWriter().write("The username is " +
renderRequest.getParameter("userName"));
}
Upvotes: 2