自定义转换器标签(3)

程序清单9-25  custom-converter/web/result.jsp

  1. <html>
2.    <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
3.    <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
4.    <%@ taglib uri="http://corejsf.com/converter" prefix="corejsf" %>
5.    <f:view>
6.       <head>
7.          <link href="styles.css" rel="stylesheet" type="text/css"/>
8.          <title><h:outputText value="#{msgs.title}"/></title>
9.       </head>
10.       <body>
11.          <h:form>
12.             <h1><h:outputText value="#{msgs.paymentInformation}"/></h1>
13.             <h:panelGrid columns="2">
14.                <h:outputText value="#{msgs.amount}"/>
15.                <h:outputText value="#{payment.amount}">
16.                   <f:convertNumber type="currency"/>
17.                </h:outputText>
18.
19.                <h:outputText value="#{msgs.creditCard}"/>
20.                <h:outputText value="#{payment.card}">
21.                   <corejsf:convertCreditcard separator="-"/>
22.                </h:outputText>
23.
24.                <h:outputText value="#{msgs.expirationDate}"/>
25.                <h:outputText value="#{payment.date}">
26.                   <f:convertDateTime pattern="MM/yyyy"/>
27.                </h:outputText>
28.             </h:panelGrid>
29.             <h:commandButton value="#{msgs.back}" action="back"/>
30.          </h:form>
31.       </body>
32.    </f:view>
33. </html>

程序清单9-26  custom-converter/src/java/com/corejsf/CreditCardConverter.java

  1. package com.corejsf;
2.
3. import java.io.Serializable;
4.
5. import javax.faces.component.UIComponent;
6. import javax.faces.context.FacesContext;
7. import javax.faces.convert.Converter;
8. import javax.faces.convert.ConverterException;
9.
10. public class CreditCardConverter implements Converter, Serializable {
11.    private String separator;
12.
13.    // PROPERTY: separator
14.    public void setSeparator(String newValue) { separator = newValue; }
15.
16.    public Object getAsObject(
17.       FacesContext context,
18.       UIComponent component,
19.       String newValue)
20.       throws ConverterException {
21.       StringBuilder builder = new StringBuilder(newValue);
22.       int i = 0;
23.       while (i < builder.length()) {
24.          if (Character.isDigit(builder.charAt(i)))
25.             i++;
26.          else
27.             builder.deleteCharAt(i);
28.       }
29.       return new CreditCard(builder.toString());
30.    }
31.
32.    public String getAsString(
33.       FacesContext context,
34.       UIComponent component,
35.       Object value)
36.       throws ConverterException {
37.       // 长度13: xxxx xxx xxx xxx
38.       // 长度14: xxxxx xxxx xxxxx
39.       // 长度15: xxxx xxxxxx xxxxx
40.       // 长度16: xxxx xxxx xxxx xxxx
41.       // 长度22: xxxxxx xxxxxxxx xxxxxxxx
42.       if (!(value instanceof CreditCard))
43.          throw new ConverterException();
44.       String v = ((CreditCard) value).toString();
45.       String sep = separator;
46.       if (sep == null) sep = " ";
47.       int[] boundaries = null;
48.       int length = v.length();
49.       if (length == 13)
50.          boundaries = new int[] { 4, 7, 10 };
51.       else if (length == 14)
52.          boundaries = new int[] { 5, 9 };
53.       else if (length == 15)
54.          boundaries = new int[] { 4, 10 };
55.       else if (length == 16)
56.          boundaries = new int[] { 4, 8, 12 };
57.       else if (length == 22)
58.          boundaries = new int[] { 6, 14 };
59.       else
60.          return v;
61.       StringBuilder result = new StringBuilder();
62.       int start = 0;
63.       for (int i = 0; i < boundaries.length; i++) {
64.          int end = boundaries[i];
65.          result.append(v.substring(start, end));
66.          result.append(sep);
67.          start = end;
68.       }
69.       result.append(v.substring(start));
70.       return result.toString();
71.    }
72. }

程序清单9-27  custom-converter/web/WEB-INF/faces-config.xml

  1. <?xml version="1.0"?>
2. <faces-config xmlns="http://java.sun.com/xml/ns/javaee"
3.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4.    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
5.         http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd"
6.    version="1.2">
7.    <navigation-rule>
8.       <from-view-id>/index.jsp</from-view-id>
9.       <navigation-case>
10.          <from-outcome>process</from-outcome>
11.          <to-view-id>/result.jsp</to-view-id>
12.       </navigation-case>
13.    </navigation-rule>
14.
15.    <navigation-rule>
16.       <from-view-id>/result.jsp</from-view-id>
17.       <navigation-case>
18.          <from-outcome>back</from-outcome>
19.          <to-view-id>/index.jsp</to-view-id>
20.       </navigation-case>
21.    </navigation-rule>
22.
23.    <converter>
24.       <converter-id>com.corejsf.CreditCard</converter-id>
25.       <converter-class>com.corejsf.CreditCardConverter </converter-class>
26.    </converter>
27.
28.    <converter>
29.       <converter-for-class>com.corejsf.CreditCard </converter-for-class>
30.       <converter-class>com.corejsf.CreditCardConverter </converter-class>
31.    </converter>
32.
33.    <managed-bean>
34.       <managed-bean-name>payment</managed-bean-name>
35.       <managed-bean-class>com.corejsf.PaymentBean </managed-bean-class>
36.       <managed-bean-scope>session</managed-bean-scope>
37.    </managed-bean>
38.
39.    <application>
40.       <resource-bundle>
41.          <base-name>com.corejsf.messages</base-name>
42.          <var>msgs</var>
43.       </resource-bundle>
44.    </application>
45. </faces-config>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值