How do I combine Path params with Query params?

Splash Forums PrettyFaces Users How do I combine Path params with Query params?

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

Viewing 15 posts - 1 through 15 (of 28 total)
  • Author
    Posts
  • #17838

    EugeneV
    Participant

    If I understood correctly, query-param’s are optional and their order does not matter. However, they are matched only in the “query” part of the URL. I’d like to match them like path params though: like /report/#{reportName}/#{reportDate} – yet with reportDate being optional. I do not need EL injection – getting null from request parameter map is good enough. Any suggestions?

    #20489

    EugeneV
    Participant

    I think I figured it out: just add two mappings!

    <url-mapping id="report-date">
    <pattern value="report/#{reportName}/#{reportDate}" />
    <view-id>/index.jsf</view-id>
    </url-mapping>

    <url-mapping id="report">
    <pattern value="report/#{reportName}" />
    <view-id>/index.jsf</view-id>
    </url-mapping>

    #20490

    That’s correct. This is the way I would have done it! :-)

    #20491

    Yep! And in 3.1.1 you’ll be able to do this:

    <url-mapping id="report">
    <pattern value="report/#{reportName}" />
    <view-id>/index.jsf</view-id>
    </url-mapping>

    <url-mapping id="report-date" parentId="report">
    <pattern value="/#{reportDate}" />
    <view-id>/index.jsf</view-id>
    </url-mapping>

    You can try it now if you use the 3.1.1-SNAPSHOT ;)

    #20492

    EugeneV
    Participant

    Hmmm…

    I changed

    <pattern value="report/#{reportName}/#{reportDate}" />

    to

    <pattern value="report/#{reportName}/#{ /d{8}/ reportDate}" />

    as shown in the docs, and PrettyFaces no longer recognizes these URLs (shows the same message I’d get for requesting an unmapped URL of a non-existant page). Any idea what’s going on?

    Glad I tested it before writing a date-validating regex handling leap years and all…

    #20493

    Hmm… I noticed some strange behavior with this myself during a demo. I’ll take a look at it and get back to you asap :)

    #20494

    EugeneV
    Participant

    Hi,

    Any progress?

    #20495

    None yet, sorry — the holidays are killing my productivity :) If you wanted to take a look and see if you can figure out what’s going on, though, that would help a lot! yay opensource!

    Thanks and happy holidays!

    http://code.google.com/p/prettyfaces/issues/detail?id=83

    #20496

    EugeneV
    Participant

    Bump…

    Also, another question: how do I provide default param values? Say, I have something like:

    <url-mapping id="report-date">
    <pattern value="report/#{reportBean.reportName}/#{reportBean.reportDate}" />
    <view-id>/index.jsf</view-id>
    <action>#{reportBean.reload}</action>
    </url-mapping>

    Sure, I can have a second mapping without reportDate, like in my example from a new weeks ago, but if my reportBean is SessionScoped, it will still have reportDate set from a previous request, so then I have to *also* check request params… Can’t I somehow grab params conditionally in a single pattern, and if a param is not present, call its setter with null value?

    #20497

    Regarding your regular expression problem, try:

    <pattern value="report/#{reportName}/#{ /\d{8}/ reportDate}" />

    Meaning you will have to write /\d{8}/ instead of /d{8}/ (escape the ”). See the current snapshot of the documentation for details:

    http://ocpsoft.com/docs/prettyfaces/snapshot/en-US/html/Configuration.html#config.pathparams.regex

    Regarding default path parameter values. That is currently not possible. And I’m note sure if this would make sense. Path parameters are required by their nature. If you want optional values in the URL, you should really use query parameters.

    But if you still want to use path parameters for the report date, you will have to add a second mapping for that. Perhaps you could remove the bean from session scope? Is it really required to hold such a bean in the session scope? Otherwise you could use PrettyContext in your action method to get the “current mapping id” and manually set your report date to null if you detect the mapping without a report date.

    #20498

    EugeneV
    Participant

    Thanks for the regexp!

    I made my bean session-scoped because the page includes an iframe, and both inner and outer pages contain elements from the same bean, which I did not want to create twice per request. It seems that view scope would not help here either.

    What happens if I redirect one url-mapping in pretty-config.xml to another? Like:

    <url-mapping id="default-report">
    <pattern value="/" />
    <view-id>/report/dashboard</view-id>
    </url-mapping>
    <url-mapping id="report">
    <pattern value="report/#{reportBean.reportTag}" />
    <view-id>/index.jsf</view-id>
    <action>#{reportBean.loadReport}</action>
    </url-mapping>

    It appears to work as expected, except I do not see the effect of other filters that were called earlier in the chain. In my case, I open a Hibernate session and begin transaction first, than chain.doFilter calls PrettyFilter, which works fine except for this redirect scenario.

    #20499

    That’s because you are using a servlet FORWARD when setting a view-id to the URL that matches another url-mapping.

    In order for other filters to fire in this scenario, you must ensure that their dispatcher elements in web.xml include the FORWARD dispatcher.

    <filter-mapping>
    <filter-name> [your filter] </filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>ERROR</dispatcher>
    <dispatcher>ASYNCHRONOUS</dispatcher>
    </filter-mapping>

    #20500

    EugeneV
    Participant

    Does PrettyFilter need ASYNCHRONOUS dispatcher as well?

    Never mind, JBoss 5.1 allows only FORWARD, INCLUDE, REQUEST, ERROR

    #20501

    Anly if you are using Asynchronous features – some component libraries do – and it is recommended that you enable Async for PrettyFaces just in case.

    #20502

    EugeneV
    Participant

    But I can’t enable it under Servlet 2.5, can I?

Viewing 15 posts - 1 through 15 (of 28 total)

You must be logged in to reply to this topic.

Comments are closed.