gpt4 book ai didi

java - liferay portlet 中的 NullPointerException

转载 作者:行者123 更新时间:2023-12-02 06:04:56 28 4
gpt4 key购买 nike

这是在我的 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(如果有声明),请告诉我为什么我的本地变量用户名为空。顺便说一句,这个 portlet 示例位于《action book》中的 liferay 上。感谢您阅读我的问题。

最佳答案

您需要检查 doView() 方法中是否为 null,因为您仅在编辑模式下将 name 保存在首选项中,而在编辑模式下不会调用该模式您是第一次访问您的 portlet。

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

关于java - liferay portlet 中的 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22369834/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com