⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 creditcardconverter.java

📁 struts框架的jsf组件的核心实用例子集合
💻 JAVA
字号:
package com.corejsf;import java.io.Serializable;import javax.faces.component.UIComponent;import javax.faces.context.FacesContext;import javax.faces.convert.Converter;import javax.faces.convert.ConverterException;public class CreditCardConverter implements Converter, Serializable {   private String separator;   // PROPERTY: separator   public void setSeparator(String newValue) { separator = newValue; }   public Object getAsObject(      FacesContext context,      UIComponent component,      String newValue)      throws ConverterException {      StringBuffer buffer = new StringBuffer(newValue);      int i = 0;      while (i < buffer.length()) {         if (Character.isDigit(buffer.charAt(i)))            i++;         else            buffer.deleteCharAt(i);      }      return new CreditCard(buffer.toString());   }   public String getAsString(      FacesContext context,      UIComponent component,      Object value)      throws ConverterException {      // length 13: xxxx xxx xxx xxx      // length 14: xxxxx xxxx xxxxx      // length 15: xxxx xxxxxx xxxxx      // length 16: xxxx xxxx xxxx xxxx      // length 22: xxxxxx xxxxxxxx xxxxxxxx      if (!(value instanceof CreditCard))         throw new ConverterException();            String v = ((CreditCard) value).toString();      String sep = separator;      if (sep == null) sep = " ";      int[] boundaries = null;      int length = v.length();      if (length == 13)         boundaries = new int[] { 4, 7, 10 };      else if (length == 14)         boundaries = new int[] { 5, 9 };      else if (length == 15)         boundaries = new int[] { 4, 10 };      else if (length == 16)         boundaries = new int[] { 4, 8, 12 };      else if (length == 22)         boundaries = new int[] { 6, 14 };      else         return v;      StringBuffer result = new StringBuffer();      int start = 0;      for (int i = 0; i < boundaries.length; i++) {         int end = boundaries[i];         result.append(v.substring(start, end));         result.append(sep);         start = end;      }      result.append(v.substring(start));      return result.toString();   }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -