新建项目:
所需建文件如下:(参考最近一次jsf文档)
1.新建四个以.xhml后缀结尾的文件名,以及一个xml的配置文件。
2.在mybeans包中建立2个jsf受管bean:一个用于存放功能函数,一个用于将inputtext中的中文输入与读取(虽然现在读取仍然为乱码)
代码内容:
在end.xhtml文件中:
1 <?xml version='1.0' encoding='UTF-8' ?> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3 <html xmlns="http://www.w3.org/1999/xhtml" 4 xmlns:h="http://xmlns.jcp.org/jsf/html" 5 xmlns:f="http://xmlns.jcp.org/jsf/core"> 6 7 <h:head> 8 <title>Facelet Title</title> 9 </h:head> 10 11 <h:body> 12 <h:form> 13 <p>请留下您对本店商品服务的评价</p> 14 <h:inputText value="#{orderBean.fankui3}" required="true" styleClass="input" > 15 <f:converter converterId="stringconverter"/> <!--注:这里不是EL表达式--> 16 </h:inputText> 17 <h1><p>欢迎您的再次光临,祝您购物愉快!</p></h1> 18 19 <h:commandButton value="再次购物" action="returnFromOrderFlow"/><!--结束,退出--> 20 </h:form> 21 </h:body> 22 </html>
在order-address.xhtml文件中:
1 <?xml version='1.0' encoding='UTF-8' ?> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3 <html xmlns="http://www.w3.org/1999/xhtml" 4 xmlns:h="http://xmlns.jcp.org/jsf/html" 5 xmlns:f="http://xmlns.jcp.org/jsf/core"> 6 <h:head> 7 <title>order-address</title> 8 </h:head> 9 <h:body> 10 <h:form> 11 12 <div class="text" style=" text-align:center;"><h1>欢迎来到爱群小店</h1></div> 13 <hr align="center" width="10%" /> 14 <h2>请完善您的订单信息</h2> 15 <p>A壁纸 B地毯 C软包 D海绵</p> 16 <h3><h:outputText value="您所需要的商品 #{orderBean.itemName}"/></h3> 17 <h3><h:outputText value="您所需要的数量#{orderBean.itemCount}"/></h3> 18 <br/> 19 20 <br/> 21 <hr align="center" width="100%" /> 22 <h3><h:outputText value ="请填写您的收货地址::"/></h3> 23 <h:inputText value="#{orderBean.address}"/> 24 <br/> 25 <p>备注</p> 26 <h:inputText id="account3" value="#{orderBean.fankui2}" required="true" styleClass="input" > 27 <f:converter converterId="stringconverter"/> <!--注:这里不是EL表达式--> 28 </h:inputText> 29 <br/> 30 31 <!--将itemAddress做一对一的绑定--> 32 <br/><br/> 33 <h:commandButton value="下一步" action="order-confirmation"/><!--下一个页面--> 34 35 </h:form> 36 </h:body> 37 </html>
在order-confirmation.xhtml文件中:
1 <?xml version='1.0' encoding='UTF-8' ?> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3 <html xmlns="http://www.w3.org/1999/xhtml" 4 xmlns:h="http://xmlns.jcp.org/jsf/html" 5 xmlns:f="http://xmlns.jcp.org/jsf/core"> 6 <h:head> 7 <title>order-confirmation</title> 8 </h:head> 9 <h:body> 10 <h:form> 11 12 <div class="text" style=" text-align:center;"><h1>欢迎来到爱群小店</h1></div> 13 <h1>核对您的订单信息</h1> 14 <p>A壁纸 B地毯 C软包 D海绵</p> 15 <h2><h:outputText value="您所订购的商品:#{orderBean.itemName} "></h:outputText> </h2> 16 <br/> 17 <h2><h:outputText value="您所需要的数量:#{orderBean.itemCount}"/></h2> 18 <br/> 19 <br/> 20 <br/> 21 <h3><h:outputText value ="您的收货地址是::#{orderBean.address}"/></h3> 22 <p>您希望何时到货</p> 23 <h:inputText id="account2" value="#{orderBean.fankui1}" required="true" styleClass="input" > 24 <f:converter converterId="stringconverter"/> <!--注:这里不是EL表达式--> 25 </h:inputText> 26 27 <br/><br/> 28 <h:commandButton value="提交订单" action="end"/><!--跳转结束页面--> 29 </h:form> 30 </h:body> 31 </html>
在order-flow.xml文件中:
1 <?xml version='1.0' encoding='UTF-8'?> 2 <faces-config version="2.2" 3 xmlns="http://xmlns.jcp.org/xml/ns/javaee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"> 6 7 <flow-definition id="order"> 8 9 <flow-return id="returnFromOrderFlow"><!--决定最后标识符的位置--> 10 <from-outcome> 11 #{orderBean.returnValue}<!--由此处的返回值决定跳转位置--> 12 </from-outcome> 13 </flow-return> 14 <converter> 15 <converter-id>stringconverter</converter-id> 16 <converter-class>myBeans.StringConverter</converter-class> 17 </converter> 18 </flow-definition> 19 20 </faces-config>
在ordere.xhtml文件中:
1 <?xml version='1.0' encoding='UTF-8' ?> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3 <html xmlns="http://www.w3.org/1999/xhtml" 4 xmlns:h="http://xmlns.jcp.org/jsf/html" 5 xmlns:f="http://xmlns.jcp.org/jsf/core"> 6 7 8 9 <h:head> 10 <title>Select item quantity</title> 11 </h:head> 12 13 14 <h:body> 15 <!----> 16 <h:form><div class="text" style=" text-align:center;"><h1>欢迎来到爱群小店</h1></div> 17 18 <h2>填写您所需要的商品</h2> 19 <p>A.柯尼塞格one1Koenigsegg B.LykanHypersport C.迈巴赫exelero D.阿斯顿马丁 one-77</p> 20 <p><h:outputText value="商品名称:"/></p> 21 <h:inputText value="#{orderBean.itemName}" > 22 23 </h:inputText> 24 25 <!--将itemname做一对一的绑定--> 26 <br/> 27 <p> <h:outputText value ="订购数量:"/></p> 28 <h:inputText value="#{orderBean.itemCount}"/><!--将itemCount做一对一的绑定--> 29 <br/><br/> 30 <h2>请填写您所需要的个性化配置</h2> 31 <h:inputText id="account1" value="#{orderBean.fankui0}" required="true" styleClass="input" > 32 <f:converter converterId="stringconverter"/> <!--注:这里不是EL表达式--> 33 </h:inputText> 34 <br/> 35 <h:commandButton value="下一步" action="order-address"/><!--输入地址--> 36 </h:form> 37 </h:body> 38 </html>
在OrderBean.java文件中:
1 /* 2 * To change this license header, choose License Headers in Project Properties. 3 * To change this template file, choose Tools | Templates 4 * and open the template in the editor. 5 */ 6 package myBeans; 7 8 import javax.inject.Named; 9 import javax.enterprise.context.SessionScoped; 10 import javax.inject.Named; 11 import javax.faces.flow.FlowScoped; 12 import java.io.Serializable; 13 import javax.annotation.ManagedBean; 14 15 /** 16 * 17 * @author Administrator 18 */ 19 @Named 20 @FlowScoped("order") 21 22 public class OrderBean implements Serializable { 23 24 /** 25 * Creates a new instance of OrderBean 26 */ 27 private String itemCount; 28 private String address; 29 private String itemName; 30 private String fankui0; 31 private String fankui1; 32 private String fankui2; 33 private String fankui3; 34 private String fankui4; 35 public String getReturnValue(){ 36 return "/index"; 37 //“/”:相对的绝对路径 38 } 39 public OrderBean() { 40 41 } 42 43 public String getItemCount() { 44 return itemCount; 45 } 46 47 public void setItemCount(String itemCount) { 48 this.itemCount = itemCount; 49 } 50 51 public String getAddress() { 52 return address; 53 } 54 55 public void setAddress(String address) { 56 this.address = address; 57 } 58 59 public String getItemName() { 60 61 return itemName; 62 } 63 64 public void setItemName(String itemName) { 65 this.itemName = itemName; 66 } 67 68 public String getFankui0() { 69 return fankui0; 70 } 71 72 public void setFankui0(String fankui0) { 73 this.fankui0 = fankui0; 74 } 75 76 public String getFankui1() { 77 return fankui1; 78 } 79 80 public void setFankui1(String fankui1) { 81 this.fankui1 = fankui1; 82 } 83 84 public String getFankui2() { 85 return fankui2; 86 } 87 88 public void setFankui2(String fankui2) { 89 this.fankui2 = fankui2; 90 } 91 92 public String getFankui3() { 93 return fankui3; 94 } 95 96 public void setFankui3(String fankui3) { 97 this.fankui3 = fankui3; 98 } 99 100 public String getFankui4() { 101 return fankui4; 102 } 103 104 public void setFankui4(String fankui4) { 105 this.fankui4 = fankui4; 106 } 107 108 109 110 }
在StringConverter.java文件中:
1 /* 2 * To change this license header, choose License Headers in Project Properties. 3 * To change this template file, choose Tools | Templates 4 * and open the template in the editor. 5 */ 6 package myBeans; 7 8 /** 9 * 10 * @author Administrator 11 */ 12 import java.io.UnsupportedEncodingException; 13 import java.util.Map; 14 import javax.faces.component.UIComponent; 15 import javax.faces.component.UIInput; 16 import javax.faces.convert.Converter; 17 import javax.faces.context.FacesContext; 18 import javax.faces.convert.ConverterException; 19 20 public class StringConverter implements Converter { 21 public Object getAsObject(FacesContext context, UIComponent component, 22 String newValues) throws ConverterException { 23 String newstr = ""; 24 if (newValues == null) { 25 newValues = ""; 26 } 27 byte[] byte1 = null; 28 try { 29 byte1 = newValues.getBytes("ISO-8859-1"); 30 newstr = new String(byte1, "GB2312"); 31 UIInput input=(UIInput)component;// 32 input.setSubmittedValue(newstr); 33 } 34 catch (UnsupportedEncodingException e) { 35 e.printStackTrace(); 36 } 37 38 return newstr; 39 40 } 41 42 public String getAsString(FacesContext context, UIComponent component, 43 Object Values) throws ConverterException { 44 return (String) Values; 45 } 46 }