2012-09-26 26 views
1

因此,我目前正在與JSF一起嘗試創建一個簡單的項目,該項目從JSF inputText字段獲取輸入,對其執行某些操作,然後將其吐出。不幸的是,當我試圖從inputText字段中提取數據時,我遇到了一個令人生氣的障礙。每當我試圖從這個字段拉,它總是返回null,即使有明顯的數據在字段中。此外,無論何時從這個inputText字段中提取數據,該字段都會變爲空白。幫助這個問題將不勝感激。這裏是我的代碼:從inputText字段總是返回null

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

}

我一直在拉我的頭髮試圖弄清楚這一點了幾個小時,所以任何幫助是非常值得歡迎的。在此先感謝,

- 保羅

回答

2

嘗試:

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

感謝您的快速反應。不幸的是,我有時間限制,所以我已經選擇了不同的方法。只要我有機會,我會測試你的解決方案,所以我可以upvote你的答案。 –