无法在JAVA后端获取JSF输入字段值

时间:2009-11-28 18:22:29

标签: forms jsf parameters request submit

我在JSF上有以下UI部分 - 它是带输入字段的简单搜索表单并提交:

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

相应地,在后端,我尝试下一步获取输入字段的值:

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;

}

将null名称传递给后端。

似乎.jsf部分的问题,但从第一眼看起来还不错......

1 个答案:

答案 0 :(得分:2)

您必须将<h:inputText>指向托管bean属性:

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

并在bean中定义:

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

然后在getFound()方法中使用searchParam;

当然,您需要将bean定义为托管bean,但我认为您已经完成了它:

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

您可以查看JSF教程(例如this