在portlet中设置SearchContainer以使用EL和JSTL在JSP中使用它 [英] Setting SearchContainer in portlet to use it in JSP using EL and JSTL

查看:159
本文介绍了在portlet中设置SearchContainer以使用EL和JSTL在JSP中使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的liferay应用程序中使用 SearchContainer 。目前,我将使用JSP Scriplets在< liferay-ui:search-container-results> 标记中设置结果。这是到目前为止的代码段:

I am trying to use SearchContainer in my liferay application. Currently I've to use JSP Scriplets to set the results in <liferay-ui:search-container-results> tags. This is the snippet so far:

<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>

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

Now, I would like to change those scriplets to EL. I found one post regarding the same issue, but that is using Spring MVC. And I've no idea where to write the below line as given in the answer to that question, in portlets:

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

In无法在我的portlet操作中写入,因为我的操作中的参数是 ActionRequest ActionResponse ,它没有定义方法 createRenderURL()。我如何获得 PortletURL

In can't write it in my portlet action, as the parameter in my action is ActionRequest and ActionResponse, which does not define the method createRenderURL(). How would I get the PortletURL?

我应该在哪里写上述声明?目前我正在写回到此页面的相同操作。我做得对吗?这是我从同一页面开始的操作,因为搜索容器位于:

Where should I write the above statement? Currently I'm writing in the same action from where I'm returning to this page. Am I doing it right? Here's the action that I'm firing from the same page, as the search-container is in:

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");
    }
}

我想要的,每次课程已添加,它在我的搜索容器中呈现,位于我触发 addCourse 操作的同一页面上。

I want that, everytime a Course is added, it is rendered in my search container, on the same page from where I'm firing the addCourse action.

是的,我的portlet扩展 MVCPortlet

And yes, my portlet extends MVCPortlet.

更新

好的,我想了几个部分。

Ok, I figured out a few part.


  • 第一次加载portlet时,我可以覆盖 doView 方法在我的portlet中,然后在 renderRequest 中添加 SearchContainer ,因为我可以在<$ c中访问它$ C>的doView 。

  • 但是,当我继续 editCourse()行动时,我正在做回复.setRenderParameter(),将其发送到另一个jsp页面。在那个JSP页面中,我正在触发 updateCourse()操作。

  • 现在,来自 updateCourse () action,我再次使用 response.setRenderParameter()将其发送到我正在使用搜索容器。但现在,因为它没有通过 doView()方法,我无法创建 SearchContainer 并添加它要求。

  • For the first time when portlet is loaded, I can override the doView method in my portlet, and then add the SearchContainer in renderRequest there, as I've access to it in doView.
  • But again, when I go on to editCourse() action, where I'am doing a response.setRenderParameter(), to send it to another jsp page. And in that JSP page, I'm firing an updateCourse() action.
  • Now, from updateCourse() action, I'm again using response.setRenderParameter() to send it to the original JSP page, where I'm using Search Container. But now, since it is not going through doView() method, I can't create the SearchContainer and add it to request.

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

So, is there any work-around here? How to make sure that the attribute I set in renderRequest in doView method is available in updateCourse method? I know that doesn't sound practical, as it is completely a new request, but is there any other way?

我能想到的一个解决方法是设置较大范围内的属性,如 session context 而不是 renderRequest 。但是,我不会在任何其他地方需要该属性。所以,我认为这不合适。

One work-around I can think of is to set the attribute in larger scope, like session or context instead of renderRequest. But, I won't need that attribute anywhere else. So, I don't think that would be appropriate.

任何输入?

更新2

刚才,我用过:

actionResponse.setPortletMode(PortletMode.VIEW);

代替:

actionResponse.setRenderParameter("jspPage", jspPage);

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

And it worked, as it now goes through doView() method. Just wanted to ask, is this the appropriate way? What's the difference between two methods when we are setting render parameter to the same JSP page, where doView methods redirects?

我的当前 doView 方法如下所示:

My Current doView method looks like:

@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);
}


推荐答案

转换评论回答:


  1. 每当渲染一个portlet时,它都是通过doView方法呈现的,而不是直接通过动作方法作为portlet生命周期的一部分。

  2. 结果和总设置为renderRequest.setAttribute(searchResults,courses)和renderRequest.setAttribute(searchTotal,total)in doView将在view.jsp中作为$ {searchResults}和$ {searchTotal}提供。

  3. 每次执行任何操作时都会调用doView,searchResults和searchTotal将会是再次设置并将显示。

  4. 或者您可以在doView方法本身中设置 searchContainer ,如回答您已在问题中链接过。

  1. Whenever you render a portlet it is rendered through the doView method and not directly through the action methods as part of the portlet life-cycle.
  2. the results and total set as renderRequest.setAttribute("searchResults", courses) and renderRequest.setAttribute("searchTotal", total) in doView will be available in the view.jsp as ${searchResults} and ${searchTotal}.
  3. Everytime you perform any action the doView will be called after that and the searchResults and searchTotal will be set again and will be shown.
  4. or you can just set the searchContainer in the doView method itself as explained in the answer which you had linked in your question.

这篇关于在portlet中设置SearchContainer以使用EL和JSTL在JSP中使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆