liferayliferay-6liferay-ide

Edit message portlet in liferay


import java.io.IOException;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletPreferences;
import javax.portlet.ReadOnlyException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ValidatorException;

import com.liferay.util.bridges.mvc.MVCPortlet;

public class GreetingMessage extends MVCPortlet {

public static final String GREETING = "greeting";
public static final String DEFAULT_GREETING = "Hello! It's my default greeting message";

@Override
public void render(RenderRequest request, RenderResponse response)
        throws IOException, PortletException {

    PortletPreferences preferences = request.getPreferences();
    request.setAttribute(GREETING,
            preferences.getValue(GREETING, DEFAULT_GREETING));
    super.render(request, response);
}

public void updateGreeting(ActionRequest request, ActionResponse response)
        throws ValidatorException, IOException, ReadOnlyException {

    String greeting = request.getParameter("greeting");
    PortletPreferences prefs = request.getPreferences();

    if (greeting != null) {
        prefs.setValue(GREETING, greeting);
        prefs.store();
        request.setAttribute(GREETING, greeting);
    }
}

}

view.jsp:

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> 
<portlet:defineObjects /> 

<p>${greeting}</p>

<portlet:renderURL var="editGreetingURL">
      <portlet:param name="mvcPath" value="/html/greetingmessage/edit.jsp"/>
</portlet:renderURL>

<p><a href="${editGreetingURL}">Edit greeting</a></p>

edit.jsp

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<%@ page import="javax.portlet.PortletPreferences" %>

<portlet:defineObjects />

<portlet:actionURL name="updateGreeting" var="updateGreetingURL"> 
 </portlet:actionURL>

<aui:form action="<%= updateGreetingURL %>" method="post">
<aui:input label="greeting" name="greeting" type="text" value="${greeting}" />
<aui:button type="submit" />
</aui:form>

<portlet:renderURL var="viewGreetingURL"> 
     <portlet:param name="mvcPath" value="/html/greetingmessage/view.jsp" />
</portlet:renderURL>

<p><a href="${viewGreetingURL}">&larr; AND NOW IT'S BACK</a></p>

It's tested code of my "edit-greeting" portlet. The question is, how can I make localization??? I've read a lot of docs but it's all for nothing. I created in WEB-INF folder src/language.properties and src/language_es.properties. What should I do next? Help me please. @Shivam


Solution

  • To Answer your question 1)You can handle your attributes and portlet preferences in render method and set them as attributes in render request,which can be subsequently read in your jsp via some scripting language like jstl 2)There is no need to make changes in portlet.xml file. The init params as the name suggests are added to provide some params needed for initializing a portlet view.

    You need to make the below changes to the render method

    public void render(RenderRequest req,RenderResponse res) throws IOException, PortletException
    {
        String greeting = req.getParameter("greeting");
        PortletPreferences prefs = req.getPreferences();
        String defaultGreeting="Hello! Welcome to our portalOLOLOLOLOL.";
    
        if(prefs.getValue("greeting","true")==null)
        {
            prefs.setValue("greeting", defaultGreeting);
        }
        if (greeting != null) 
        {
            prefs.setValue("greeting", greeting);
            prefs.store();
            req.setAttribute("greeting", prefs.getValue("greeting","true"));
        }
        else
        {
            req.setAttribute("greeting", prefs.getValue("greeting","true"));
        }
        super.render(req,res);
    }
    

    There will not be any changes required in view.jsp and edit.jsp(apart from removing code),Hence I forgot to mention the same. As for the render method,the best approach would be defintely to use action url and use action method,but since it seems you are looking to try out some approach and to make mininmum changes to your,I have kept it render only. As for the code,the prefs.getValue("greeting","true") checks whether a certain attribute is present in portlet preferences or not.

    Updated with process action

    public class NewPortlet extends MVCPortlet {

    public static final String GREETING="greeting";
    
    @Override
    public void render(RenderRequest req,RenderResponse res) throws IOException, PortletException
    {
        PortletPreferences prefs = req.getPreferences();
        String defaultGreeting="Hello! Welcome to our portalOLOLOLOLOL.";
    
        if(prefs.getValue(GREETING,"true")==null)
        {
            prefs.setValue(GREETING, defaultGreeting);
            prefs.store();
        }
        req.setAttribute(GREETING, prefs.getValue(GREETING,"true"));
        super.render(req,res);
    }
    
    public void updateGreeting(ActionRequest req,ActionResponse res) throws ValidatorException, IOException, ReadOnlyException
    {
        String greeting = req.getParameter("greeting");
        PortletPreferences prefs = req.getPreferences();
    
        if (greeting != null) 
        {
            prefs.setValue(GREETING, greeting);
            prefs.store();
            req.setAttribute(GREETING, greeting);
        }
    
    }
    

    }

    Updates in edit jsp

      <portlet:actionURL name="updateGreeting" var="updateGreetingURL"> 
     </portlet:actionURL>
    
    <aui:form action="<%= updateGreetingURL %>" method="post">
    <aui:input label="greeting" name="greeting" type="text" value="${greeting}" />
    <aui:button type="submit" />
    </aui:form>