【问题标题】:JSF two parameters from two inputText to one methodJSF从两个inputText到一个方法的两个参数
【发布时间】:2011-11-05 12:12:32
【问题描述】:

我有一个方法,它接受 x 和 y 并将它们相加并返回结果。 现在第一个 InputText 将采用 x,第二个采用 Y。但我想将 X 和 Y 都传递给同一个方法,以便调用 public int addNumbers(int x, int y) 并返回它。知道怎么做吗? 我对 JSF 非常陌生 谢谢,

【问题讨论】:

    标签: java jsf


    【解决方案1】:

    有一个托管表单

    public DataFormBean{
       String number1;
       String number2;
       //setter getter + constructors
    }
    
    public ActionBean{
        DataFormBean dataBean;
        //setter + getter
    
        public String add(){
           // convert your number here to int and pass it to the method and set the result back to the baen
        }
    }
    

    您需要声明托管 bean 并进行注入

    <h:form>
      <h:inputText value="#{dataBean.number1}" >
      <h:inputText value="#{dataBean.number2}" >
    <h:commandButton action="#{actionbean.add}" >
    </h:form>
    

    见

    【讨论】:

    • 嗯,我有托管 bean,其中定义了我提到的方法。但我想知道你如何将参数从 xhtml 页面传递给方法
    • 是的,因为通常我可以只定义一个 inputText 并执行 #{beanName.method} 但至于 2 个参数我无法弄清楚
    • 您需要将 inputTexts 绑定到 number1 和 number2,如 Jigar Joshi 所说,在添加操作中您只需执行 getNumber1() + getNumber2() 之类的操作。您需要在 bean 中添加 getter 和 setter
    • @sys_debug 您不会将参数直接传递给方法签名。组件值绑定到字段值并在JSF2 Lifecycle 的“更新模型值”阶段进行更新。然后在 backing bean 中你可以在你的 action 方法中使用它们。
    • 所以如果我理解正确的话,我应该在托管bean中有add方法......然后对于页面中的每个属性或元素,我将获取它的值,然后执行添加独立地(通过调用函数)而不需要直接传递参数。我希望这是正确的
    猜你喜欢
    • 2012-08-18
    • 2011-12-16
    • 2012-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-22
    • 1970-01-01
    相关资源
    最近更新 更多