[solved] Dynamic number of ‘/’ characters

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

This topic contains 7 replies, has 3 voices, and was last updated by  Lincoln Baxter III 7 years, 2 months ago.

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #17757

    voltman
    Participant

    Hello,

    I know that the answer to my question is in FAQ and in documentation, but i think i’ve tried everything and it still doesn’t work – probably because my knowledge about regular expressions is not enough. My problem is that I’m passing some dynamic number of parameters splited by a delimiter ‘/’, In preety faces 2.x it was easy and I just needed to make a ‘.*’ at the and of a pattern – i.e: /search/.*, and than just go to some function and parse whole URL, by getting it from PrettyContext. In PrettyFaces 3 I don’t know how to do it, how to get whole query with slashes ‘/’ that starts at some point and write it to variable that will be processed. I’m looking for smt like <pattern>search#{ /[w:/]+/ someBean.somevariable}/

    That will accept link like search/q:smth1/p:/smth2/ …. And so on …

    Any help? Many thanks in advance

    Jacob

    #20020

    bomadil
    Participant

    I am searching for the same, i mapped everything beginning with /intranet/

    to a single JSF ViewId with PrettyFaces 1.2.6 using JSF 1.2.

    How is it possible with 3.0.1 ?

    #20021

    Hi guys. It should still work the same way in PrettyFaces 3 – Except the API for accessing the URL has changed.

    URL url = PrettyContext.getCurrentInstance().getRequestURL();
    QueryString query = PrettyContext.getCurrentInstance().getRequestQueryString();

    But voltman, your pattern is wrong :) use something like this instead:

    <pattern>/search/#{ /.*/ someBean.somevariable}/</pattern>

    bomadil, does this answer your question?

    #20022

    voltman
    Participant

    Hi,

    The pattern like you provided:

    <pattern>/search/#{ /.*/ someBean.somevariable}/</pattern>

    was my first shot and it’s doesn’t work on links like:

    search/q:smth1/p:smth2/ – 404 error

    where it still does work for

    search/q:smth1/ – OK

    My frist thought was that it natuarlly breaks at ‘/’ sign as u pointed that in documntation, so i’ve tried different greedy patterns that include the magic slash sign but they did fail aswell. So that is why I’m asking

    #20023

    Interesting… could you send me your configuration so I can try to reproduce? Thanks.

    #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 :)

    #20025

    Congratulations, you found a bug. I’m working on patching it, but am on vacation right now. For the moment, you can do this instead:

    “In pretty faces 2.x it was easy and I just needed to make a ‘.*’ at the and of a pattern – i.e: /search/.*, and than just go to some function and parse whole URL, by getting it from PrettyContext.”

    Do the same thing. Just call different methods from PrettyContext:

    String url = PrettyContext.getCurrentInstance().getRequestURL().getDecodedURL();
    String query = PrettyContext.getCurrentInstance().getRequestQueryString().toString();

    Cheers,

    Lincoln

    #20026

    Ok, this should be fixed in the trunk now. Try the latest 3.0.2-SNAPSHOT (you might have to get it from the OcpSoft repository until it syncs with maven central.)

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.

Comments are closed.