Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass url parameters to JSF/xHTML?

I know this has been asked multiple times but mine is a slightly diff question.

I have a JSF page which queries a database and throws results, before i move to JSF, i used to do this in JSP and it was working fine.

When i was using JSP below is the link format i used to use

http://localhost:8080/blmdatabase/index.jsp?SearchString=Name&Category=Contact&Submit=Submit

My index.jsp used to capture the values using param.SearchString & param.Category, and 'Submit' used to activate the 'submit' button for the search.

How do i do the same for xHTML/JSF ?

Here is what i tried ...

http://localhost:8080/blmdatabase/index.xhtml?search=#{some search string}

In my index.xhtml

    <td>
    <f:metadata>
        <f:viewParam name="search" value="#{databaseSearch.searchstring}" />
    </f:metadata>
  <p:inputText id="searchstring" size="20" maxlength="20"  value="#{databaseSearch.searchstring}"/> <p:commandButton id="submit" icon="ui-icon-search" title="Search Database" update="panel" actionListener="#{databaseSearch.customerList}" />
    </td>

in my databaseSearch.java

@ManagedBean(name = "databaseSearch")
@SessionScoped

public class databaseSearch implements Serializable {

    public String searchstring;
  //getter and setter for searchstring
}

Also, i would need it 'Submit' the form .... I am new to this, so please excuse me if this was already discussed before ...

Also if i specific index.html , my jsf components would not load, just a blank page. like if i go

http://localhost:8080/blmdatabase/

my primefaces components load fine, but if i do

 http://localhost:8080/blmdatabase/index.xhtml

it does not, so now i am wondering how to pass the parameters :(

Web.xml

<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
like image 845
Telson Alva Avatar asked Dec 10 '25 10:12

Telson Alva


1 Answers

You could 'submit' your form adding <f:event type="preRenderView"> inside the <f:metadata> tag.

<f:metadata>
    <f:viewParam name="search" value="#{databaseSearch.searchstring}" />
    <f:event type="preRenderView" listener="#{databaseSearch.doSearch}" />
</f:metadata>

This way, you could implement how your bean will search for this specific query string

public void doSearch(ComponentSystemEvent event) {
    if(!searchString.isEmpty()) {
        // Do your search here 
    }
}
like image 196
Diogo Moreira Avatar answered Dec 12 '25 03:12

Diogo Moreira



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!