Map part of URL to Parameter

Splash Forums Rewrite Users Map part of URL to Parameter

Tagged: 

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

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #23814

    naska.om
    Participant

    Using Rewirte how do I make URLs like this: http://myshop.at/product/123
    out of: http://myshop.at/view_product_details.xhtml?id=123

    I have already set view params.

    • This topic was modified 3 years, 2 months ago by  naska.om. Reason: added the library I wanted to do the job with, i.e. rewrite and not prettyfaces
    #23816

    You can use a Join for this. Just add the following rule to your configuration provider:

    
    .addRule(Join.path("/product/{id}").to("/view_product_details.xhtml"))
    
    #23817

    naska.om
    Participant

    Thanks Christian. It made the URL nice but destroyed my styles. It seems the page doesn’t read my styles anymore. What’s wrong?

    #23818

    naska.om
    Participant

    Thanks again. I solved it by adding .withInboundCorrection())
    .when(DispatchType.isRequest())
    .where("products/{id}").matches("[^/.]+");

    #23819

    naska.om
    Participant

    Oops.It didn’t.

    #23820

    naska.om
    Participant

    In addition to this, when I add rule .addRule(Join.path("/{Home}").to("/{Home}.xhtml")
    .withInboundCorrection())
    .when(DispatchType.isRequest())
    .where("Home").matches("[^/.]+");
    }
    the link <h:commandLink id="viewProduct" action="#{productController.viewProduct()}">
    <h:outputText value="#{item.title}"/>
    <f:setPropertyActionListener target="#{productController.id}" value="#{item.id}"/>
    </h:commandLink>
    does not work. It just reloads the page.

    #23825

    You have to make sure that your patterns don’t match other resources. Generally it is a good idea to restrict the regex of the parameters as you did it. In case of your product page it would be a good idea to restrict the parameter to numbers:

    
    .addRule(Join.path("/product/{id}").to("/view_product_details.xhtml"))
    .where("id").matches("[0-9]+")
    

    If you have problems with broken graphics or styles, please post the exact URL and all your rules.

    For the home page it seems like you actually don’t use a parameter. Just use a string literal:

    
    .addRule(Join.path("/Home").to("/Home.xhtml")
    

    And I think you don’t need the .when(DispatchType.isRequest()) part.

    #23835

    naska.om
    Participant

    I solved the styles problem by adding #{request.contextPath} to the links. And for The commandLink by `.addRule()
    .when(Path.matches(“/products/{id}”))
    .perform(Redirect.temporary(context.getContextPath() + “/products#”))` Thanks, your point to add a check for id is a good idea and that I don’t need to set the DispatchType.isRequest(). I’m quite realizing the work of rewrite. Thanks again.

    #23843

    Yep! You have to manually add the context path to the Request 🙂 We can’t assume that because maybe you want to redirect to another application! Glad you got it working.

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

You must be logged in to reply to this topic.

Comments are closed.