I am building an online application in JSF 2 with Primefaces and Spring. I want to use Pretty Faces to make our search urls bookmarkable.
Here is my pretty-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<pretty-config xmlns="http://ocpsoft.org/prettyfaces/3.3.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://ocpsoft.org/xml/ns/prettyfaces/ocpsoft-pretty-faces-3.3.3.xsd">
<url-mapping id="index">
<pattern value="/" />
<view-id value="/sites/public/template/index.xhtml" />
</url-mapping>
<url-mapping id="searchWithParams">
<pattern value="/search/#{searchView.searchQuery}/#{searchView.searchTags}/#{searchView.searchOrder}" />
<view-id value="/sites/public/product/productSearch.xhtml" />
</url-mapping>
<url-mapping id="searchWithoutParams">
<pattern value="/search" />
<view-id value="/sites/public/product/productSearch.xhtml" />
</url-mapping>
</pretty-config>
Here is my Bean I want to inject in:
@Component
@Scope("request")
public class SearchView {
private String searchQuery = "";
private String searchTags = "";
private String searchOrder = "";
public SearchView() {
// void
}
public void navigate() {
String url = "";
try {
url = "/search" +
"/" + URLEncoder.encode(searchQuery, "UTF-8") +
"/" + URLEncoder.encode(searchTags, "UTF-8") +
"/" + URLEncoder.encode(searchOrder, "UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
url = "/search";
}
try {
FacesContext.getCurrentInstance().getExternalContext().redirect(url);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// Getters and Setters ...
}
Only "/search" works perfectly fine. I also tried changing the order of the mappings in the pretty-config.xml and using dummy values for the other two parameters and only using one parameter. Another thing I tried was to change teh expressions "#{bean.param}" to "#{getParam : bean.param}". Changing the scope of my bean doesn't help either.
On the webpage I use a primefaces commandlink which has "searchView.navigate" as action parameter.
I always get the following error message:
HTTP Status 404 - /search/a/b/c
type Status report
message /search/a/b/c
description The requested resource (/search/a/b/c) is not available.
Definitely having a pattern like /search/#{searchView.searchQuery}/#{searchView.searchTags}/#{searchView.searchOrder} is a bad idea.
Here a quote of my conversation with @Lincon, Prettyfaces team lead :
- Me : And other one, is it possible to add two params in pattern? Like /detail/#{appId}/#{navigationIndex} .
- Lincon : You could certainly create a secondary URL-mapping that would also display the same view-ID, and as long as the parameters are different, eg: One mapping is "/detail" and the other is "/detail/#{appId}", you'll be fine. But if they have the same pattern, you'll have problems.
Personally I don't even use EL expressions into url patterns. I began to use them but they have a big drawback: they force you to specify the url as it is written in the pattern.
For example, having /students/#{studentId} will always force you to specify a student id to reach that page. Otherwise you need to declare a second pattern for the same view-id. Appart from that, a second parameter in the GET request will always force you to use the http standard way to pass the parameters (imagine you want to add a second one, you would need /students/detail/#{studentId}?viewMode=2 , passing them in a different way).
For me is much easier to declare something like /students/detail and after pass the params in the classic (not as pretty) http way: /students/detail?studentId=1&viewMode=2 .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.