Java Reference
In-Depth Information
Listing 6-1 illustrates the code of the
form.xhtml
file.
Listing 6-1. form.xhtml
1. <?xml version="1.0" encoding="UTF-8"?>
2. <!DOCTYPE html >
3. <html xmlns="
http://www.w3.org/1999/xhtml
"
4. xmlns:h="
http://java.sun.com/jsf/html
"
>
5.
6. <h:head>
7. <title>First JSF app</title>
8. </h:head>
9. <h:body>
10. <h3>Enter your name:</h3>
11.
12. <h:form>
13. <h:inputText value="#{helloBean.name}"></h:inputText>
14. <h:commandButton value="Submit" action="hello"></h:commandButton>
15. </h:form>
16. </h:body>
17. </html>
Line 4
: The
xmlns
attribute declares the JSF namespace.
Lines 6, 9, 12, 13, and 14
: Some tags have prefixes, such as
h:head
and
h:inputText
. These are JSF tags. The
h:inputText
and
h:commandButton
tags
correspond to the text field and Submit button in Figure 6-11.
Line 13
: The input fields are linked to object properties. For example,
attributevalue="#{helloBean.name}"
tells the JSF implementation to link the
text field with the
name
property of a user object.
Line 14
: The
#{...}
delimiters enclose expressions in the JSF expression
language.
Line 14
: When you enter the name and click the Submit button, the
hello.xhtml
file is displayed, as specified in the
action
attribute of the
h:commandButton
tag.
Listing 6-2 illustrates the code of the
hello.xhtml
file.
Listing 6-2. hello.xhtml
1. <?xml version="1.0" encoding="UTF-8"?>
2. <!DOCTYPE html>
3. <html xmlns="
http://www.w3.org/1999/xhtml
"
4. xmlns:h="
http://java.sun.com/jsf/html
"
>
5.
6. <h:head>
7. <title>Hello world</title>
8. </h:head>
9. <h:body>
10. <h2>Hello #{helloBean.name}</h2>
11. </h:body>
12. </html>