Rewrite spaces in URL

Splash Forums PrettyFaces Users Rewrite spaces in URL

This topic contains 6 replies, has 2 voices, and was last updated by  Christian Kaltepoth 10 years, 3 months ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #18040

    m1m
    Participant

    Hi

    I’m using JSF2 with PrettyFaces 3.3.0. I have list of categories generated automatically. Category name is included as path parameter. It’s very simple pattern, like pattern=/#{name}/. However spaces in url are encoded as %20, so it dosen’t look good.

    http://domain.com/my%20category/

    How can I write pretty rewrite rule to replace spaces to _. I want do this only on “client” side, so in backing bean I still have spaces in #{name}.

    #21452

    Replacing spaces with underscores is possible, but the other way around wont work, because you cannot distinguish whether an underscore is a space or a just an underscore.

    With 3.3.0 you could implement a custom rewrite processor for something like this. See the documentation for details.

    In 4.0.0-SNAPSHOT we support custom JSF converters you can attach to individual path and query parameters. Here you cans simply implement a JSF converter that converts the string to the “url representation” and the other way around.

    #21453

    BTW: Perhaps %20 isn’t “pretty”, but it’s the correct representation of an space character in URLs.

    Another idea. You could simply add another property to your category model object containing some kind of “URL category name”. For example the name lowercased and all spaces replaced by underscores. If you could also make sure that the URL category name is unique, you can easily use it in URL parameters because you can lookup a category by the unique name from the database.

    #21455

    m1m
    Participant

    Thanks, for now I’ll add another property. When 4.0.0 is to be released? Is there any documentation for 4.0.0-SNAPSHOT?

    #21456

    I cannot give any specific date for the 4.0.0 release. And unfortunately there is currently no documentation automatically deployed from the snapshots. Currently the only way to obtain the documentation is to clone the git repository and build it manually.

    Doing the conversion in special getters/setters will definitively work. However if you want a more complete solution you should take a look at the custom rewrite processors.

    #21457

    m1m
    Participant

    Could you post simple example how to use conventers for path parameters?

    This is not working for me:

    @URLMapping(id = "page",
    viewId = "/page.html",
    pattern = "/#{name}-#{id}/",
    converter=@URLConverter(index=0,converterId="testConv"))
    ....
    @FacesConverter("testConv")
    public class TestConv implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
    return value.toUpperCase();
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
    return value.toString().toUpperCase();
    }
    }

    #{name} is not mapped to backing bean, it is only set by f:param inside h:link for seo purposes only

    Should this converter be executed before creating link in html source?

    #21458

    Hey m1m,

    sorry, but converters currently don’t work correctly when using <h:link> and <pretty:link> in 4.0.0-SNAPSHOT. There is still some discussion on how to implement this. See this ticket for details:

    http://code.google.com/p/prettyfaces/issues/detail?id=51

    As I said 4.0.0 is currently in heavy development. So features like this aren’t completely usable at the moment. Sorry! :(

    At the moment I can only suggest to take a look at custom rewrite processors to implement such a conversion like you want it.

    Christian

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

The forum ‘PrettyFaces Users’ is closed to new topics and replies.

Comments are closed.