从javascript更新bean
我有一个.xhtml页面,从中我启动了一个javascript,在这个javascript中我想更新一个bean的内容,我发现最简单的方法是添加一个隐藏的表单并将该bean的属性链接到它的值: index.xhtml从javascript更新bean,javascript,jsf,Javascript,Jsf,我有一个.xhtml页面,从中我启动了一个javascript,在这个javascript中我想更新一个bean的内容,我发现最简单的方法是添加一个隐藏的表单并将该bean的属性链接到它的值: index.xhtml <h:form id="poi-form" styleClass="invisible"> <h:inputHidden id="poi" value="#{userBean.email}" /> </h:form> <h:for
<h:form id="poi-form" styleClass="invisible">
<h:inputHidden id="poi" value="#{userBean.email}" />
</h:form>
<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>
但是,从timeline.xhtml中,该值不是我期望的值(好像它没有更新),因为我在中看到了用户的旧电子邮件值
userBean.java
@PostConstruct
public void init() {
email = "usersOldEmailValue;
}
public String updateData() {
HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
this.email= request.getParameter("poiemail");
return "redirectURL";
}
我忘了什么吗?提前感谢您的帮助 您实际上忘记了提交表单,因此服务器端可以更新您的模型
此外,如果您使用的是PrimeFaces,也可以使用p:remoteCommand。使用以下代码修复: index.xhtml
<h:form id="poi-form" styleClass="invisible">
<h:inputHidden id="poi" value="#{userBean.email}" />
</h:form>
<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>
userBean.java
@PostConstruct
public void init() {
email = "usersOldEmailValue;
}
public String updateData() {
HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
this.email= request.getParameter("poiemail");
return "redirectURL";
}
哪个JSF版本?为什么要使用javascript导航到另一个页面?你能进一步解释一下吗?也许我们可以建议一个更好的方案approach@Tarik我使用javascript导航到另一个页面,因为我使用的javascript来自第三方(google plus登录),我必须在我的项目中使用……@Tarik JSF版本是2.2顺便问一下,为什么不调用ajax?也许你可以在这里找到一个让你满意的解决方案:我已经用函数handleEmailResponse(resp){document.getElementById('poi-form:poi').value='usersNewEmailValue';document.getElementById('poi-form').submit();window.location.replace(“timeline.xhtml”);}更新了bean中的任何内容。。。谢谢你的提示!在表单提交完成之前,您正在离开页面。您应该在表单提交完成后进行位置更新。您的浏览器可能找不到足够的时间来执行表单提交,因为您会立即告诉它离开页面。