1

My JavaScript:

function setJson(value) {
        document.getElementById("json").value = value;
    }

My XHTML:

<h:inputHidden id="json" value="#{indexManaged.json}" valueChangeListener="#{indexManaged.goTo('datatable')}" />

My ManagedBean:

@ManagedBean
@ViewScoped
public class IndexManaged implements Serializable {

    private String json;
    public String getJson() { return json; }
    public void setJson(String json) { this.json = json; }

    public String goTo(String page) {
        Flash flash = FacesContext.getCurrentInstance().getExternalContext().getFlash();
        flash.put("json", json);
        return page + "?faces-redirect=true";
    }
}

The scenario:

I have a Java Applet that fires the function setJson(value). But when the applet sets a new value to my inputHidden, isn't the valueChangeListener suposed to fire my ManagedBean method? What am I doing wrong?

BBacon
  • 2,456
  • 5
  • 32
  • 52
  • 2
    Are you aware that `valueChangeListener` is fired upon form submit and not value change event (like `f:ajax`) – Daniel Sep 15 '13 at 19:00
  • Please elaborate "not working" in developer's terms instead of enduser's terms. How exactly are you submitting the value to the server? – BalusC Sep 16 '13 at 13:38
  • I think Daniel pointed out the problem. Is there a way to use AJAX on a inputHidden? Sorry about the poorly developed question BalusC... I improved it a little bit. – BBacon Sep 16 '13 at 19:28

2 Answers2

3

The valueChangeListener isn't a client side listener. It's a server side listener which runs when the form is submitted. So, you need to submit the form as well.

Wrap it in a form

<h:form id="form">

and submit it as follows

document.getElementById("form").submit();

Don't forget to fix the client ID of the hidden input accordingly:

document.getElementById("form:json").value = value;

See also:


Unrelated to the concrete problem, there are cleaner ways to achieve this. Have a look at PrimeFaces <p:remoteCommand> and OmniFaces <o:commandScript>.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

Not sure about the <h:inputHidden

But you can use the following thing:

<h:inputText id="json" value="#{indexManaged.json}" style="display:none">
    <f:ajax listener="#{myBean.myListener}"/>
</h:inputText>

and trigger it with jquery like this:

$("#json").change();// instead of `json` you might be needed to provide 
  //full id like this,  $("#myform\\:json").change()
Daniel
  • 36,833
  • 10
  • 119
  • 200