Install SearchContainer in a portlet for use in JSP using EL and JSTL

I am trying to use SearchContainer in my liferay application. Currently, I have to use JSP Scriplets to set the results in the <liferay-ui:search-container-results> tags. So far this is a snippet:

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

Now I would like to change these scripts to EL. I found one post on the same issue, but uses the Spring MVC . And I have no idea where to write the bottom line indicated in the answer to this question in portlets:

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

In cannot write it to the action of my portlet, because in my action the parameter is ActionRequest and ActionResponse , which does not define the createRenderURL() method. How can I get PortletURL ?

Where can I write the expression above? I am currently writing in the same action from where I am returning to this page. Am I doing it right? Here is the action that I run from the same page as 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"); } } 

I want each time a Course added, it appears in my search container on the same page from where I run the addCourse action.

And yes, my portlet extends MVCPortlet .


UPDATE

Ok, I understood a few parts.

  • The first time the portlet is loaded, I can override the doView method in my portlet, and then add the SearchContainer to renderRequest , since I have access to it in doView .
  • But then again, when I go to editCourse() action, where I do response.setRenderParameter() to send it to another jsp page. And on this JSP page, I run the updateCourse() action.
  • Now, from the updateCourse() action, I again use response.setRenderParameter() to send it to the JSP source page, where I use the Search Container. But now, because it does not pass through the method of doView() , I can not create SearchContainer and add it to the query.

So is there any work here? How to make sure that the attribute set in renderRequest in doView is available in the updateCourse method? I know this does not seem practical, since this is a completely new request, but is there any other way?

One work environment that I think of is setting the attribute in a larger scope, like session or context instead of renderRequest . But I will no longer need this attribute. Therefore, I do not think that would be appropriate.

Any inputs?


Update 2 :

Just now I used:

 actionResponse.setPortletMode(PortletMode.VIEW); 

instead:

 actionResponse.setRenderParameter("jspPage", jspPage); 

And it worked, since now it goes through the doView() method. Just wanted to ask, is this a suitable way? What is the difference between the two methods when setting the render parameter to the same JSP page where the doView methods doView redirected?


My current doView method is as follows:

 @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); } 
+6
source share
1 answer

Conversion Comments :

  • Whenever you render a portlet, it is rendered using the doView method, and not directly through the action methods, as part of the portlet life cycle.
  • the results and total set as renderRequest.setAttribute ("searchResults", courses) and renderRequest.setAttribute ("searchTotal", total) in doView will be available in view.jsp as $ {searchResults} and $ {searchTotal}.
  • Each time you perform an action, doView will be called after that, and searchResults and searchTotal will be set again and displayed.
  • or you can simply set searchContainer in the doView method itself, as described in the answer that you linked in your question.
+3
source

Source: https://habr.com/ru/post/950522/


All Articles