繁体   English   中英

liferay portlet中的NullPointerException

[英]NullPointerException in liferay portlet

这是在我的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);

}

这是我的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>

和我的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方法的第3行得到java.lang.NullPointerException(如果是statament),请告诉我为什么我的本地变量用户名得到了空值。 顺便说一句,这个portlet示例在行动书中的liferay上。 谢谢你阅读我的问题。

您需要在doView()方法中检查null,因为您只在编辑模式中将name保存在首选项中,这在首次访问portlet时不会被调用。

如果我说的话对你没有意义,请浏览http://www.javaworld.com/article/2073645/soa/introducing-the-portlet-specification--part-1.html

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

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

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

处理null,你得到java.lang.NullPointerException,它应该是简单的Nessaj :)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM