Reputation: 329
I have a .xhtml page from which I launch a javascript, inside this javascript I would like to update the content of a bean, the easiest way I have found to do this is by adding a hidden form and linking said bean's property to its value:
index.xhtml
<h:form id="poi-form" styleClass="invisible">
<h:inputHidden id="poi" value="#{userBean.email}" />
</h:form>
javascriptfile.js
function handleEmailResponse(resp) {
document.getElementById('poi-form:poi').value = 'usersNewEmailValue';
window.location.replace("timeline.xhtml");
}
However, from the timeline.xhtml the value isn't the one I expected (as if it isn't updated) since I see the users old email value set in
userBean.java
@PostConstruct
public void init() {
email = "usersOldEmailValue;
}
Am I forgetting something? Thanks in advance for your help!
Upvotes: 3
Views: 3224
Reputation: 329
Fixed using the following code:
index.xhtml
<h:form id="poi-form" styleClass="invisible">
<input type="text" id="poiemail" name="poiemail" />
<h:commandButton action="#{userBean.updateData()}" id="miAwesomeButton" value="I'm clicked by javascript"/>
</h:form>
javascriptfile.js
function handleEmailResponse(resp) {
document.getElementById('poiemail').value = 'usersNewEmailValue';
document.getElementById('poi-form:miAwesomeButton').click();
}
userBean.java
public String updateData() {
HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
this.email= request.getParameter("poiemail");
return "redirectURL";
}
Upvotes: 1
Reputation: 913
You are actually forgetting submitting the form, so the server side could update your model.
Also, if you are using PrimeFaces, you can use p:remoteCommand as well.
Upvotes: 2