+ All Categories
Home > Documents > J2EE —— 第 18 章 在 JSP 页中使用 JavaServer Faces 技术

J2EE —— 第 18 章 在 JSP 页中使用 JavaServer Faces 技术

Date post: 20-Jan-2016
Category:
Upload: sasha
View: 122 times
Download: 0 times
Share this document with a friend
Description:
J2EE —— 第 18 章 在 JSP 页中使用 JavaServer Faces 技术. 设置页面. other JavaServer Faces tags and core tags, including one or - PowerPoint PPT Presentation
27
J2EE—— 第 18 第 第 JSP 第第第第 JavaServer Faces 第第
Transcript
Page 1: J2EE —— 第 18 章 在 JSP 页中使用 JavaServer Faces 技术

J2EE—— 第 18 章在 JSP 页中使用 JavaServer Faces

技术

Page 2: J2EE —— 第 18 章 在 JSP 页中使用 JavaServer Faces 技术

设置页面<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %> <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %> <f:view> <h:form> other JavaServer Faces tags and core tags, including one o

r more button or hyperlink components for submitting the form </h:form>

<f:subview id="myNestedPage"> <jsp:include page="theNestedPage.jsp" /> </f:subview></f:view>

Page 3: J2EE —— 第 18 章 在 JSP 页中使用 JavaServer Faces 技术

使用核心标签 (1)

Page 4: J2EE —— 第 18 章 在 JSP 页中使用 JavaServer Faces 技术

使用核心标签 (2)

Page 5: J2EE —— 第 18 章 在 JSP 页中使用 JavaServer Faces 技术

使用 HTML 组件标签- UI 组件标签属性

id :被另一组件或类引用,受条件或迭代标签影响 immediate :事件、验证和转换在应用请求值阶段

点击按钮时,新值可用 rendered :呈现组件的条件<h:commandLink id="check" ... rendered="#{cart.numberOfItems > 0}"> <

h:outputText value="#{bundle.CartCheck}"/></h:commandLink> style 和 styleClass<h:message style="color: red; font-family: 'New Century Schoolbook', serif;

font-style: oblique; text-decoration: overline" id="errors1" for="userNo"/>

<h:dataTable id="books“ styleClass="list-background“ value="#{bookDBAO.books}“ var="book">

Page 6: J2EE —— 第 18 章 在 JSP 页中使用 JavaServer Faces 技术

UIForm 组件和 UIColumn 组件

<h:form><h:dataTable id="items" ... value="#{cart.items}" var="item"> ... <h:column> <f:facet name="header"> <h:outputText value="#{bundle.ItemQuantity}"/> </f:facet> <h:inputText ... value="#{item.quantity}"> <f:validateLongRange minimum="1"/> </h:inputText> </h:column> ...</h:dataTable></h:form>

Page 7: J2EE —— 第 18 章 在 JSP 页中使用 JavaServer Faces 技术

UICommand 组件<h:commandButton value="#{bundle.Submit}" action="#{cashier.submi

t}"/><h:commandLink id="NAmerica" action="bookstore" actionListener="#{localeBean.chooseLocaleFromLink}"> <h:outputText value="#{bundle.English}" /> </h:commandLink> UIData 组件<h:dataTable id="items“ columnClasses="list-column-center, list-colum

n-left,list-column-right, list-column-center“ footerClass="list-footer"headerClass="list-header“ rowClasses="list-row-even, list-row-odd"styleClass="list-background“ value="#{cart.items}“ var="item">

Page 8: J2EE —— 第 18 章 在 JSP 页中使用 JavaServer Faces 技术

<h:column ><f:facet name="header"><h:outputText value="#{bundle.ItemQuantity}" /></f:facet> <h:inputText id="quantity" size="4“ value="#{item.quantity}" > ...</h:inputText> ...</h:column><h:column><f:facet name="header"><h:outputText value="#{bundle.ItemTitle}"/> </f:facet> <h:commandLink action="#{showcart.details}"> <h:outputText value="#{item.item.title}"/> </h:commandLink></h:column>...<f:facet name="footer“><h:panelGroup> <h:outputText value="#{bundle.Subtotal}"/> <h:outputText value="#{cart.total}" /> <f:convertNumber type="currency" /> </h:outputText></h:panelGroup></f:facet></h:dataTable>

Page 9: J2EE —— 第 18 章 在 JSP 页中使用 JavaServer Faces 技术

页面效果

Page 10: J2EE —— 第 18 章 在 JSP 页中使用 JavaServer Faces 技术

UIInput 和 UIOutput 组件<h:inputText id="name" size="50" value="#{cashier.name}“ required="tru

e"> <f:valueChangeListener type="listeners.NameChanged" /> </h:inputText> <h:selectBooleanCheckbox id="fanClub" rendered="false" binding="#{cash

ier.specialOffer}" /> <h:outputLabel for="fanClub" rendered="false" binding="#{cashier.special

OfferText}" > <h:outputText id="fanClubLabel" value="#{bundle.DukeFanClub}" /></h:outputLabel><h:outputLabel for="fanClub" rendered="false" binding="#{cashier.special

OfferText}" value="#{bundle.DukeFanClub}" /> </h:outputLabel>

Page 11: J2EE —— 第 18 章 在 JSP 页中使用 JavaServer Faces 技术

outputLink 、 outputFormat 、 outputSecret

<h:outputLink value="javadocs"> <f:verbatim>Documentation for this demo</f:verbatim> </h:outputLink> <h:outputFormat value="#{bundle.CartItemCount}"> <f:param value="#{cart.numberOfItems}"/> </h:outputFormat><h:inputSecret redisplay="false“ value="#{LoginBean.password}" />

Page 12: J2EE —— 第 18 章 在 JSP 页中使用 JavaServer Faces 技术

UIPanel 组件<h:panelGrid columns="3" headerClass="list-header“ rowClasses="list-row-

even, list-row-odd" styleClass="list-background" title="#{bundle.Checkout}">

<f:facet name="header"><h:outputText value="#{bundle.Checkout}"/> </f:facet> <h:outputText value="#{bundle.Name}" /> <h:inputText id="name" size="50" value="#{cashier.name}" required="true"> <f:valueChangeListener type="listeners.NameChanged" /> </h:inputText> <h:message styleClass="validationMessage" for="name"/> <h:outputText value="#{bundle.CCNumber}"/> <h:inputText id="ccno" size="19" converter="CreditCardConverter" required

="true"><bookstore:formatValidator formatPatterns= "9999999999999999|9999 9999 9999 9999|9999-9999-9999-9999"/> </h:inputText><h:message styleClass="validationMessage" for="ccno"/> ...

</h:panelGrid>

Page 13: J2EE —— 第 18 章 在 JSP 页中使用 JavaServer Faces 技术

UISelecteBoolean, UISelectMany 组件

<h:selectBooleanCheckbox id="fanClub“ rendered="false" binding="#{cashier.specialOffer}" /><h:outputLabel for="fanClub" rendered="false" binding="#{cashier.specialOfferText}"> <h:outputText id="fanClubLabel" value="#{bundle.DukeFanClub}" /

> </h:outputLabel>

<h:selectManyCheckbox id="newsletters“ layout="pageDirection" value="#{cashier.newsletters}"> <f:selectItems value="#{newsletters}"/></h:selectManyCheckbox>

Page 14: J2EE —— 第 18 章 在 JSP 页中使用 JavaServer Faces 技术

UIMessage,UIMessages, UISelectOne 组件

<h:inputText id="userNo" value="#{UserNumberBean.userNumber}"> <f:validateLongRange minimum="0" maximum="10" /> ... <h:message style="color: red; font-family: 'New Century Schoolbook',

serif; font-style: oblique; text-decoration: overline" id="errors1" for="userNo"/>

<h:selectOneMenu id="shippingOption“ required="true“ value="#{cashier.shippingOption}"> <f:selectItem itemValue="2“ itemLabel="#{bundle.QuickShip}"/> <f:selectItem itemValue="5“ itemLabel="#{bundle.NormalShip}"/> <f:selectItem itemValue="7" itemLabel="#{bundle.SaverShip}"/> </h:selectOneMenu>

Page 15: J2EE —— 第 18 章 在 JSP 页中使用 JavaServer Faces 技术

使用 selectItems 标签<h:selectManyCheckbox id="newsletters“ layout="pageDirection" value="#{cashier.newsletters}"> <f:selectItems value="#{newsletters}"/></h:selectManyCheckbox> 使用 selectItemGroup 使用本地化消息<f:loadBundle var="bundle" basename= "messages.BookstoreMessages" /><h:outputText value="#{bundle.Talk}"/>

Page 16: J2EE —— 第 18 章 在 JSP 页中使用 JavaServer Faces 技术

newsletters - a Managed Bean

<managed-bean><managed-bean-name>newsletters</managed-bean-name> <managed-bean-class> java.util.ArrayList</managed-bean-class><managed-bean-scope>application</managed-bean-scope> <list-entries> <value-class>javax.faces.model.SelectItem</value-class> <value>

#{newsletter0}</value> <value>#{newsletter1}</value> <value>#{newsletter2}</value> <value>#{newsletter3}</value>

</list-entries></managed-bean> <managed-bean><managed-bean-name>newsletter0</managed-bean-name><managed-bean-class> javax.faces.model.SelectItem</managed-bean-class><managed-bean-scope>none</managed-bean-scope><managed-property> <property-name>label</property-name> <value>Duke's Quarterly</value></managed-property><managed-property><property-name>value</property-name> <value>200</value></managed-property></managed-bean>

Page 17: J2EE —— 第 18 章 在 JSP 页中使用 JavaServer Faces 技术

使用标准转换器Integer age = 0;public Integer getAge(){ return age;}public void setAge(Integer age) {this.age = age;}<h:inputText converter="javax.faces.convert.IntegerConverter" /

><h:inputText value="#{LoginBean.Age}" /> <f:converter converterId="Integer" /> </h:inputText>

Page 18: J2EE —— 第 18 章 在 JSP 页中使用 JavaServer Faces 技术

使用 DateTimeConverter dateStyle, locale, pattern, timeStyle, timezone, type 属

性<h:outputText value="#{cashier.shipDate}"> <f:convertDateTime dateStyle="full" /> </h:outputText><h:outputText value="#{cashier.shipDate}"> <f:convertDateTime pattern="EEEEEEEE, MMM dd, yyyy" /></h:outputText>Saturday, Feb 22, 2003 <h:inputText value="#{cashier.shipDate}"> <f:convertDateTime dateStyle="full" locale="Locale.SPAIN" timeStyle

="long" /> </h:inputText>Sabado, Feb 22, 2003

Page 19: J2EE —— 第 18 章 在 JSP 页中使用 JavaServer Faces 技术

使用 NumberConverter currencyCode, currencySymbol, groupingUsed, integer

Only, maxFractionDigits, maxIntegerDigits, minFractionDigits, minIntegerDigits, locale, pattern, type 属性

<h:outputText value="#{cart.total}" > <f:convertNumber type="currency" </h:outputText><h:outputText id="cartTotal" value="#{cart.Total}" > <f:convertNum

ber pattern="$####" /> </h:outputText>$934

Page 20: J2EE —— 第 18 章 在 JSP 页中使用 JavaServer Faces 技术

在组件上注册监听器<h:inputText id="name" size="50" value="#{cashier.name}" requi

red="true"> <f:valueChangeListener type="listeners.NameChanged" /> </h:inputText><h:commandLink id="NAmerica" action="bookstore"> <f:actionL

istener type="listeners.LocaleChange" /> </h:commandLink> 使用标准验证程序<h:inputText id="name" size="50" value="#{cashier.name}" requi

red="true"> ...</h:inputText><h:inputText id="quantity" size="4" value="#{item.quantity}" > <f:

validateLongRange minimum="1"/> </h:inputText><h:message for="quantity"/><f:validateLongRange minimum="#{ShowCartBean.minimum}" /

>

Page 21: J2EE —— 第 18 章 在 JSP 页中使用 JavaServer Faces 技术

把组件值绑定到属性int currentOption = null; int getCurrentOption(){...}void setCurrentOption(int option){...} #{MyBean.currentOption}<managed-bean><managed-bean-name> NA </managed-bean-name

> <managed-bean-class> model.ImageArea </managed-bean-class> <managed-bean-scope> application </managed-bean-scope> <managed-property><property-name>shape</property-name> <value>poly</value></managed-property> ... </managed-bean><h:outputText value="#{NA.shape}" />

Page 22: J2EE —— 第 18 章 在 JSP 页中使用 JavaServer Faces 技术

示例值绑定表达式

Page 23: J2EE —— 第 18 章 在 JSP 页中使用 JavaServer Faces 技术

把组件值绑定到隐式对象<h:outputFormat title="thanks" value="#{bundle.ThankYouPara

m}"> <f:param value="#{sessionScope.name}"/> </h:outputFormat> applicationScope, cookie, facesContext, header, heade

rValues, initParam, param, paramValues, requestScope, sessionScope, view

除作用域对象外,所有隐式对象都是只读的,因此不应该用作 UIInput 组件的值

Page 24: J2EE —— 第 18 章 在 JSP 页中使用 JavaServer Faces 技术

把组件实例绑定到 bean 属性<h:selectBooleanCheckbox id="fanClub" rendered="false" binding="#{cashier.specialOffer}" /><h:outputLabel for="fanClub" rendered="false" binding="#{cashier.specialOfferText}" > <h:outputText id="fanClubLabel" value="#{bundle.DukeFanClub}" /> </h:outputLabel> 如果标签使用值绑定而不是组件绑定,那么受支持

bean 不会直接访问组件,因此要求附加的代码从 FacesContext 访问组件,以便更改组件的 renderred 属性

Page 25: J2EE —— 第 18 章 在 JSP 页中使用 JavaServer Faces 技术

引用受支持 bean 方法<h:commandButton value="#{bundle.Submit}" action="#{cashier.submit}"

/><h:commandLink id="NAmerica" action="bookstore" actionListener="#{localeBean.chooseLocaleFromLink}"><h:inputText id="email" value="#{checkoutFormBean.email}" size="25" ma

xlength="125" validator="#{checkoutFormBean.validateEmail}"/><h:inputText id="name" size="50" value="#{cashier.name}" required="tru

e"> <f:valueChangeListener type="listeners.NameChanged" /> </h:inputText><h:inputText id="name" size="50" value="#{cashier.name}" required="tru

e" valueChangeListener="#{cashier.processValueChange}" /></h:inputText>

Page 26: J2EE —— 第 18 章 在 JSP 页中使用 JavaServer Faces 技术

使用自定义对象<h:inputText id="ccno" size="19" converter="CreditCardConverter" required="true">...</h:inputText><h:inputText id="ccno" size="19" ... required="true"><bookstore:formatValidator formatPatterns="9999999999999999|999

9 9999 9999 9999| 9999-9999-9999-9999" /> </h:inputText> <h:message styleClass="validationMessage" for="ccno"/><h:inputText id="name" value="#{CustomerBean.name}" size="10" > <f:validator validatorId="customValidator" /> ... </h:inputText>

Page 27: J2EE —— 第 18 章 在 JSP 页中使用 JavaServer Faces 技术

使用自定义组件<h:graphicImage id="mapImage" url="/template/world.jpg" alt="#{bundle.chooseLocale}" usemap="#worldMap" /> <bookstore:map id="worldMap" current="NAmericas" immediate="true" action="bookstore" actionListener="#{localeBean.chooseLocaleFromM

ap}"><bookstore:area id="NAmerica" value="#{NA}" onmouseover="/template/world_namer.jpg" onmouseout="/template/world.jpg" targetImage="mapImage" /> <bookstore:area id="France" value="#{fraA}" onmouseover="/template/w

orld_france.jpg" onmouseout="/template/world.jpg" targetImage="mapImage" />

</bookstore:map>


Recommended