ocpsoft / rewrite

OCPsoft URL-Rewriting Framework
http://ocpsoft.org/rewrite/
Apache License 2.0
189 stars 86 forks source link

Question: general rule for all jsf / xhtml pages #306

Closed rider87 closed 2 years ago

rider87 commented 2 years ago

Hi,

The forum is not working anymore (only Spam). Therefore I will ask my question here:

Is there a general rule to remove extensions for .jsf and .xhtml ? Omnifaces is doing this, but is not that powerful as Rewrte... I would like to have also a few extra rules...

Example: /portal/mypage.jsf will be than /portal/mypage

How can I do that?

lincolnthree commented 2 years ago

Absolutely:

@Override
   public Configuration getConfiguration(final ServletContext context)
   {
      return ConfigurationBuilder.begin()
               .addRule(Join.path("/{page}").to("/{page}.xhtml"))
               .addRule(Join.path("/{path1}/{page}").to("/{path1}/{page}.xhtml"))
               .addRule(Join.path("/{path1}/{path2}/{page}").to("/{path1}/{path2}/{page}.xhtml"));
 }

Etc.

That should do what you're looking for without being too complicated.

See more docs here: https://github.com/ocpsoft/rewrite/blob/master/documentation/src/main/asciidoc/configuration/mapping.asciidoc

rider87 commented 2 years ago

this means if I have different folders:

I need to configure all like this: @Override public Configuration getConfiguration(final ServletContext context) { return ConfigurationBuilder.begin() .addRule(Join.path("/common/{page}").to("/common/{page}.xhtml")) .addRule(Join.path("/portal/{page}").to("/portal/{page}.xhtml")) .addRule(Join.path("/admin/{page}").to("/admin/{page}.xhtml")) }

It´s not a big deal... I´m just wondering, if there is also a general rule for all xhtml pages, which just remove the .jsf or .xhtml

lincolnthree commented 2 years ago

Not at all. The configuration I posted will do what you are asking, for all /*/*.xhtml pages.

rider87 commented 2 years ago

you mean 1:1 this?

@Override public Configuration getConfiguration(final ServletContext context) { return ConfigurationBuilder.begin() .addRule(Join.path("/{page}").to("/{page}.xhtml")) .addRule(Join.path("/{path1}/{page}").to("/{path1}/{page}.xhtml")) .addRule(Join.path("/{path1}/{path2}/{page}").to("/{path1}/{path2}/{page}.xhtml")); }

lincolnthree commented 2 years ago

Yes. If you really want to do something that matches all pages, and doesn't care about path "depth", this should work as well:

@Override
   public Configuration getConfiguration(final ServletContext context)
   {
      return ConfigurationBuilder.begin()
               .addRule(Join.path("/{*}").to("/{*}.xhtml"));
 }

Just be careful because this rule will also match non xhtml resources and files.

So you may want to combine it with something like this:

@Override
   public Configuration getConfiguration(final ServletContext context)
   {
      return ConfigurationBuilder.begin()
               .addRule(Join.path("/{*}").to("/{*}.xhtml"))
               .when(Resource.exists("/{*}.xhtml));;
 }
lincolnthree commented 2 years ago

Closing this as the question has been answered. Thanks!