从inputText字段拉取始终返回null

时间:2012-09-26 02:58:01

标签: jsf web

所以,我正在使用JSF尝试创建一个简单的项目,该项目从JSF inputText字段获取输入,对其执行某些操作,然后将其吐出。不幸的是,当我尝试从inputText字段中提取数据时,我遇到了一个令人愤怒的障碍。每当我尝试从该字段拉出时,它总是返回null,即使字段中有明显的数据。此外,每当我从此inputText字段中提取数据时,该字段将变为空白。非常感谢帮助解决这个问题。这是我的代码:

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html">
<h:head>
    <title>Title</title>
</h:head>
<h:body>
    <h:inputText id="username" required="true"
                 binding="#{mBean.searchField}" />
    <h:button onclick="#{mBean.pull()}"/>
    <script type="text/javascript" language="javascript">
        function doThing(i){
            document.getElementById('myDiv').innerHTML = i;
        }
    </script>
    <button onclick="alert('clicked')" onmouseup="doThing('#{mBean.value}')">display</button><br/>
    <div id="myDiv"></div>
</h:body>

然后后端托管bean如下:

package bean;

import javax.faces.bean.ManagedBean; import javax.faces.bean.RequestScoped; import javax.faces.component.UIInput;

@ManagedBean @RequestScoped 公共类MBean {

/**
 * Creates a new instance of MBean
 */
public String value;
public String otherValue;
public UIInput searchField;

public MBean() {
    System.out.println("starting mbean");
}

public String getValue() {
    System.out.println("returning first value " + value);
    return value;
}

public void setValue(String v) {
    this.value = v;
    System.out.println("setting value to " + v);
}
public void report(){
    System.out.print("SUBMIT----");
    System.out.println("value: " + value);
    System.out.println("otherValue: " + otherValue);
}
public String getOtherValue() {
    System.out.println("got " + otherValue);
    return otherValue;
}
public void pull(){
    value = (String)searchField.getValue();
    System.out.println("pulled value is " + value);
}
public void setOtherValue(String otherValue) {
    this.otherValue = otherValue;
    System.out.println("otherValue was set to " + otherValue);
}

public UIInput getSearchField() {
    return searchField;
}

public void setSearchField(UIInput searchField) {
    this.searchField = searchField;
}

}

我一直在拉着头发试图弄清楚这几个小时,所以任何帮助都是非常受欢迎的。提前谢谢,

-Paul

1 个答案:

答案 0 :(得分:2)

尝试:

 <h:inputText id="username" required="true" immediate="true"
                  binding="#{mBean.searchField}" />