1

因此,我目前正在与 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;

导入 javax.faces.bean.ManagedBean;导入 javax.faces.bean.RequestScoped;导入 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;
}

}

几个小时以来,我一直在努力解决这个问题,所以非常欢迎任何帮助。提前致谢,

-保罗

4

1 回答 1

2

尝试:

 <h:inputText id="username" required="true" immediate="true"
                  binding="#{mBean.searchField}" />
于 2012-09-26T04:59:53.620 回答