Query Parameters displayed in URL

Splash Forums PrettyFaces Users Query Parameters displayed in URL

This topic contains 6 replies, has 3 voices, and was last updated by  Lincoln Baxter III 6 years, 6 months ago.

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

    kovercash
    Participant

    Hello,

    I have just started using PrettyFaces and have been quite impressed with its capabilities.

    I am having a problems with using query parameters. I have put together a simple app to test PrettyFaces functionality with the goal of implementing SEO friendly URLs.

    The app passes category IDs as query parameters and I would like to be able to display the name of the category in the URL instead of the IDs as query parameters. (e.g. instead of store/category.xhtml?categoryID=2 show store/Rice).

    page1.xhtml

    <h:link value=”Page2″ outcome=”page2″>

    <f:param name=”iid” value=”2″/>

    </h:link>

    page2.xhtml

    <h:outputText value=”passed id – #{page2Backing.iid}”/>

    BackingBean

    @ManagedBean

    @RequestScoped

    public class Page2Backing {

    private Integer iid;

    public Integer getIid() {

    return iid;

    }

    public void setIid(Integer iid) {

    this.iid = iid;

    }

    }

    pretty-config.xml

    <url-mapping id=”page2″>

    <pattern value=”/TheSecondPage/” />

    <view-id>/faces/page2.xhtml</view-id>

    <query-param name=”iid”>#{page2Backing.iid}</query-param>

    </url-mapping>

    When I use the above code, the iid property is populated with the id but the URL still displays the query param ?iid=2 after the rewritten /TheSecondPage/. How can I stop this from being displayed?

    Secondly for the goal described above, it is best to pass the name of the category name as an additional parameter to be displayed in the URL or can a look up be configured to retrieve the name from the passed ID?

    Thank you

    Kevin

    #20847

    This sounds like a perfect example for path parameters and page actions:

    <url-mapping id="store">
    <pattern value="/store/#{storeBean.categoryName}" />
    <view-id>/faces/store.xhtml</view-id>
    <action>#{storeBean.onLoad}</action>
    </url-mapping>

    An a bean like this one:

    @ManagedBean
    @RequestScoped
    public class Page2Backing {

    private String categoryName;

    private Category category;

    /* .... */

    public void onLoad() {
    this.category = categoryDao.findByName(categoryName);
    }

    }

    Please note that in this example you won’t have the ID to load the correct category but only the name. But this is required if you want SEO friendly URLs without IDs.

    Here a few references to the documentation:

    http://ocpsoft.com/docs/prettyfaces/3.2.0/en-US/html/Configuration.html#config.pathparams

    http://ocpsoft.com/docs/prettyfaces/3.2.0/en-US/html/Configuration.html#config.actions

    Christian

    #20848

    kovercash
    Participant

    Thank you Christian, I will try the method you provided.

    I am still curious as to whether the ?iid=2 could be passed to the backing bean and suppressed from the URL in the example I provided.

    Kevin

    #20849

    @kovercash

    It can if you are willing to do two redirects in order to render a single page, and save the value into the session. Otherwise, no, not really.

    -Lincoln

    #20850

    Basically that parameter needs to be *somewhere* in the URL on an HTTP GET request, otherwise it will never make it to the server. It could be in the path, it could be in a query-param, but it’s gotta be somewhere.

    #20851

    kovercash
    Participant

    Thank you Lincoln.

    Excellent job on Pretty Faces.

    ko

    #20852

    Thank you :) Let us know if we can make it better!

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

You must be logged in to reply to this topic.

Comments are closed.