在JavaScript中使用备用bean值 [英] Using backing bean value in javascript

查看:105
本文介绍了在JavaScript中使用备用bean值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

   <h:form prependId="false" id="vt_sel_form">
         <p:panelGrid styleClass="center" columns="2">                     
                        <p:commandButton value="GO" oncomplete="alert(#{test.i})"  actionListener="#{test.testfxn()}" update="@this"/>
         </p:panelGrid>
    </h:form>


  import java.io.Serializable;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.ViewScoped;

    @ViewScoped
    @ManagedBean(name = "test")

    public class TestClass implements Serializable {

        int i ;

        public int getI() {
            return i;
        }

        public void setI(int i) {
            this.i = i;
        }

        public void testfxn() {        
            setI(i++);
            //i=i+10;
            System.out.println("i" + i);
        }
    }

在这里,alert(#{test.i})始终显示为0.如何获得当我单击commandButton时更改的后备bean值. 当我单击按钮两次时,它可以工作.当我使用 a4j:commandButton 时,它可以正常工作.

Here, alert(#{test.i}) is always displaying 0. How do i get backing bean value that changes when I click the commandButton. It works when I click button twice. It used to work fine when I used a4j:commandButton.

推荐答案

这仅仅是因为>是在呈现 时对alert(#{test.i});求值的.您可以通过告诉JSF再次渲染脚本来查看更改后的值:

That's just because alert(#{test.i}); is evaluated when the commandButton is rendered. You can see the changed value by telling JSF to render the script again:

<h:commandButton value="click me" action="#{testClass.testfxn()}">
    <f:ajax render="out" />
</h:commandButton>
<h:panelGroup id="out">
    <h:outputScript>
         alert(#{testClass.i});
    </h:outputScript>
</h:panelGroup>

这篇关于在JavaScript中使用备用bean值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆