当前位置 : 主页 > 编程语言 > java >

jsp – 重定向后的Liferay portlet doView()

来源:互联网 收集:自由互联 发布时间:2021-06-25
我有一个带有几个jsp页面的portlet,每个页面都包含一个带有提交按钮的表单.当用户在第一个jsp上提交表单时,我将数据保存在db中并调用actionResponse.sendRedirect()以将用户转发到下一个jsp页
我有一个带有几个jsp页面的portlet,每个页面都包含一个带有提交按钮的表单.当用户在第一个jsp上提交表单时,我将数据保存在db中并调用actionResponse.sendRedirect()以将用户转发到下一个jsp页面.我必须使用重定向,以防止用户通过按F5意外重新提交下一页上的相同数据.一切正常,在重定向到第二页后,doView()方法没有被执行,我需要在渲染页面之前加载一些数据.这是正常行为还是我做错了什么?在渲染第二个jsp之前,我在哪里可以加载这些数据?

第一个jsp

<%-- ... --%>
<portlet:actionURL name="createNewMyCustomObj" var="createNewMyCustomObjURL" />
<aui:form action="<%= createNewMyCustomObjURL.toString() %> method="post">
    <%-- form fields --%>
    <aui:button type="submit" value="Create" />
</aui:form>

第二个jsp

<%
    MyCustomObj obj = (MyCustomObj) renderRequest.getAttribute("myCustomObj");
%>

<portlet:actionURL name="updateMyCustomObj" var="updateMyCustomObjURL" />
<aui:form action="<%= updateMyCustomObjURL.toString() %> method="post">
    <%-- fields with values from myCustomObj --%>
    <aui:button type="submit" value="Update" />
</aui:form>

MyMVCPortlet.java

public class MyMVCPortlet extends MVCPortlet {

    public createNewMyCustomObj(ActionRequest actionRequest,
         ActionResponse actionResponse) throws IOException, PortlalException, SystemException {

        PortletURL redirectURL = null;
        try {
            // get data from request
            // save it to db
            redirectURL = createRedirectURL(actionRequest, "second.jsp");
        } catch(Exception e) {
            SessionErrors.add(actionRequest, "error-key")
            // errors saving data, stay on the same page
            redirectURL = createRedirectURL(actionRequest, "first.jsp");
        }
        actionResponse.sendRedirect(redirectURL.toString());
    }

    private PortletURL createRedirectURL(ActionRequest actionRequest, String jspPage) {
        ThemeDisplay themeDisplay = (ThemeDisplay)         actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
        String portletName = (String) actionRequest.getAttribute(WebKeys.PORTLET_ID);

        PortletURL redirectURL = PortletURLFactoryUtil.create(
            actionRequest, portletName, themeDisplay.getLayout().getPlid(),      PortletRequest.RENDER_PHASE);
        redirectURL.setParameter("jspPage", jspPage);

        return redirectURL;
    }

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

        /*
         * doView() is never called after redirecting to second.jsp
         */
        if(/* second jsp page */) {
            // get myCustomObj created previously
            renderRequest.setAttribute("myCustomObj", myCustomObj);
        }
        super.doView(renderRequest, renderResponse);
    }
}
看一下MVCPortlet中的MVCPortlet.doDispatch(RenderRequest renderRequest,RenderResponse renderResponse)方法 – 在我看来,如果mvcPath或jspPage参数随请求一起发送,那么doDispatch方法完全忽略doView方法并直接路由到JSP页面.

我想在每个portlet渲染上运行doView方法,然后我建议省略这些参数并在doView中自己进行路由.只需调用include(path,renderRequest,renderResponse); doView末尾的方法,path参数是您所需的JSP页面路径.

根据您编辑的问题,您可能想尝试这样的事情:

MyMVCPortlet.java

public class MyMVCPortlet extends MVCPortlet {
    //...        

    private PortletURL createRedirectURL(ActionRequest actionRequest, String jspPage) {
        ThemeDisplay themeDisplay = (ThemeDisplay)         actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
        String portletName = (String) actionRequest.getAttribute(WebKeys.PORTLET_ID);

        PortletURL redirectURL = PortletURLFactoryUtil.create(
            actionRequest, portletName, themeDisplay.getLayout().getPlid(),      PortletRequest.RENDER_PHASE);
        redirectURL.setParameter("viewName", jspPage); // note the changed parameter name

        return redirectURL;
    }

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

        if(/* ... */) {
            // ...
        }
        String requestedView = renderRequest.getParameter("viewName");
        if (StringUtils.isBlank(requestedView)) { // StringUtils comes from commons-lang
            include(viewTemplate, renderRequest, renderResponse); // viewTemplate is a protected variable, which may also be called viewJSP depending on Liferay version I think
        } else {
            include(requestedView, renderRequest, renderResponse);
        }
    }
}
网友评论