conditional url-mapping

Splash Forums PrettyFaces Users conditional url-mapping

This topic contains 10 replies, has 3 voices, and was last updated by  m1m 5 years, 8 months ago.

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #18029

    m1m
    Participant

    I have following mappings:

    <url-mapping id="products">
    <pattern value="/products/"/>
    <view-id value="/products.html"/>
    </url-mapping>

    <url-mapping id="productsOrder" parentId="products">
    <pattern value="#{order}/"/>
    <view-id value="/products.html"/>
    </url-mapping>

    <url-mapping id="productsGroup" parentId="productsOrder">
    <pattern value="#{group}/"/>
    <view-id value="/products.html"/>
    </url-mapping>

    <url-mapping id="productsProducer" parentId="productsOrder">
    <pattern value="#{producer}/"/>
    <view-id value="/products.html"/>
    </url-mapping>

    Basically links looks like this:

    domain.com/products/1/groupX/
    domain.com/products/0/producerY/

    Current mapping dosen’t work, because producer and group are strings, so I need to provide a rule when to apply “productsGroup” and “productsProducer” mapping.

    The rule is when #{order} is 1 then should by group, when 0 there is producer.

    Is it possible to write such rule?

    I tried something like

    <url-mapping id="productsProducer" parentId="productsOrder">
    <pattern value="#{producer}/"/>
    <view-id value="/products.html?order=0"/>
    </url-mapping>

    but no success.

    #21407

    I’m not sure if I understand you usecase correctly? What do you mean with:

    Current mapping dosen’t work, because producer and group are strings

    You want two distinct mappings for group and producer? If they only differ by the 0/1 value, why don’t you declare the mapping like this:

    <url-mapping id="products">
    <pattern value="/products/"/>
    <view-id value="/products.html"/>
    </url-mapping>

    <url-mapping id="productsGroup" parentId="products">
    <pattern value="1/#{group}/"/>
    <view-id value="/products.html"/>
    </url-mapping>

    <url-mapping id="productsProducer" parentId="products">
    <pattern value="0/#{producer}/"/>
    <view-id value="/products.html"/>
    </url-mapping>

    You could then add different URL actions to the two mappings that do different things (for example change the ordering).

    Or didn’t I get your usecase right?

    #21408

    m1m
    Participant

    Thanks, but this mapping dosen’t apply to my case since I want also value of #{order} param. I came up with this:

    <url-mapping id="productsGroup" parentId="products">
    <pattern value="#{/1/order}/#{group}/"/>
    <view-id value="/products.html"/>
    </url-mapping>

    <url-mapping id="productsProducer" parentId="products">
    <pattern value="#{/0/order}/#{producer}/"/>
    <view-id value="/products.html"/>
    </url-mapping>

    It works, but the following mapping is not (always choose the first one):

    <url-mapping id="productsGroupAndProducer" parentId="products">
    <pattern value="#{/1/order}/#{group}/#{producer}/"/>
    <view-id value="/products.html"/>
    </url-mapping>

    <url-mapping id="productsProducerAndGroup" parentId="products">
    <pattern value="#{/0/order}/#{producer}/#{group}/"/>
    <view-id value="/products.html"/>
    </url-mapping>

    But it’s not problem anymore, I’ll use explicitly pretty:mappingId in my outcome

    #21409

    m1m
    Participant

    Hmm, pretty:mappingId didn’t solve my problem:

    <url-mapping id="products">
    <pattern value="/products/"/>
    <view-id value="/products.html"/>
    </url-mapping>

    <url-mapping id="productsGroup" parentId="products">
    <pattern value="#{group}/"/>
    <view-id value="/products.html"/>
    </url-mapping>

    <url-mapping id="productsProducer" parentId="products">
    <pattern value="#{producer}/"/>
    <view-id value="/products.html"/>
    </url-mapping>

    I’m using the following link, but it is resolved as productsGroup (group param is set instead of producer)

    <h:link outcome="pretty:productsProducer" value="#{p}">
    <f:param name="producer" value="#{fn:toLowerCase(p)}"/>
    </h:link>

    #21410

    What you are seeing is correct behavior. PrettyFaces has no way to inspect the custom regex you’ve specified in your URLs, so it has no way of telling which mapping to use because they both have the same path-parameter names: ‘order’ ‘producer’ ‘group’

    These two mapping are effectively the same, why do you need two? It does seem like we should be able to handle this, but right now I can’t see a clear way to do so. Something to think about if you want to add an issue requesting it for the future:

    http://code.google.com/p/prettyfaces/issues/list

    #21411

    I agree with Lincoln! Just merge the two mappings into one. This should work fine:

    <url-mapping id="productsGroupAndProducer" parentId="products">
    <pattern value="#{ /[01]/ order }/#{ value }/"/>
    <view-id value="/products.html"/>
    </url-mapping>

    This will capture requests like:

    /products/1/MyGroup/
    /products/0/MyProducer/

    You could then add an action method to the mapping and do whatever you want with the two parameters (order and value). If order is 1 the value will contain a group name and the other way around.

    Christian

    #21412

    m1m
    Participant

    Now I have following mappings

    <url-mapping id="products">
    <pattern value="/products/"/>
    <view-id value="/products.html"/>
    </url-mapping>
    <url-mapping id="productsGroup" parentId="products">
    <pattern value="#{/1/order}/#{group}/"/>
    <view-id value="/products.html"/>
    </url-mapping>
    <url-mapping id="productsProducer" parentId="products">
    <pattern value="#{/0/order}/#{producer}/"/>
    <view-id value="/products.html"/>
    </url-mapping>
    <url-mapping id="productsGroupAndProducer" parentId="productsGroup">
    <pattern value="#{producer}/"/>
    <view-id value="/products.html"/>
    </url-mapping>
    <url-mapping id="productsProducerAndGroup" parentId="productsProducer">
    <pattern value="#{group}/"/>
    <view-id value="/products.html"/>
    </url-mapping>

    And it work’s, but when using two last mappings I must use pretty:productsGroupAndProducer or pretty:productsProducerAndGroup in h:link.

    If I use /products in h:link it always get mapped to productsGroupAndProducer.

    #21413

    Is it a requirement for you to have all the parameters as path parameter? Path parameters don’t fit very well if the parameter has a more optional nature. You could use query parameters for this:

    <url-mapping id=”products”>

    <pattern value=”/products/”/>

    <query-param name=”order”>#{myListBean.order}</query-param>

    <query-param name=”producer”>#{myListBean.producer}</query-param>

    <query-param name=”group”>#{myListBean.group}</query-param>

    <view-id value=”/products.html”/>

    </url-mapping>

    Then you would have URLs like this:

    /products/
    /products/?order=0
    /products/?group=someGroup
    /products/?order=1&group=someGroup
    /products/?order=0&producer=someProducer

    You can use any combination of these parameters you want!

    #21414

    m1m
    Participant

    I already had query parameters, but I switched to prettyfaces for seo purposes, so using prettyfaces with query parameters is not good idea :)

    #21415

    could you post the h:link code that is now giving you trouble?

    #21416

    m1m
    Participant

    I use jsf view id everywhere <h:link outcome=”/products”>, but in two last mappings I must use <h:link outcome=”pretty:productsGroupAndProducer”> as I wrote here http://ocpsoft.com/support/topic/conditional-url-mapping#post-1845

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

You must be logged in to reply to this topic.

Comments are closed.