生命周期portlet中的NullPointerException



这是在我的portlet类中:

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);
}

,这是在我的视图。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>

和我的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>

当我在命令提示符中部署它时,我得到了"构建成功",但是当我将它安装在liferay门户上时,我在doView方法(if语句)的第3行得到了java.lang.NullPointerException,请告诉我为什么我的本地变量username得到了null。顺便说一下,这个portlet示例是一本生活手册。谢谢你阅读我的问题

您需要检查doView()方法中的null,因为您只在Edit Mode下将name保存在首选项中,当您第一次访问portlet时不会调用它。

如果你不明白我说的,请浏览http://www.javaworld.com/article/2073645/soa/introducing-the-portlet-specification--part-1.html

我在尝试使用Liferay v6.2运行portlet培训时遇到了同样的问题。

问题是由于HelloYouPortletprocessActionactionRequest.getParameter("username")返回null

似乎6.2要求表单和输入字段都有命名空间。因此,解决方案是将edit.jsp中的<input type="text" name="username">更改为<input type="text" name="<portlet:namespace />username">

处理java.lang中的null。NullPointerException,它应该是简单的Nessaj:)

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

相关内容

最新更新