title: Setting SearchContainer in portlet to use it in JSP using EL and JSTL
body: <p>I am trying to use <code>SearchContainer</code> in my liferay application. Currently I've to use JSP Scriplets to set the <em>results</em> in <code>&lt;liferay-ui:search-container-results&gt;</code> tags. This is the snippet so far:</p>
  
  <pre><code>&lt;liferay-ui:search-container emptyResultsMessage="there-are-no-courses" delta="5"&gt;
      &lt;liferay-ui:search-container-results&gt;
          &lt;%
              List&lt;Course&gt; tempResults = ActionUtil.getCourses(renderRequest);
  
              results = ListUtil.subList(tempResults, 
                                     searchContainer.getStart(), 
                                     searchContainer.getEnd());
  
              total = tempResults.size();
              pageContext.setAttribute("results", results);
              pageContext.setAttribute("total", total);
          %&gt;
      &lt;/liferay-ui:search-container-results&gt;
  
      &lt;liferay-ui:search-container-row ...&gt;&lt;/liferay-ui:search-container-row&gt;
  
      &lt;liferay-ui:search-iterator /&gt;
  
  &lt;/liferay-ui:search-container&gt;
  </code></pre>
  
  <p>Now, I would like to change those scriplets to EL. I <a href="https://stackoverflow.com/q/12069927/1679863">found one post</a> regarding the same issue, but that is using <code>Spring MVC</code>. And I've no idea where to write the below line as given in the answer to that question, in portlets:</p>
  
  <pre><code>SearchContainer&lt;Book&gt; searchContainer = new SearchContainer&lt;Book&gt;(renderRequest, renderResponse.createRenderURL(), null, "there are no books");
  </code></pre>
  
  <p>In can't write it in my portlet action, as the parameter in my action is <code>ActionRequest</code> and <code>ActionResponse</code>, which does not define the method <code>createRenderURL()</code>. How would I get the <code>PortletURL</code>?</p>
  
  <p>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 <code>search-container</code> is in:</p>
  
  <pre><code>public void addCourse(ActionRequest request, ActionResponse response) 
          throws Exception {
  
      ThemeDisplay themeDisplay = 
              (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
  
      Course course = ActionUtil.courseFromRequest(request);
  
      List&lt;String&gt; errors = new ArrayList&lt;String&gt;();
  
      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");
      }
  }
  </code></pre>
  
  <p>I want that, everytime a <code>Course</code> is added, it is rendered in my search container, on the same page from where I'm firing the <code>addCourse</code> action.</p>
  
  <p>And yes, my portlet extends <code>MVCPortlet</code>.</p>
  
  <hr />
  
  <p><strong>UPDATE</strong>:</p>
  
  <p>Ok, I figured out a few part. </p>
  
  <ul>
  <li>For the first time when portlet is loaded, I can override the <code>doView</code> method in my portlet, and then add the <code>SearchContainer</code> in <code>renderRequest</code> there, as I've access to it in <code>doView</code>. </li>
  <li>But again, when I go on to <code>editCourse()</code> action, where I'am doing a <code>response.setRenderParameter()</code>, to send it to another jsp page. And in that JSP page, I'm firing an <code>updateCourse()</code> action.</li>
  <li>Now, from <code>updateCourse()</code> action, I'm again using <code>response.setRenderParameter()</code> to send it to the original JSP page, where I'm using <em>Search Container</em>. But now, since it is not going through <code>doView()</code> method, I can't create the <code>SearchContainer</code> and add it to request.</li>
  </ul>
  
  <p>So, is there any work-around here? How to make sure that the attribute I set in <code>renderRequest</code> in <code>doView</code> method is available in <code>updateCourse</code> method? I know that doesn't sound practical, as it is completely a new request, but is there any other way?</p>
  
  <p>One work-around I can think of is to set the attribute in larger scope, like <code>session</code> or <code>context</code> instead of <code>renderRequest</code>. But, I won't need that attribute anywhere else. So, I don't think that would be appropriate.</p>
  
  <p>Any inputs?</p>
  
  <hr />
  
  <p><strong>Update 2</strong>:</p>
  
  <p>Just now, I used:</p>
  
  <pre><code>actionResponse.setPortletMode(PortletMode.VIEW);
  </code></pre>
  
  <p>in place of:</p>
  
  <pre><code>actionResponse.setRenderParameter("jspPage", jspPage);
  </code></pre>
  
  <p>And it worked, as it now goes through <code>doView()</code> 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 <code>doView</code> methods redirects?</p>
  
  <hr />
  
  <p>My Current <code>doView</code> method looks like:</p>
  
  <pre><code>@Override
  public void doView(RenderRequest renderRequest, RenderResponse renderResponse) 
          throws IOException, PortletException {
  
      SearchContainer&lt;Course&gt; searchContainer = 
              new SearchContainer&lt;Course&gt;(renderRequest, renderResponse.createRenderURL(), null, "there-are-no-courses");
  
      searchContainer.setDelta(5);
      List&lt;Course&gt; tempResults = ActionUtil.getCourses(renderRequest);
  
      List&lt;Course&gt; results = ListUtil.subList(tempResults, 
                                      searchContainer.getStart(), 
                                      searchContainer.getEnd());
  
      searchContainer.setTotal(tempResults.size());
      searchContainer.setResults(results);
  
      renderRequest.setAttribute("searchContainer", searchContainer);
      super.doView(renderRequest, renderResponse);
  }
  </code></pre>