Generic url rewrite

Splash Forums PrettyFaces Users Generic url rewrite

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

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

    BOFFSOMY
    Member

    Hi,

    Reading many tutorials, i didn’t find any way to make rewrite rules like in php.

    Here is the case, I want to transform any url looking like this

    /faces/user.xhtml?user=6 into this user-6.xhtml, but the rewrite rule has to be the same for another url like:

    /faces/news.xhtml?news=6

    To make it clear, i want a generic rule. Pretty Faces can does that?

    Thanks for your help

    #22878

    Sure, just use something like this:

    <rewrite match="^/(w+)-(d+).xhtml$" substitute="/faces/$1.xhtml?$1=$2" redirect="302" />

    See this chapter of the documentation for details:

    http://ocpsoft.org/docs/prettyfaces/3.3.2/en-US/html/inbound_rewriting.html#inbound_rewriting.matching

    You could also have a look at OcpSoft Rewrite, which is the successor of PrettyFaces:

    http://ocpsoft.org/rewrite/

    #22879

    With Rewrite you would use a typesafe approach. Something like:

    addRule( Join.path("/{type}-{id}.xhtml").to("/faces/{type}.xhtml?{type}={id}") )

    #22880

    BOFFSOMY
    Member

    Does it mean I’d better use Rewrite than PrettyFaces?

    #22881

    Well, it really depends on what you need. Rewrite lets you do more specific, low-level tasks, where PrettyFaces really serves the purpose of creating Pretty URLs in JSF, and does not have as much power as Rewrite. If you don’t need to enhance the navigation features of JSF, then you might as well use Rewrite, but if any of the other features of PrettyFaces appeal to you, then you should use that.

    Fortunately, in the near future, PrettyFaces will be released using Rewrite as a core, so you won’t have to choose anymore.

    #22882

    BOFFSOMY
    Member

    Thanks for your time guys! I will use Pretty Faces, because it fits my only need, but I read some pretty nice stuff about Rewrite, but I will check that later: step by step! ;-)

    By the way,I created a rule, but it screwd the insertion of my css, or something else really weird happened.

    I put this piece of code in my web.xml:

    <filter>
    <filter-name>Pretty Filter</filter-name>
    <filter-class>com.ocpsoft.pretty.PrettyFilter</filter-class>
    </filter>
    <filter-mapping>
    <filter-name>Pretty Filter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>REQUEST</dispatcher>
    </filter-mapping>

    What is wrong?

    #22883

    The filter looks good. Could you post the rule you created?

    I’ll always have to make sure that the rules you create don’t match URLs like CSS and images or otherwise those will break.

    #22884

    BOFFSOMY
    Member

    Here it is: <rewrite match="/faces/(w+)" substitute="/$1" />

    #22885

    I think you will have to do it the other way around:

    <rewrite match="/(w+)" substitute="/faces/$1" />

    This will transform requests like /test.xhtml to /faces/test.xhtml.

    But I also think this pattern is a bit to general because /(w+) will also match things like /images.png. Therefore you should use something like:

    <rewrite match="/(w+).xhtml" substitute="/faces/$1.xhtml" />

    I hope this helps. :)

    #22886

    Glad you got it figured out. If you start to have issues with the RegEx in your rule, then I recommend switching to Rewrite and giving that a shot :) http://ocpsoft.org/rewrite/

    Otherwise, you can find more info on regular expressions here if needed (but it seems like you already have a good grasp) – http://ocpsoft.org/opensource/guide-to-regular-expressions-in-java-part-1/

    #22887

    BOFFSOMY
    Member

    I’m using Rewrite because I think I will use some functionnalities in the future.

    I tried that:

    return ConfigurationBuilder.begin()
    .addRule(Join.path"/faces/{url}").to("/{url}"));

    Basically I want to remove “faces” in my url (I think you already guest that but who know :-D)

    #22888

    You can’t use regular expressions directly. You must use parameters, then specify the regex to which each parameter matches.

    As christian showed earlier, use something like this:

    addRule( Join.path("/{page}.xhtml").to("/faces/{page}.xhtml") ).where("page").matches(".*")

    #22889

    BOFFSOMY
    Member

    Is it “/{page}.xhtml”).to(“/faces/{page}.xhtml” or “/faces/{page}.xhtml”).to(“/{page}.xhtml”)?

    #22890

    It’s what I posted.

    #22891

    Are you running in to issues with it?

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

You must be logged in to reply to this topic.

Comments are closed.