Rewriting problem

Splash Forums PrettyFaces Users Rewriting problem

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

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #17886

    robert.m
    Participant

    Hello everybody!

    I have a problem using url rewriting for my current project.

    What i want is that the user can enter a URL like:

    http://www.example.com/welcome

    The URL should internally be mapped to

    /faces/public/welcome.xhtml

    While it does work if i add a url-mapping for each page, that’s definitely not a satisfactory solution.

    I also tried using rewrite. Something like:

    <rewrite match=”/(.*)” substitute=”/faces/public/$1.xhtml” redirect=”chain”/>

    But as you can imagine, this is like an infinite rewrite-loop.

    Does anyone have any suggestions for this case?

    Thank you in advance!

    Best Regards,

    Robert

    #20777

    You probably want to use a negative lookahead to prevent the infinite loop:

    Guide to Regular Expressions in Java (Part 2)

    <rewrite match="/(?!.*\.xhtml.*)(.*)" substitute="/faces/public/$1.xhtml" redirect="chain" outbound="false"/>

    You may or may not need the \. or . depending on your requirements.

    You probably also want another rule set up to fix URLs that point to XHTML files. My example might cause an infinite loop, in which case we are working on updating this regex engine to support “stops” so that when a certain condition has been reached, rewriting is halted. Right now prettyfaces works much better with <url-mappings>, so you should consider using DynaView instead:

    // to fix inbound URLs pointing to XHTML files

    <rewrite match="/faces/public/(.*).xhtml" substitute="/$1" redirect="301"/>

    http://ocpsoft.com/docs/prettyfaces/3.2.0/en-US/html/Configuration.html#config.dynaview

    //using dynaview instead of rewrite:

    <url-mapping id="global">
    <pattern value="/*" />
    <view-id> #{bean.getViewPath} </view-id>
    </url-mapping>

    @Named("bean")
    @RequestScoped
    public class Bean {

    public String getViewPath() {

    URL url = PrettyContext.getCurrentInstance().getRequestURL();
    String viewId = url.toString();
    if(viewId.endsWith(".xhtml") return viewId;
    else return viewId + ".xhtml";

    }
    }

    ~Lincoln

    #20778

    robert.m
    Participant

    Thank you for your answer Lincoln. I’ll try to implement it using dynaview. What i still have to do is provide the Bean with a list of all pages (e.g. “welcome”, “contact”, … I have ~5-6 of them). Otherwise, all requests to resources like javascript or css would be changed to a .xhtml-URL.

    But anyways, injecting a List of Strings into the bean is easier than having to add a <url-mapping> for every page. Especially if the list can be generated using a database.

    Thanks for your advice :)

    #20779

    No problem! Dynaview will intercept whatever is in the pattern, so be careful ;)

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

You must be logged in to reply to this topic.

Comments are closed.