3

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!

4
  • which JSF version? and why are you using javascript to navigate to another page? could you explain further maybe we can suggest a better approach Commented Feb 23, 2015 at 1:10
  • @Tarik I'm using javascript to navigate to another page as the javascript I am using is from a 3rd party (google plus sign in) that I must use in my project.... Commented Feb 23, 2015 at 9:28
  • @Tarik JSF version is 2.2 by the way Commented Feb 23, 2015 at 9:46
  • why not making ajax call? maybe you can find a solution that sweets you here: stackoverflow.com/questions/8567114/… Commented Feb 23, 2015 at 14:50

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I have updated my code with: function handleEmailResponse(resp) { document.getElementById('poi-form:poi').value = 'usersNewEmailValue'; document.getElementById('poi-form').submit(); window.location.replace("timeline.xhtml"); } Yet still nothing is updated in the bean... thanks for the tip though!
you are navigating away from the page before the form submit finishes. you should do the location update after form submission completed. your browser probably cannot find enough time to perform the form submit, since you immediately tell it to navigate away from the page.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.