Re: [solved] Dynamic number of ‘/’ characters

Splash Forums PrettyFaces Users [solved] Dynamic number of ‘/’ characters Re: [solved] Dynamic number of ‘/’ characters

#20024

voltman
Participant

Yea sure… this is part of pretty-config.xml:

<!--       <url-mapping id="paramSearch0">
<pattern>/search/s/#{ /.*/ searchParamBean.searchQuery}/</pattern>
<view-id>#{searchParamBean.parseURL}</view-id>
</url-mapping>-->

<url-mapping id="paramSearch1">
<pattern>/search/s/#{searchParamBean.searchQuery}/</pattern>
<view-id>#{searchParamBean.parseURL}</view-id>
</url-mapping>

<url-mapping id="paramSearch2">
<pattern>/search/s/#{searchParamBean.searchQuery}/#{searchParamBean.searchQuery}/</pattern>
<view-id>#{searchParamBean.parseURL}</view-id>
</url-mapping>

<url-mapping id="paramSearch3">
<pattern>/search/s/#{searchParamBean.searchQuery}/#{searchParamBean.searchQuery}/#{searchParamBean.searchQuery}/</pattern>
<view-id>#{searchParamBean.parseURL}</view-id>
</url-mapping>

<url-mapping id="paramSearch4">
<pattern>/search/s/#{query: searchParamBean.searchQuery}/#{city: searchParamBean.searchQuery}/#{employer: searchParamBean.searchQuery}/#{region: searchParamBean.searchQuery}/</pattern>
<view-id>#{searchParamBean.parseURL}</view-id>
</url-mapping>

<url-mapping id="paramSearch5">
<pattern>/search/s/#{query: searchParamBean.searchQuery}/#{ city: searchParamBean.searchQuery}/#{employer: searchParamBean.searchQuery}/#{region: searchParamBean.searchQuery}/#{page: searchParamBean.searchQuery}/</pattern>
<view-id>#{searchParamBean.parseURL}</view-id>
</url-mapping>

– yes i know that this solution is nasty, but this is working for me now, the best way would be to use commented solution at the top marked with id “paramSearch0”.

SearchParamBean is class made just for purpose of parsing url, geting search params and passing them to appropriete beans. Normally using pattern like <pattern>/search/s/#{ /.*/ searchParamBean.searchQuery}/</pattern> I would get all the params easily in one variable. Since this doesnt work form me I have to do the trick with setter and getter methods for variable searchQuery

SearchParamBean class methods:

public String getSearchQuery() {
return searchQuery;
}

public void setSearchQuery(String searchQuery) {
if(searchQuery.equalsIgnoreCase(""))
this.searchQuery = searchQuery;
else
this.searchQuery += ";" + searchQuery;
}

public String parseURL(){
String returnURL = null, orgURL = null;
Map<String, String> paramMap = new HashMap<String, String>();
orgURL = PrettyContext.getCurrentInstance().getContextPath();

String [] paramArray = null, tParamArray = null, ttParamArray = null;

paramArray = searchQuery.split(";");

for(String param: paramArray){
tParamArray = param.split("/");
for(String ttParam : tParamArray){
ttParamArray = ttParam.split(":");
if(ttParamArray != null && ttParamArray.length > 1)
paramMap.put(ttParamArray[0], ttParamArray[1]);
}
}

ELContext elContext = FacesContext.getCurrentInstance().getELContext()
SearchBean searchBean = (SearchBean) FacesContext.getCurrentInstance().getApplication().getELResolver().getValue(elContext, null, "searchBean");

CityBean cityBean = (CityBean)FacesContext.getCurrentInstance().getApplication().getELResolver().getValue(elContext, null, "cityBean");

if(paramMap.containsKey("q"))
searchBean.setSearchQuery(paramMap.get("q"));
if(paramMap.containsKey("c"))
searchBean.setSearchCity(paramMap.get("c"));
if(paramMap.containsKey("e"))
searchBean.setSearchEmployer(paramMap.get("e"));
if(paramMap.containsKey("r"))
searchBean.setSearchRegion(paramMap.get("r"));
if(paramMap.containsKey("p"))
searchBean.setPageNumber(Integer.parseInt(paramMap.get("p")));
searchBean.getSearchData();

returnURL= "/faces/pages/search.xhtml";
return returnURL;
}

At the end application should accept links like ../search/s/q:something1/c:something2/e:something2/r:something3/p:0/

If you need anythink else please tell :)