views:

671

answers:

1

Hello.

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

+1  A: 

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)

Bozho
See,please, full method for search upper.Managed bean configured ok.I've added searchParam property to bean.An now UI looks like: <h:form> <h:commandButton action="#{operation.found}" value="#{msg.search}" /> <h:inputText name="searchParam" value="#{store.searchParam}" /> </h:form>But still get null on backend.
sergionni
the action method should be void, and you should use `operation.getFound`, instead of operation.found.
Bozho
perhaps i needn't searchParam for bean?it seems that i need just get input value from request and pass it to HQL query.from point of Portlets and JSP it's clear actions, but not so clear for JSF and managed beans.
sergionni
JSF doesn't put the input parameters in the request in an predictable way. You don't have to get the request at all.Go check some tutorial - this for example - http://www.exadel.com/tutorial/jsf/jsftutorial-kickstart.html
Bozho
thank you for answers, i managed do it with getBean() and serachParam property
sergionni