Question

this is in my portlet class :

import java.io.IOException;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletMode;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.PortletURL;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class HelloYouPortlet extends GenericPortlet {
    public void init() throws PortletException {
        editJSP = getInitParameter("edit-jsp");
        viewJSP = getInitParameter("view-jsp");
    }

    public void doEdit(RenderRequest renderRequest,
            RenderResponse renderResponse) throws IOException, PortletException {
        renderResponse.setContentType("text/html");
        PortletURL addNameURL = renderResponse.createActionURL();
        addNameURL.setParameter("addName", "addName");
        renderRequest.setAttribute("addNameURL", addNameURL.toString());
        include(editJSP, renderRequest, renderResponse);
    }

    public void doView(RenderRequest renderRequest,
            RenderResponse renderResponse) throws IOException, PortletException {
        PortletPreferences prefs = renderRequest.getPreferences();
        String username = (String) prefs.getValue("name", "no");
        if (username.equalsIgnoreCase("no")) {
            username = "";
        }
        renderRequest.setAttribute("userName", username);
        include(viewJSP, renderRequest, renderResponse);
    }

    public void processAction(ActionRequest actionRequest,
            ActionResponse actionResponse) throws IOException, PortletException {
        String addName = actionRequest.getParameter("addName");
        if (addName != null) {
            PortletPreferences prefs = actionRequest.getPreferences();
            prefs.setValue("name", actionRequest.getParameter("username"));
            prefs.store();
            actionResponse.setPortletMode(PortletMode.VIEW);
        }
    }

    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 editJSP;
    protected String viewJSP;
    private static Log _log = LogFactory.getLog(HelloYouPortlet.class);

}

and this is on my view.jsp :

<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
<jsp:useBean id="userName" class="java.lang.String" scope="request" />
<portlet:defineObjects />
<p>This is the Hello You portlet.</p>
<p>Hello <%= userName %>!</p>

and my edit.jsp :

<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet"%>
<jsp:useBean class="java.lang.String" id="addNameURL" scope="request" />
<portlet:defineObjects />
<form id="<portlet:namespace />helloForm" action="<%=addNameURL%>"
    method="post">
    <table>
        <tr>
            <td>Name:</td>
            <td><input type="text" name="username"></td>
        </tr>
    </table>
    <input type="submit" id="nameButton" title="Add Name" value="Add Name">
</form>

when I deploy it in command prompt, I got "build successful" but when I install it on liferay portal and I got java.lang.NullPointerException at line 3 in doView method (if statament), please tell me why my local variable username got null. By the way, this portlet example is on liferay in action book. thanks for reading my question.

Était-ce utile?

La solution 3

Handle for null where you are getting java.lang.NullPointerException, It should be simple Nessaj :)

 if (null != username && username.equalsIgnoreCase("no")) {
                username = "";
    }

Autres conseils

You need to check for null in your doView() method, because you are saving name in the preferences only in Edit Mode, which will not have been invoked when you access your portlet for the first time.

If what I said doesn't make sense to you, go through http://www.javaworld.com/article/2073645/soa/introducing-the-portlet-specification--part-1.html

I had the same issue when trying to run through the portlet training with Liferay v6.2.

The problem was due to actionRequest.getParameter("username") returning null in HelloYouPortlet's processAction.

It seems 6.2 requires both the form and input fields to be namespaced. So the fix was to change <input type="text" name="username"> to <input type="text" name="<portlet:namespace />username"> in edit.jsp.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top