J2EE劍俠行

          直覺我的J2EE應用生涯,打造我心中的一把利劍。

          常用鏈接

          統計

          技術鏈接

          最新評論

          用二維數組管理好你零亂的狀態、分類和其它常用選項

          ??? 很長時間沒有寫BLOG了,先啰嗦幾句。
          ??? 我已經從一家不知名的小公司又一次回到了大公司里了,這是我7年工作來又一次回歸大公司。當然了小公司有小公司的心胸狹窄和陰險,大公司有嚴格的管理制度和進升方式,這都不能夠相提并論。所以建議有能力的朋友在大公司里做事,能夠讓自己用正確的方式做正確的事。
          ?-----------------------天行鍵,君子以自強不息!
          ??? 話入正題。
          ??? 在大家的編碼過程當中,有沒有遇到過這么一種情況,很多零亂的狀態、分類和其它常用選項常常是定義死了。但是沒有一個完整的東東來約束他,在每個模塊當中使用相關的信息時,往往重新COPY一次,或者COPY過來修改一次。如果多人協作的話,務必會讓代碼變的零亂、不好管理等。
          ??? 本次主要是把一些靜態的分類、狀態或者其它常用選項使用二維數組管理起來。如果你是一個使用JSTL或者STRUTS做前臺表現的話,你就更應該好好關注了。
          ??? 以下以一個審核的狀態做示例,使用代碼實現(光說不練是假功夫).
          創建Config.java
          package?com.antbee;

          import?java.util.ArrayList;
          import?java.util.List;


          /**
          ?*?<p>
          ?*?</p>
          ?*?
          ?*?<p>
          ?*?Copyright:?版權所有?(c)?2002?-?2006
          ?*?</p>
          ?*?<p>
          ?*?Company:?Antbee
          ?*?</p>
          ?*?
          ?*?
          @author?@家軍
          ?*?
          @version?1.0
          ?
          */

          public?class?Config?{
          ????
          /**
          ?????*?客戶狀態
          ?????
          */
          ????Object?vec[][]?
          =?{?{?new?Integer(1),?new?String("申請"),new?String("apply")},
          ????????????{?
          new?Integer(2),?new?String("待審核"),new?String("auditing")?},
          ????????????{?
          new?Integer(3),?new?String("簽約"),new?String("sign?up")?},
          ????????????{?
          new?Integer(4),?new?String("續簽中"),new?String("nominator")?},
          ????????????{?
          new?Integer(5),?new?String("過期"),new?String("overdue")?}?};
          ????
          public?List?Stutas?=?GetConfigEntity(vec);????
          ????
          public?List?GetConfigEntity(Object?allconfig[][])?{
          ????????List?ConfigList?
          =?new?ArrayList();
          ??? ??? ConfigEntity first = new ConfigEntity();
          ??? ??? first.setTypeSeri(Integer.valueOf("0"));
          ??? ??? first.setTypeCnName("請選擇");
          ??? ??? first.setTypeEnName("Pls Select");
          ??? ??? ConfigList.add(first);
          ????????
          for(int?i=0;i<allconfig.length;i++){
          ????????????ConfigEntity?configEntity?
          =?new?ConfigEntity();????????????
          ????????????configEntity.setTypeSeri(Integer.valueOf(allconfig[i][
          0].toString()));
          ????????????configEntity.setTypeCnName(allconfig[i][
          1].toString());
          ????????????configEntity.setTypeEnName(allconfig[i][
          2].toString());
          ????????????ConfigList.add(configEntity);
          ????????}
          ????????
          return?ConfigList;
          ????}
          }
          對應的PO實體類ConfigEntity.java(如果有數據庫的話,你就可以直接從數據庫當中取)
          package?com.antbee;

          import?java.io.Serializable;

          public?class?ConfigEntity?implements?Serializable?{
          ???????
          /**?The?cached?hash?code?value?for?this?instance.??Settting?to?0?triggers?re-calculation.?*/
          ????
          private?int?hashValue?=?0;

          ????
          /**?The?composite?primary?key?value.?*/
          ????
          private?java.lang.Integer?typeSeri;

          ????
          /**?The?value?of?the?simple?typeName?property.?*/
          ????
          private?java.lang.String?typeCnName;
          ????
          ????
          /**?The?value?of?the?simple?typeName?property.?*/
          ????
          private?java.lang.String?typeEnName;

          ????
          /**
          ?????*?Simple?constructor?of?AbstractDicType?instances.
          ?????
          */
          ????
          public?ConfigEntity()
          ????{
          ????}

          ????
          /**
          ?????*?Constructor?of?AbstractDicType?instances?given?a?simple?primary?key.
          ?????*?
          @param?typeSeri
          ?????
          */
          ????
          public?ConfigEntity(java.lang.Integer?typeSeri)
          ????{
          ????????
          this.setTypeSeri(typeSeri);
          ????}

          ????
          /**
          ?????*?Return?the?simple?primary?key?value?that?identifies?this?object.
          ?????*?
          @return?java.lang.Integer
          ?????
          */
          ????
          public?java.lang.Integer?getTypeSeri()
          ????{
          ????????
          return?typeSeri;
          ????}

          ????
          /**
          ?????*?Set?the?simple?primary?key?value?that?identifies?this?object.
          ?????*?
          @param?typeSeri
          ?????
          */
          ????
          public?void?setTypeSeri(java.lang.Integer?typeSeri)
          ????{
          ????????
          this.hashValue?=?0;
          ????????
          this.typeSeri?=?typeSeri;
          ????}

          ????
          /**
          ?????*?Return?the?value?of?the?TYPE_NAME?column.
          ?????*?
          @return?java.lang.String
          ?????
          */
          ????
          public?java.lang.String?getTypeCnName()
          ????{
          ????????
          return?this.typeCnName;
          ????}

          ????
          /**
          ?????*?Set?the?value?of?the?TYPE_NAME?column.
          ?????*?
          @param?typeName
          ?????
          */
          ????
          public?void?setTypeCnName(java.lang.String?typeCnName)
          ????{
          ????????
          this.typeCnName?=?typeCnName;
          ????}???

          ????
          /**
          ?????*?Return?the?value?of?the?TYPE_NAME?column.
          ?????*?
          @return?java.lang.String
          ?????
          */
          ????
          public?java.lang.String?getTypeEnName()
          ????{
          ????????
          return?this.typeEnName;
          ????}

          ????
          /**
          ?????*?Set?the?value?of?the?TYPE_NAME?column.
          ?????*?
          @param?typeName
          ?????
          */
          ????
          public?void?setTypeEnName(java.lang.String?typeEnName)
          ????{
          ????????
          this.typeEnName?=?typeEnName;
          ????}?
          ????
          ????
          /**
          ?????*?Implementation?of?the?equals?comparison?on?the?basis?of?equality?of?the?primary?key?values.
          ?????*?
          @param?rhs
          ?????*?
          @return?boolean
          ?????
          */
          ????
          public?boolean?equals(Object?rhs)
          ????{
          ????????
          if?(rhs?==?null)
          ????????????
          return?false;
          ????????
          if?(!?(rhs?instanceof?ConfigEntity))
          ????????????
          return?false;
          ????????ConfigEntity?that?
          =?(ConfigEntity)?rhs;
          ????????
          if?(this.getTypeSeri()?==?null?||?that.getTypeSeri()?==?null)
          ????????????
          return?false;
          ????????
          return?(this.getTypeSeri().equals(that.getTypeSeri()));
          ????}

          ????
          /**
          ?????*?Implementation?of?the?hashCode?method?conforming?to?the?Bloch?pattern?with
          ?????*?the?exception?of?array?properties?(these?are?very?unlikely?primary?key?types).
          ?????*?
          @return?int
          ?????
          */
          ????
          public?int?hashCode()
          ????{
          ????????
          if?(this.hashValue?==?0)
          ????????{
          ????????????
          int?result?=?17;
          ????????????
          int?typeSeriValue?=?this.getTypeSeri()?==?null???0?:?this.getTypeSeri().hashCode();
          ????????????result?
          =?result?*?37?+?typeSeriValue;
          ????????????
          this.hashValue?=?result;
          ????????}
          ????????
          return?this.hashValue;
          ????}
          }
          具體使用方法
          /*Antbee?@家軍
          */
          Config?n?
          =?new?Config();
          List?View_Stutas?
          =?n.Stutas;????????request.setAttribute("View_Stutas",?View_Stutas);
          頁面當中示例:
          <c:forEach?items="${View_Stutas}"?var="View_Stutas">
          ??????????????
          <option?value="<c:out?value="${View_Stutas.typeSeri?}"?/>"><c:out?value="${View_Stutas.typeEnName}"/></option>
          ???????????
          </c:forEach>
          可以看出,在實體類里一共有三個方法,其它有兩個名字, 一個是中文名字,一個是英文名字,你可以在頁面當中截取本地語言并顯示其中一個語言就行。

          posted on 2006-09-05 13:31 @家軍 閱讀(1289) 評論(0)  編輯  收藏 所屬分類: J2EE技術類

          主站蜘蛛池模板: 琼结县| 巴楚县| 灵璧县| 河西区| 辽中县| 安岳县| 龙陵县| 含山县| 宁远县| 宣威市| 遂川县| 益阳市| 邵阳市| 谷城县| 隆回县| 萍乡市| 安庆市| 乌海市| 民和| 乐山市| 即墨市| 高阳县| 长子县| 达日县| 顺昌县| 青河县| 南乐县| 葵青区| 连江县| 丰原市| 星子县| 吴堡县| 江源县| 嘉祥县| 连山| 柳河县| 闽侯县| 莱芜市| 保康县| 申扎县| 绥阳县|