【问题标题】:Glassfish complaining about JSF component IDsGlassfish 抱怨 JSF 组件 ID
【发布时间】:2010-06-16 15:45:29
【问题描述】:

我对 JSF (v2.0) 非常陌生,我正在尝试在 netbeans.org 和 coreservlets.com 等地方学习它。我正在开发一个非常简单的“加/减/乘/除”Java webapp,但遇到了问题。当我刚开始时,应用程序是输入两个数字并按“+”键,它们会自动加在一起。现在我已经增加了更多的复杂性,我无法对托管 bean 进行操作。这就是我刚刚“添加”时所拥有的:

<h:inputText styleClass="display" id="number01" size="4" maxlength="3" value="#{Calculator.number01}" />  
<h:inputText styleClass="display" id="number02" size="4" maxlength="3" value="#{Calculator.number02}" />  
<h:commandButton id="add" action="answer" value="+" />  

对于“答案”页面,我这样显示答案:

<h:outputText value="#{Calculator.answer}" />  

我在 Calculator.java 托管 bean 中有正确的 getter 和 setter,并且操作运行良好。

现在我已经添加了其他三个操作,我无法想象如何将操作参数传递给 bean,以便我可以切换它。我试过这个:

<h:commandButton id="operation" action="answer" value="+" />       
<h:commandButton id="operation" action="answer" value="-" />  
<h:commandButton id="operation" action="answer" value="*" />  
<h:commandButton id="operation" action="answer" value="/" />  

但是,Glassfish 抱怨我已经使用过一次“操作”,我在这里尝试使用它四次。

关于如何对托管 bean 进行多项操作以便它可以执行所需操作的任何建议/提示?

感谢您抽出宝贵时间阅读。

【问题讨论】:

    标签: jsf


    【解决方案1】:

    组件id 确实应该是唯一的。这是 HTML 规范隐含的要求。你知道,JSF 所做的只是生成适当的 HTML/CSS/JS 代码。给他们一个不同的id 或者干脆别管它,在这种特定情况下它没有额外的价值(除非你想在上面挂上一些 CSS/JS)。

    要实现您的功能要求,您可能会发现f:setPropertyActionListener 很有用。

    <h:commandButton action="answer" value="+">
        <f:setPropertyActionListener target="#{calculator.operation}" value="+" />
    </h:commandButton>      
    <h:commandButton action="answer" value="-">  
        <f:setPropertyActionListener target="#{calculator.operation}" value="-" />
    </h:commandButton>      
    <h:commandButton action="answer" value="*">  
        <f:setPropertyActionListener target="#{calculator.operation}" value="*" />
    </h:commandButton>      
    <h:commandButton action="answer" value="/"> 
        <f:setPropertyActionListener target="#{calculator.operation}" value="/" />
    </h:commandButton>      
    

    并且在您的 calculator 托管 bean 中有一个属性 operation

    private String operation; // +setter.
    

    您可以在getAnswer()方法中访问它并进行相应的处理。


    或者,让每个按钮指向一个不同的 bean 操作,但返回所有 "answer"

    <h:commandButton action="#{calculator.add}" value="+" />      
    <h:commandButton action="#{calculator.substract}" value="-" />      
    <h:commandButton action="#{calculator.multiply}" value="*" />      
    <h:commandButton action="#{calculator.divide}" value="/" />      
    

    在您的 calculator 托管 bean 中使用以下方法:

    public String add() {
        answer = number1 + number2;
        return "answer";
    }
    
    public String substract() {
        answer = number1 - number2;
        return "answer";
    }
    
    // etc...
    

    然后让getAnswer() 返回answer,然后什么都不做。这是更清晰的职责分离。

    【讨论】:

    • 组件 id 确实应该是唯一的。 - 无论如何,NamingContainer 是唯一的,但这可能不是初学者想要进入的主题。
    • @McDowell:我已经犹豫是否要添加此信息,但我决定保留此详细信息 :)
    • 不客气 :) 不要忘记将答案标记为已接受。另见stackoverflow.com/faq
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-08
    • 1970-01-01
    • 2017-11-17
    • 1970-01-01
    • 1970-01-01
    • 2021-07-29
    相关资源
    最近更新 更多