URL Encoding – how does it work?

Splash Forums PrettyFaces Users URL Encoding – how does it work?

This topic contains 13 replies, has 2 voices, and was last updated by  Christian Kaltepoth 8 years ago.

Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
  • #25735



    my pretty-config.xml looks like that:

        <url-mapping id="viewRecipe">
            <pattern value="/#{ /([a-zA-Z ]*)/ recipe }.html" />
            <view-id value="/viewRecipe.jsf" />

    and I catch the parameter like that in the viewRecipe.jsf

                <f:viewParam name="recipe" value="#{RecipeBean.recipe}" converter="#{recipeStringConverter}" required="true" maxlength="150"  />

    I also created an glassfish-web.xml with the following content:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN"
    <glassfish-web-app error-url="">
        <class-loader delegate="true"/>
        <!-- Change the default character encoding from ISO-8859-1 to UTF-8 -->
        <parameter-encoding default-charset="UTF-8"/>

    I set the encoding in the template with <?xml version='1.0' encoding='UTF-8' ?> and <meta http-equiv="content-type" content="text/html; charset=utf-8"/> . The <h:outputLink value="#{r.name}.html">bearbeiten</h:outputLink> (r.name=Schwarzwälder-Kirsch-Muffins) will be transformed to http://localhost:8080/wie-koche-ich/Schwarzw%E4lder-Kirsch-Muffins.html

    Have I to adjust my url rewriting or is there an other trick to fix this?

    • This topic was modified 8 years ago by  mfe_.

    Try to create the link by using h:link instead.

    <h:link outcome="/viewRecipe.jsf">
      <f:param name="recipe" value="#{r.name}" />

    Does this change anything?



    When using your Code it will generate the following link http://localhost:8080/wie-koche-ich/edit/recipe/Schwarzwälder-Kirsch-Muffins.html

    But the webserver responds with a HTTP Status 404 – Not Found Error.
    There is no output on the Glassfish Server, when getting the 404 Error to determine whats wrong.

    • This reply was modified 8 years ago by  mfe_.
    • This reply was modified 8 years ago by  mfe_.

    So the link contains %C3%A4 instead of ä in the URL? This would be correct. That’s the correct escape sequence for the German umlaut ä when using UTF8.

    You have to double check the formatting of the URL using the developer tools of your browser, because some browsers (like chrome) won’t display %C3%A4 in the address bar although that is the URL that is used when requesting the resource from the server.



    I’m a litte bit confused because it seems to be all correct. The following code

                                   <h:link outcome="/editRecipe.jsf">
                                        <f:param name="recipe" value="#{r.name}" />

    produces the hyperlink,

    <a href="/wie-koche-ich/edit/recipe/Schwarzw%C3%A4lder-Kirsch-Muffins.html">

    When clicking on the link the Webdeveloper Console / Network displays the following:
    Request URL: http://localhost:8080/wie-koche-ich/edit/recipe/Schwarzw%C3%A4lder-Kirsch-Muffins.html
    Request Method: GET
    Status Code: HTTP/1.1 404 Not Found

    So the link and the encoding is ok (I guess), but the page will not be found. Is there a way to hook into this, and find out why there is no proper page?

    Do I need to adjust my Rewrite Rule that äöü will be transformed to %C3%A4 and so on?


    The rendered link is correct. The problem seems to be the regular expression that you are using for the parameter. The value of the parameter as submitted by the browser is:


    Which won’t match against the regular expression:

    [a-zA-Z ]*

    Try this configuration instead:

    <pattern value="/#{ /[%a-zA-Z0-9\-]+)/ recipe }.html" />



    Thanks for your hint, but I still get a 404 Error when calling:

    [10:26:49.322] GET http://localhost:8080/wie-koche-ich/Schwarzw%C3%A4lder-Kirsch-Muffins.html [HTTP/1.1 404 Not Found 67ms]
    [10:26:49.306] The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must be declared in the document or in the transfer protocol. @ http://localhost:8080/wie-koche-ich/Schwarzw%C3%A4lder-Kirsch-Muffins.html

    The “The character encoding of the HTML document was not declared” is regarding the 404 Page, so this can be ignored.

    I also tried some other regular expressions on http://ocpsoft.org/tutorials/regular-expressions/java-visual-regex-tester/ and they seems to work (e.g ([%a-zA-Z0-9-]*) – http://localhost:8080/wie-koche-ich/Schwarzw%C3%A4lder-Kirsch-Muffins.html )

    What can I try else?


    Does it work if you completely remove the regular expression from the parameter?

    <url-mapping id="viewRecipe">
        <pattern value="/#{recipe}.html" />
        <view-id value="/viewRecipe.jsf" />

    Also, is this this only PrettyFaces mapping you have in your app? Or do you have other mappings which work?



    Thanks for the suggestion. When removing the regular expression the page
    http://localhost:8080/wie-koche-ich/Schwarzw%C3%A4lder-Kirsch-Muffins.html will be loaded without any problems.

    My complete pretty-config.xml looks like this:

      <url-mapping id="index">
            <pattern value="/" />
            <view-id value="/index.jsf" />
    <url-mapping id="viewRecipe">
        <pattern value="/#{recipe}.html" />
        <view-id value="/viewRecipe.jsf" />
        <url-mapping id="viewIngredient">
            <pattern value="/ingredient/#{ /([a-zA-Z ]*)/ ingredient }.html" />
            <view-id value="/viewIngredient.jsf" />

    So it seems the regular expression is not matching with the decoded öüä-.

    • This reply was modified 8 years ago by  mfe_.
    • This reply was modified 8 years ago by  mfe_.

    Hmmm. I really think that PrettyFaces matches against the _encoded_ version of the URL. Could you give these two versions a try:

    <pattern value="/#{ /[%a-zA-Z0-9-]+)/ recipe }.html" />

    <pattern value="/#{ /[%a-zA-Z0-9\\-]+)/ recipe }.html" />

    You could also try to add German umlauts to the regular expression. But I don’t think this will help:

    <pattern value="/#{ /[%a-zA-ZäüöÄÖÜß0-9\\-]+)/ recipe }.html" />



    Thanks a lot Christian! <pattern value="/#{ /([%a-zA-ZäüöÄÖÜß0-9\\-]+)/ recipe }.html" /> works for me. 🙂


    Great! I’m curious. Does this also work?

    <pattern value="/#{ /([%a-zA-Z0-9\\-]+)/ recipe }.html" />



    Does this also work?

    No – Results in a 404 error.


    Hmmm. Weird. However, I’m happy to hear that the other pattern works for you.

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

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

Comments are closed.