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

标签 java jsp jstl liferay portlet

我正在尝试使用 SearchContainer在我的 liferay 应用程序中。目前我必须使用 JSP Scriplet 来设置 <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>

现在,我想将这些 scriplet 更改为 EL。我 found one post关于同样的问题,但那是使用 Spring MVC .而且我不知道在 portlet 中该问题的答案中给出的以下行应该写在哪里:

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

无法在我的 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");
    }
}

我想要那个,每次 Course添加后,它呈现在我的搜索容器中,在我触发 addCourse 的同一页面上行动。

是的,我的 portlet 扩展了 MVCPortlet .


更新:

好的,我想通了一些部分。

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

那么,这里有什么变通办法吗?如何确保我在renderRequest中设置的属性在 doView updateCourse 中提供了方法方法?我知道这听起来不切实际,因为这是一个全新的请求,但还有其他方法吗?

我能想到的一种解决方法是在更大范围内设置属性,例如 sessioncontext而不是 renderRequest .但是,我在其他任何地方都不需要该属性。所以,我认为这不合适。

有什么意见吗?


更新 2:

刚才,我使用了:

actionResponse.setPortletMode(PortletMode.VIEW);

代替:

actionResponse.setRenderParameter("jspPage", jspPage);

它成功了,因为它现在经过了 doView()方法。只是想问一下,这种方法合适吗?当我们将渲染参数设置为同一个 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);
}

最佳答案

正在转换 comments回答:

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

关于java - 在 portlet 中设置 SearchContainer 以在使用 EL 和 JSTL 的 JSP 中使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17923082/

相关文章:

Java 5.0 编译器 API?

java - Spring:将字节数组注入(inject)到ArrayList中

javascript - 动态下拉菜单在 IE11 中不起作用?

java - jsp页面中的ChoiceBox迭代

java - 不能在一个简单的例子上使用 JSTL

java - http ://java. sun.com/jsp/jSTL/core 和 http ://java. sun.com/jSTL/core 之间的区别

java - apache james邮件服务器发送电子邮件

java - 如何返回与使用 Java 6 传入的类类型相同的对象的实例?

java - JSP 条件语句

java - 为什么 ${!blabla.blibli} 在 JSTL 中返回 true 即使 blabla 不存在?