java enum 結合jstl自訂標籤做展示

來源:互聯網
上載者:User

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)}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.