Java Enum 學習篇:
http://blog.csdn.net/abing37/archive/2009/07/29/4391222.aspx
以前做項目都是使用final Static定義狀態,這樣做也能滿足要求,當狀態代碼發生改變的時候只需改一處地方,剛才聽同事說可以使用enum來定義常量,更加優雅,所以決定學習下。上面這篇部落格介紹了enum的入門和原理,我將把同事做的jstl自訂標籤展示出來,積累下知識
1.定義enum-HvProcStateEnum
public enum HvProcStateEnum {</p><p>TOCHECK("90","待覆核"),TOUPDATE("93","待修改"),TOGRANT("92","待授權"),<br />TOSEND("03","待發送"),SENDED("04","已發送"),APIOK("05","API成功"),<br />APIFAIL("06","AIP拒絕"); //.................其他暫略</p><p>private String code;<br />private String descript;</p><p>private HvProcStateEnum(String code,String descript){<br />this.code = code;<br />this.descript = descript;<br />}<br />public String getCode() {<br />return code;<br />}<br />public void setCode(String code) {<br />this.code = code;<br />}<br />public String getDescript() {<br />return descript;<br />}<br />public void setDescript(String descript) {<br />this.descript = descript;<br />}</p><p>}
2.state.tld
<?xml version="1.0" encoding="UTF-8" ?> </p><p><taglib xmlns="http://java.sun.com/xml/ns/j2ee"<br /> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<br /> xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"<br /> version="2.0"></p><p><tlib-version>1.1</tlib-version><br /><short-name>state</short-name><br /><uri>/test.state</uri> </p><p><function><br /><description>大額往賬匯兌業務中文處理狀態</description><br /><name>hvsendproc</name><br /><function-class>com.test.util.StateUtil</function-class><br /><function-signature><br /> java.lang.String getHvCnProc(java.lang.String)<br /> </function-signature><br /> <example>${state:getHvCnProc(value)}</example><br /></function></p><p></taglib>
3.處理類StateUtil
public class StateUtil {</p><p>/*<br /> * 返回大額往賬匯兌業務處理狀態中文描述<br /> */<br />public static String getHvCnProc(String code){<br />String retValue = "未知";<br />if(code != null){<br />code = code.trim();<br />if("".equals(code)){<br />return retValue;<br />}<br />}else{<br />return retValue;<br />}</p><p>for(HvProcStateEnum en: HvProcStateEnum.values()){<br />if(en.getCode().equals(code)){<br />retValue = en.getDescript();<br />break;<br />}<br />}</p><p>return retValue;<br />}</p><p>}<br />
4.在web.xml註冊tld
<jsp-config><br /> <taglib><br /> <taglib-uri>/test.state</taglib-uri><br /><taglib-location>/WEB-INF/tld/state.tld</taglib-location><br /></taglib><br /></jsp-config>
5.使用jstl自訂標籤
jsp裡加入
<%@ taglib prefix="state" uri="/test.state" %>
然後
${state:hvsendproc(hvSndExchglist.procstate)}