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 2 years, 6 months ago.

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

    mfe_
    Participant

    Greetings,

    my pretty-config.xml looks like that:

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

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

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

    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"
    "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
    <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"/>
    </glassfish-web-app>

    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 2 years, 7 months ago by  mfe_.
    #25737

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

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

    Does this change anything?

    #25738

    mfe_
    Participant

    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 2 years, 7 months ago by  mfe_.
    • This reply was modified 2 years, 7 months ago by  mfe_.
    #25741

    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.

    #25742

    mfe_
    Participant

    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}" />
                                        bearbeiten
                                    </h:link>

    produces the hyperlink,

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

    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?

    #25743

    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:

    Schwarzw%C3%A4lder-Kirsch-Muffins

    Which won’t match against the regular expression:

    [a-zA-Z ]*

    Try this configuration instead:

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

    #25745

    mfe_
    Participant

    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?

    #25748

    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" />
    </url-mapping>
    

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

    #25749

    mfe_
    Participant

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

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

    • This reply was modified 2 years, 6 months ago by  mfe_.
    • This reply was modified 2 years, 6 months ago by  mfe_.
    #25751

    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" />

    #25752

    mfe_
    Participant

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

    #25753

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

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

    #25754

    mfe_
    Participant

    Does this also work?

    No – Results in a 404 error.

    #25756

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

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

You must be logged in to reply to this topic.

Comments are closed.