在portlet中设置SearchContainer以使用EL和JSTL在JSP中使用它

Roh*_*ain 6 java jsp portlet jstl liferay

我想SearchContainer在我的liferay应用程序中使用.目前我要使用JSP Scriplets 在标签中设置结果<liferay-ui:search-container-results>.这是到目前为止的片段:

<liferay-ui:search-container emptyResultsMessage="there-are-no-courses" delta="5">
    <liferay-ui:search-container-results>
        <%
            List<Course> tempResults = ActionUtil.getCourses(renderRequest);

            results = ListUtil.subList(tempResults, 
                                   searchContainer.getStart(), 
                                   searchContainer.getEnd());

            total = tempResults.size();
            pageContext.setAttribute("results", results);
            pageContext.setAttribute("total", total);
        %>
    </liferay-ui:search-container-results>

    <liferay-ui:search-container-row ...></liferay-ui:search-container-row>

    <liferay-ui:search-iterator />

</liferay-ui:search-container>
Run Code Online (Sandbox Code Playgroud)

现在,我想将这些划线改为EL.我发现了一篇关于同一问题的帖子,但那是在使用Spring MVC.而且我不知道在portlet中,在该问题的答案中给出了下面这一行:

SearchContainer<Book> searchContainer = new SearchContainer<Book>(renderRequest, renderResponse.createRenderURL(), null, "there are no books");
Run Code Online (Sandbox Code Playgroud)

在我的portlet操作中无法写入它,因为我的操作中的参数是ActionRequestActionResponse,它没有定义方法createRenderURL().我怎么会得到的PortletURL

我应该在哪里写上述声明?目前我正在写同一个动作,我将返回此页面.我做得对吗?这是我从同一页面触发的动作,如下所示search-container:

public void addCourse(ActionRequest request, ActionResponse response) 
        throws Exception {

    ThemeDisplay themeDisplay = 
            (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

    Course course = ActionUtil.courseFromRequest(request);

    List<String> errors = new ArrayList<String>();

    if (CourseRegValidator.validateCourse(course, errors)) {
        CourseLocalServiceUtil.addCourse(course, themeDisplay.getUserId());
        SessionMessages.add(request, "course-added-successfully");

        // I thought I might put it here.
        // But I don't know what to pass as `PortletURL` in constructor of SearchContainer
    } else {
        SessionErrors.add(request, "fields-required");
    }
}
Run Code Online (Sandbox Code Playgroud)

我想要的是,每次Course添加a时,它都会在我的搜索容器中呈现,在我触发addCourse操作的同一页面上.

是的,我的portlet扩展了MVCPortlet.


更新:

好的,我想了几个部分.

  • 第一次加载portlet时,我可以覆盖doView我的portlet中的方法,然后SearchContainerrenderRequest那里添加,因为我可以访问它doView.
  • 但是,当我继续editCourse()行动,我正在做的时候response.setRenderParameter(),将它发送到另一个jsp页面.在那个JSP页面中,我正在触发一个updateCourse()动作.
  • 现在,从updateCourse()动作开始,我再次使用response.setRenderParameter()它将其发送到原始JSP页面,我正在使用搜索容器.但是现在,由于它不是通过doView()方法,我无法创建SearchContainer并添加它来请求.

那么,这里有什么解决方法吗?如何确保我renderRequestdoView方法中设置的属性在方法中可用updateCourse?我知道这听起来不太实际,因为这完全是一个新的要求,但还有其他方法吗?

我能想到的一个解决方法是将属性设置在更大的范围内,比如sessioncontext代替renderRequest.但是,我不会在任何其他地方需要该属性.所以,我认为这不合适.

有什么投入?


更新2:

刚才,我用过:

actionResponse.setPortletMode(PortletMode.VIEW);
Run Code Online (Sandbox Code Playgroud)

取代:

actionResponse.setRenderParameter("jspPage", jspPage);
Run Code Online (Sandbox Code Playgroud)

它起作用,因为它现在通过doView()方法.只是想问,这是适当的方式吗?当我们将render参数设置到同一个JSP页面时,两个方法之间有什么区别,其中doView方法重定向?


我当前的doView方法如下:

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

    SearchContainer<Course> searchContainer = 
            new SearchContainer<Course>(renderRequest, renderResponse.createRenderURL(), null, "there-are-no-courses");

    searchContainer.setDelta(5);
    List<Course> tempResults = ActionUtil.getCourses(renderRequest);

    List<Course> results = ListUtil.subList(tempResults, 
                                    searchContainer.getStart(), 
                                    searchContainer.getEnd());

    searchContainer.setTotal(tempResults.size());
    searchContainer.setResults(results);

    renderRequest.setAttribute("searchContainer", searchContainer);
    super.doView(renderRequest, renderResponse);
}
Run Code Online (Sandbox Code Playgroud)

Pra*_*h K 3

评论转换为答案:

  1. 每当您呈现 portlet 时,它都会通过 doView 方法呈现,而不是直接通过作为 portlet 生命周期一部分的操作方法呈现。
  2. doView 中设置为 renderRequest.setAttribute("searchResults", course) 和 renderRequest.setAttribute("searchTotal", Total) 的结果和总数将在 view.jsp 中以 ${searchResults} 和 ${searchTotal} 形式提供。
  3. 每次执行任何操作时,都会调用 doView,并且 searchResults 和 searchTotal 将再次设置并显示。
  4. 或者您可以按照问题中链接的答案searchContainer中的说明在 doView 方法本身中设置。