Search code examples
javaliferay

Liferay Genric Portlet Dem Null Value


This is my class with @ProcessAction annotaion. When i input in text field it will show input data in the console:

public class FirstGenericDemo extends GenericPortlet {

    public void init() {
        viewTemplate = getInitParameter("view-template");
    }

    @RenderMode(name= "view")
    public void myView(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException {
        include(viewTemplate, renderRequest, renderResponse);
    }

   //Annotation
   @ProcessAction(name = "myActionMethod")
   public void myActionMethod(ActionRequest request, ActionResponse response) throws PortletException, IOException {
    // TODO Auto-generated method stub
        String name = ParamUtil.getString(request,"name");
        System.out.println("Name => " + name);
    }

    protected void include(String path, RenderRequest renderRequest,
       RenderResponse renderResponse)
    throws IOException, PortletException {

        PortletRequestDispatcher portletRequestDispatcher =
            getPortletContext().getRequestDispatcher(path);

        if (portletRequestDispatcher == null) {
            _log.error(path + " is not a valid include");
        }
        else {
            portletRequestDispatcher.include(renderRequest, renderResponse);
        }
    } 
    protected String viewTemplate;
    private static Log _log = LogFactoryUtil.getLog(FirstGenericDemo.class);

}

And here is myView.jsp:

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

<portlet:defineObjects />

This is the <b>First Generic Demo</b> portlet in View mode.

<portlet:actionURL var="myaction" name="myActionMethod">
</portlet:actionURL>
<form action="${myaction}" method="post">
Name: <input type = "text" name="name">
<input type="submit" value="SUBMIT">
</form>

But when i run it always prints "Name => null" in console. How i could fix it?


Solution

  • Parameters to a portlet typically need to be namespaced in HTML - as a page is composed of so many portlets, this is to disambiguate the individual inputs that can co-exist on the page. Change your JSP to contain

    <input type="text" name="<portlet:namespace/>name" />
    

    and you're set. On the Java side, this will be disambiguated to "name".

    You can also try to use the <aui:input /> taglibrary, as it does this decoration automagically.

    A third option is to configure Liferay-specific settings for this portlet to not require namespaced parameters. You do this in a WAR-packaged portlet in liferay-portlet.xml, or in an OSGi bundle (Liferay 7.0 and up) as a com.liferay.portlet.requires-namespaced-parameters=false component property.