ptg
From the Library of Wow! eBook
ptg
FACELETS PAGE LAYOUT
TEXT FIELD
page.xhtml
WEB-INF/classes/com/corejsf/SampleBean.java
BUTTON
page.xhtml
WEB-INF/classes/com/corejsf/SampleBean.java
The outcomes success and error can be mapped to pages
in faces-config.xml. If no mapping is specified, the page
/success.xhtml or /error.xhtml is displayed.
GET REQUESTS
Request parameters set bean properties before the
page is rendered.
The getContinueOutcome method is called when the button is
rendered. The view parameters are added to the
request URL.
RADIO BUTTONS
page.xhtml
WEB-INF/classes/com/corejsf/SampleBean.java
WEB-INF/classes/com/corejsf/Condiment.java
CONVERSION
The number is displayed with currency symbol and
group separator: $1,000.00
VALIDATION
Using the bean validation framework (JSR 303)
Page-level validation and conversion
Error messages
RESOURCES
page.xhtml
resources/css/styles.css
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>...</h:head>
<h:body>
<h:form>
...
</h:form>
</h:body>
</html>
<h:inputText value="#{bean1.luckyNumber}">
@Named("bean1") // or @ManagedBean(name="bean1")
@SessionScoped
public class SampleBean {
public int getLuckyNumber() { ... }
public void setLuckyNumber(int value) { ... }
...
}
<h:commandButton value="press me" action="#{bean1.login}"/>
public class SampleBean {
public String login() {
if (...) return "success"; else return "error";
}
...
}
<f:metadata>
<f:viewParam name="item" value="#{bean1.currentItem}"/>
<f:viewParam name="userId" value="#{bean1.user}"/>
</f:metadata>
<h:button value="Continue" outcome="#{bean1.continueOutcome}"
includeViewParams="true"/>
<h:selectOneRadio value="#{bean1.condiment}>
<f:selectItems value="#{bean1.choices}" var="it"
itemLabel="#{it.description}"
itemValue="#{it.productId}"/>
</h:selectOneRadio>
public class SampleBean {
public Collection<Condiment> getChoices() { ...}
public int getCondiment() { ... }
public void setCondiment(int value) { ... }
...
}
public class Condiment {
public String getDescription() { ... }
public int getProductId() { ... }
}
<h:outputText value="#{bean1.amount}">
<f:convertNumber type="currency"/>
</h:outputText>
public class SampleBean {
@Max(1000) private BigDecimal amount;
}
<h:inputText value="#{bean1.amount}" required="true">
<f:validateDoubleRange maximum="1000"/>
</h:inputText>
Amount
<h:inputText id="amt" label="Amount" value="#{bean1.amount}"/>
<h:message for="amt"/>
<h:outputStylesheet library="css" name="styles.css"/>
...
<h:message for="amt" errorClass="errors">
.errors {
font-style: italic;
color: red;
}
From the Library of Wow! eBook
ptg
core
JAVASERVER
™
FACES
THIRD EDITION
From the Library of Wow! eBook
ptg
This page intentionally left blank
From the Library of Wow! eBook
ptg
DAVID GEARY
CAY HORSTMANN
Upper Saddle River, NJ • Boston • Indianapolis • San Francisco
New York • Toronto • Montreal • London • Munich • Paris • Madrid
Capetown • Sydney • Tokyo • Singapore • Mexico City
core
JAVASERVER
™
FACES
THIRD EDITION
From the Library of Wow! eBook
- 1
- 2
- 3
前往页