paul jerman
paul jerman

Reputation: 611

Pulling from inputText field always returns null

So, I'm currently working with JSF trying to create a simple project that takes input from a JSF inputText field, does something to it, and then spits it back out. Unfortunately i've reached an infuriating roadblock when trying to pull data from the inputText field. Whenever I attempt to pull from this field, it always returns null, even though there is obviously data in the field. Also, whenever I pull data from this inputText field, the field goes blank. Help with this issue would be greatly appreciated. Here is my code:

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

and then the backend managed bean is as follows:

package bean;

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

@ManagedBean @RequestScoped public class 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;
}

}

I've been pulling my hair trying to figure this out for hours, so any help is very welcome. Thanks in advance,

-Paul

Upvotes: 1

Views: 1951

Answers (1)

Dipul Patel
Dipul Patel

Reputation: 187

Try:

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

Upvotes: 2

Related Questions