组成部分
id
应该是独一无二的。这是HTML规范隐式要求的。你知道,JSF所做的只是生成适当的HTML/CSS/JS代码。给他们一个不同的
身份证件
或者只是把它放在一边,它在这种特定的情况下没有额外的价值(除非你想在它上面挂一些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>
拥有财产
operation
在你
calculator
管理bean:
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="/" />
在您的
计算器
管理bean:
public String add() {
answer = number1 + number2;
return "answer";
}
public String substract() {
answer = number1 - number2;
return "answer";
}
// etc...
就让
获得应答()
返回
answer
在那里什么也不做。这是更明确的职责分离。