0

我正在创建一个简单的 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>

问题是视图上的输出始终是 Hello!。尽管用户在输入框中输入了一个名称,但没有将任何名称传递给输出。为什么?

4

1 回答 1

1

更改您的 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);

}

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

于 2013-03-15T12:29:16.810 回答