此内容没有您所选择的语言版本。
1.9.2. Bookmarkable search results page
The blog example has a small form at the top right of each page that allows the user to search for blog entries. This is defined in
menu.xhtml
, which is included by the Facelets template template.xhtml
:
To implement a bookmarkable search results page, after the search form submission is processed, we must perform a browser redirect. Because the JSF view ID is used as the action outcome, Seam automatically redirects to the view ID when the form is submitted. We could also have defined a navigation rule as follows:
In that case, the form would have looked like this:
However, to get a bookmarkable URL like
http://localhost:8080/seam-blog/search/
, the values submitted with the form must be included in the URL. There is no easy way to do this with JSF, but with Seam, only two features are required: page parameters and URL rewriting. Both are defined here in WEB-INF/pages.xml
:
The page parameter instructs Seam to link the
searchPattern
request parameter to the value held by #{searchService.searchPattern}
, whenever the search page is requested, and whenever a link to the search page is generated. Seam takes responsibility for maintaining the link between URL state and application state.
The URL for a search on the term
book
would ordinarily be http://localhost:8080/seam-blog/seam/search.xhtml?searchPattern=book
. Seam can simplify this URL by using a rewrite rule. The first rewrite rule, for the pattern /search/{searchPattern}
, states that whenever a URL for search.xhtml contains a searchPattern request parameter, that URL can be compressed into a simplified URL. So, the earlier URL (http://localhost:8080/seam-blog/seam/search.xhtml?searchPattern= book
) can instead be written as http://localhost:8080/seam-blog/search/book
.
As with page parameters, rewriting URLs is bidirectional. This means that Seam forwards requests for the simplified URL to the correct view, and it automatically generates the simplified view — users need not construct URLs. The entire process is handled transparently. The only requirement for rewriting URLs is to enable the rewrite filter in
components.xml
:
<web:rewrite-filter view-mapping="/seam/*" />
<web:rewrite-filter view-mapping="/seam/*" />
The redirect takes us to the
search.xhtml
page:
Again, this uses "pull"-style MVC to retrieve the search results with Hibernate Search.