gpt4 book ai didi

java - 名称属性未在 Liferay Portlet 中呈现

转载 作者:行者123 更新时间:2023-12-01 14:46:02 24 4
gpt4 key购买 nike

我正在创建一个简单的 Liferay Portlet,但无法设法获取输入框中给出的名称进行渲染。主要的 Portlet 类是:

public class MyHelloWorldPortlet extends GenericPortlet {

public void init() {
editJSP = "/html/myhelloworldportlet/edit.jsp";
viewJSP = "/html/myhelloworldportlet/view.jsp";
}

public void processAction(
ActionRequest actionRequest, ActionResponse actionResponse)
throws IOException, PortletException {

String name = actionRequest.getParameter("name");
PortletPreferences prefs = actionRequest.getPreferences();

prefs.setValue("Name", name);
prefs.store();

actionResponse.setPortletMode(PortletMode.VIEW);
}

public void doEdit(
RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {

include(editJSP, renderRequest, renderResponse);
}

public void doView(
RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {

PortletPreferences prefs = renderRequest.getPreferences();
String defaultValue = "";
String name = prefs.getValue("NAME", defaultValue);

renderRequest.setAttribute("ATTRIBUTE_NAME", name);

include(viewJSP, renderRequest, renderResponse);
}

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 = LogFactoryUtil.getLog(MyHelloWorldPortlet.class);

}

edit.jsp 文件是:

<form action="<portlet:actionURL/>" method="post" name="<portlet:namespace/>fm">
<label for="<portlet:namespace/>name">Name:</label>
<input name="<portlet:namespace/>name" type="text">
<br/><br/>

<input type="submit" value="Add Name">
</form>

view.jsp 文件是:

<%
String name = (String)request.getAttribute("ATTRIBUTE_NAME");
%>


<p>Hello <%= name %>!</p>

问题是 View 上的输出始终是 Hello!。尽管用户在输入框中输入了名称,但并未将任何名称传递到输出。为什么?

最佳答案

更改你的 doView 方法:

public void doView(
RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {

PortletPreferences prefs = renderRequest.getPreferences();
String defaultValue = "";
// you need to recover the preference using the same name you used when you set it
String name = prefs.getValue("Name", defaultValue);

renderRequest.setAttribute("ATTRIBUTE_NAME", name);

include(viewJSP, renderRequest, renderResponse);

}

在我看来,你的其余代码都很好。

关于java - 名称属性未在 Liferay Portlet 中呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15431909/

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