malejpavouk
malejpavouk

Reputation: 4445

jsf2: form: GET method

I am trying to construct equivalent of this code in JSF2

                <form action="/search.xhtml" method="get">
                    <div class="search">
                        <input type="hidden" name="mutation" value="#{facesContext.viewRoot.locale}" />
                        <input name="searchString" class="text" />
                        <input type="submit" class="searchSubmit" value="#{msg.searchIt}" />
                    </div>
                </form>

The point of this construction is to redirect user to the search.html page, which shows the search results. The page uses URL params to decode the searchString and language mutation. And because it uses get, it is also bookmarkable.

With JSF2 I tried to use the h:button with param for the mutation, but I dont have a clue, how to force jsf to encode the h:inputText searchString.

Thanks for your help.

Upvotes: 3

Views: 7676

Answers (1)

Dmitry
Dmitry

Reputation: 367

As far as I know, there is no possibility to use method="GET" in JSF. It is not what you exactly want, but may be using post-redirect-get pattern can solve your issue:

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core">
    <f:metadata>
       <f:viewParam name="searchString" value="#{requestScopedBean.searchString}"/>
       <f:viewParam name="mutation" value="#{requestScopedBean.mutation}"/>
    </f:metadata>
    <h:body>
        <h:form>
            <h:inputText value="#{requestScopedBean.searchString}"/>
            <h:commandButton value="submit" action="/tests/search?faces-redirect=true&amp;includeViewParams=true">
                <f:param name="mutation" value="whatever"/>
            </h:commandButton>
        </h:form>
    </h:body>

</html>

More about PRG pattern in JSF2 in this article: http://www.warski.org/blog/?p=185

Upvotes: 5

Related Questions