Question

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>
Was it helpful?

Solution

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 
    }
}

OTHER TIPS

Your Faces Servlet maps to anything that's hold in the faces virtual folder:

<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
</servlet-mapping>

So http://localhost:8080/blmdatabase/index.xhtml URL won't be parsed through Faces Servlet. You must use http://localhost:8080/blmdatabase/faces/index.xhtml, note the use of faces/ before your index.xhtml file, also note that your <welcome-file> also points to faces/index.xhtml.

The downside of this URL pattern is that Faces Servlet would also process non-facelets resources like JavaScript files (.js), Style files (.css), images (*.png, *.jpg) and others. A better Faces Servlet mapping would be:

<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

In this way, Faces Servlet will process xhtml pages only and you won't need the faces virtual folder anymore. With this change, now you can access to http://localhost:8080/blmdatabase/index.xhtml with no problems.

you could use a method in the class dataBaseSearch:

if(searchString != null){
//execute a query in database
// return result to a variable(resultSet or list)
}

and use getter and setter to get the the resultSet or List and render the result in a datatable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top