Question

I have following UI part on JSF - it's simple search form with input field and submit:

    <h:form>
        <h:commandButton action="#{operation.found}" value="#{msg.search}" />
        <h:inputText name="searchParam"/>
    </h:form>

And correspondingly, on backend, i attempt to get value of input field next way:

public List<Store> getFound() {

    String name = (String) FacesContext.getCurrentInstance()
            .getExternalContext().getRequestParameterMap().get(
                    "searchParam");

    SessionFactory sessionFactory = new Configuration().configure()
            .buildSessionFactory();

    HibernateTemplate hbt = new HibernateTemplate();

    hbt.setSessionFactory(sessionFactory);

    foundStores = hbt.find(BEAN_PATH + " WHERE name = ?",
            new Object[] { name });

    return foundStores;

}

And null name is passed to backend.

It seems that problem in .jsf part, but from first glance looks ok...

Was it helpful?

Solution

You must point the <h:inputText> to a managed-bean property:

<h:inputText name="searchParam" value="#{searchBean.searchParam}" />

and define in your bean:

private String searchParam;
public String getSearchParam() {..}
public void setSearchParam(String searchParam) {..}

and then use the searchParam in your getFound() method;

Of course, you need to have the bean defined as managed bean, but I assume you have done it:

<managed-bean>
    <managed-bean-name>searchBean</managed-bean-name>
    <managed-bean-class>mypackage.SearchBean</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

You can check a JSF tutorial (like this, for example)

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