2.10.2. Bookmarkable search results page
The blog example has a tiny form in the top right of each page that allows the user to search for blog entries. This is defined in a file, menu.xhtml
, included by the facelets template, template.xhtml
:
<div id="search">
<h:form>
<h:inputText value="#{searchAction.searchPattern}"/>
<h:commandButton value="Search" action="/search.xhtml"/>
</h:form>
</div>
Example 2.27.
To implement a bookmarkable search results page, we need to perform a browser redirect after processing the search form submission. Because we used the JSF view id as the action outcome, Seam automatically redirects to the view id when the form is submitted. Alternatively, we could have defined a navigation rule like this:
<navigation-rule>
<navigation-case>
<from-outcome>searchResults</from-outcome>
<to-view-id>/search.xhtml</to-view-id>
<redirect/>
</navigation-case>
</navigation-rule>
Example 2.28.
Then the form would have looked like this:
<div id="search">
<h:form>
<h:inputText value="#{searchAction.searchPattern}"/>
<h:commandButton value="Search" action="searchResults"/>
</h:form>
</div>
Example 2.29.
But when we redirect, we need to include the values submitted with the form as request parameters, to get a bookmarkable URL like https://localhost:8080/seam-blog/search.seam?searchPattern=seam
. JSF does not provide an easy way to do this, but Seam does. We use a Seam
page parameter
, defined in WEB-INF/pages.xml
:
<pages>
<page view-id="/search.xhtml">
<param name="searchPattern" value="#{searchService.searchPattern}"/>
</page>
...
</pages>
Example 2.30.
This tells Seam to include the value of #{searchService.searchPattern}
as a request parameter named searchPattern
when redirecting to the page, and then re-apply the value of that parameter to the model before rendering the page.
The redirect takes us to the search.xhtml
page:
<h:dataTable value="#{searchResults}" var="blogEntry">
<h:column>
<div>
<h:outputLink value="entry.seam">
<f:param name="blogEntryId" value="#{blogEntry.id}"/>
#{blogEntry.title}
</h:outputLink>
posted on
<h:outputText value="#{blogEntry.date}">
<f:convertDateTime timeZone="#{blog.timeZone}" locale="#{blog.locale}" type="both"/>
</h:outputText>
</div>
</h:column>
</h:dataTable>
Example 2.31.
Which again uses "pull"-style MVC to retrieve the actual search results:
@Name("searchService")
public class SearchService
{
@In
private EntityManager entityManager;
private String searchPattern;
@Factory("searchResults")
public List<BlogEntry> getSearchResults()
{
if (searchPattern==null)
{
return null;
}
else
{
return entityManager.createQuery("select be from BlogEntry be where lower(be.title)
like :searchPattern or lower(be.body) like :searchPattern
order by be.date desc")
.setParameter( "searchPattern", getSqlSearchPattern() )
.setMaxResults(100)
.getResultList();
}
}
private String getSqlSearchPattern()
{
return searchPattern==null ? "" : '%' +
searchPattern.toLowerCase().replace('*', '%').replace('?', '_')
+ '%';
}
public String getSearchPattern()
{
return searchPattern;
}
public void setSearchPattern(String searchPattern)
{
this.searchPattern = searchPattern;
}
}
Example 2.32.