How to avoid junk character in URL?

Splash Forums PrettyFaces Users How to avoid junk character in URL?

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

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #18780

    Carolina
    Member

    Hi,

    I am using pretty faces for URL rewriting.

    In my url I have few string parameter. Those string parameter has some space in between.

    In pretty-config.xml I defiened as below.

    <url-mapping id=3D"hotelreview">
    <pattern value=3D"/hotel-detail/#{hotelSearchResultsBean.hotelName}/#{hotelSearchResultsBean.cityName}" />
    <view-id value=3D"/pages/hotel/hotelReview.xhtml" />
    </url-mapping>

    The URL rewriting working perfectly. However the string parameter space taking as %20 in web browser URL.

    How to avoid the %20 in URL.

    For example I need to show as below

    Expected URL: http://localhost:8080/b2c/hotel-detail/Dar Al Ghufran/Makkah

    Current URL: http://localhost:8080/b2c/hotel-detail/Dar%20Al%20Ghufran%20/Makkah

    #23043

    The problem is that many characters (like space characters for example) are not allowed in URLs. So to use these characters you have to escape them. %20 is the escaped version of a space character. So it is absolutely correct to have %20 instead of spaces in an URL.

    Look here for some more detailed explanation for the encoding:

    http://en.wikipedia.org/wiki/Percent-encoding

    If you don’t want such escaped characters in your URL, you will have to prevent that such characters (like spaces) are present in your path parameters. A very simple approach would be to replace spaces by underscores or something like this.

    I hope this helps :)

    Christian

    #23044

    Carolina
    Member

    Christian,

    Yes I got the point. It shows %20 in browser URL, but in java request.getRequestURL() gives original URL.

    Any how i have replaced the space to ‘-‘ character for better view. :)

    #23045

    PrettyFaces 3.x doesn’t support conversion of path parameters. That’s something you get with Rewrite / PrettyFaces 4.0.

    Currently you could simply use special getters and setters. Something like:

    <url-mapping id=3D"hotelreview">
    <pattern value=3D"/hotel-detail/#{hotelSearchResultsBean.prettyHotelName}/#{hotelSearchResultsBean.prettyCityName}" />
    <view-id value=3D"/pages/hotel/hotelReview.xhtml" />
    </url-mapping>

    And the bean:

    public class HotelSearchResultsBean {

    private String hotelName;

    /* regular getters / setters */

    public String getPrettyHotelName() {
    return hotelName.replace(' ', '-');
    }

    public void setPrettyHotelName(String str) {
    this.hotelName = str.replace('-', ' ');
    }

    }

    But please not that this will of cause break if you have both – and space characters in your strings.

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

You must be logged in to reply to this topic.

Comments are closed.