Java Reference
In-Depth Information
40
public void
add() {
41 result = number1 + number2;
42 }
43
44
public void
subtract() {
45 result = number1 - number2;
46 }
47
48
public void
divide() {
49 result = number1 / number2;
50 }
51
52
public void
multiply() {
53 result = number1 * number2;
54 }
55 }
add
subtract
divide
multiply
The managed bean has three properties
number1
,
number2
, and
result
(lines 9-38). The
methods
add()
,
subtract()
,
divide()
, and
multiply()
add, subtract, multiply, and
divide
number1
with
number2
and assigns the result to
result
(lines 40-54).
L
ISTING
33.8
Calculator.xhtml
1 <?xml version='1.0' encoding='UTF-8' ?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4 <html xmlns="http://www.w3.org/1999/xhtml"
5 xmlns:h="http://xmlns.jcp.org/jsf/html">
6 <h:head>
7 <title>Calculator</title>
8 </h:head>
9 <h:body>
10 <h:form>
11 <h:panelGrid columns="6">
12 <h:outputLabel value="Number 1"/>
13 <h:inputText id="number1InputText" size ="4"
14 style="text-align: right"
15 value="#{calculator.number1}"/>
16 <h:outputLabel value="Number 2" />
17 <h:inputText id="number2InputText" size ="4"
18 style="text-align: right"
19 value="#{calculator.number2}"/>
20 <h:outputLabel value="Result" />
21 <h:inputText id="resultInputText" size ="4"
22 style="text-align: right"
23 value="#{calculator.result}"/>
24 </h:panelGrid>
25
26 <h:panelGrid columns="4">
27 <h:commandButton value="Add"
28 action ="#{calculator.add}"/>
29 <h:commandButton value="Subtract"
30 action ="#{calculator.subtract}"/>
31 <h:commandButton value="Multiply"
32 action ="#{calculator.multiply}"/>
33 <h:commandButton value="Divide"
34 action ="#{calculator.divide}"/>
35 </h:panelGrid>
36 </h:form>
37 </h:body>
38 </html>
right align
bind text input
action
Search WWH ::
Custom Search