§封Ja葬va§

          2009年3月5日 #

          Junit4測試時(shí)出的問題,大家?guī)臀铱纯?/a>

          public class DrawDao extends HibernateDaoSupport {

          /**
          * 添加領(lǐng)用申請(qǐng)
          *
          * @param draw
          *            領(lǐng)用申請(qǐng)信息(不可為null)
          * @return
          */
          @SuppressWarnings("unchecked")
          public boolean insertDraw(Carddrawapply draw, List<Carddrawapplydetail> drawdetailList) {
          boolean flag = false;
          Session session = null;
          Transaction tx = null;
          try {
          session = this.getSession();
          tx = session.beginTransaction();

          long shopid = MyConstant.HTDZ_DEFAULT_SHOPID;//商家ID
          String billtype = CardConstant.CARD_LY_ID;//業(yè)務(wù)編號(hào)
          String engshort = MyConstant.HTDZ_STR;//商家英文簡稱
          String busishort = CardConstant.CARD_LY_CODE;//業(yè)務(wù)英文簡稱

          String formcode = CardCode.getFormCode(session,shopid,billtype,engshort,busishort);
          if(!"".equals(formcode)){
          draw.setCarddrawapplycode(formcode);
          draw.setSourcetype("0");
          draw.setApplydate(MyFormat.getToday());
          draw.setStatus(StatusUtil.CARDDRAWAPPLY_STATUS01);
          session.save(draw);
          long carddrawapplyid = draw.getCarddrawapplyid();
          session.flush();
          if(drawdetailList.size()==0){
          tx.rollback();
          }else{
          for(Carddrawapplydetail drawdetail : drawdetailList){
          if(drawdetail!=null){
          drawdetail.setCarddrawapplyid(carddrawapplyid);
          session.save(drawdetail);
          }
          }
          tx.commit();
          flag = true;
          }
          }

          } catch (Exception e) {
          e.printStackTrace();
          tx.rollback();
          flag = false;
          } finally {
          session.close();
          }
          return flag;
          }
          }

          以上Dao的方法已經(jīng)正常。
          對(duì)此方法的測試,我是在后期補(bǔ)上的。但是居然報(bào)錯(cuò)。幫我看看:

          Junit4:(不用繼承TestCase)

          public class TestDrawDao{

          private DrawDao drawDao;

          @Before
          public void setUp(){
          drawDao = new DrawDao();
          }


          /**
          * 測試'卡領(lǐng)用申請(qǐng)信息的添加'
          */
          @Test
          public void testInsertDraw(){
          Carddrawapply draw = new Carddrawapply();
          draw.setApplyoperid(1);
          draw.setApplydesc("申請(qǐng)說明");

          List<Carddrawapplydetail> drawdetailList = new ArrayList<Carddrawapplydetail>();

          //IC卡,100元面值的100張
          Carddrawapplydetail drawdetail1 = new Carddrawapplydetail();
          drawdetail1.setCardtype("1");
          drawdetail1.setParvalue(100);
          drawdetail1.setAmount(100);
          //充值卡,50元面值的50張
          Carddrawapplydetail drawdetail2 = new Carddrawapplydetail();
          drawdetail2.setCardtype("2");
          drawdetail2.setParvalue(50);
          drawdetail2.setAmount(50);

          drawdetailList.add(drawdetail1);
          drawdetailList.add(drawdetail2);

          boolean flag = drawDao.insertDraw(draw, drawdetailList);
          //斷言
          Assert.assertEquals(true,flag);
          }
          }

          異常如下:
          java.lang.NullPointerException
          at org.springframework.orm.hibernate3.support.HibernateDaoSupport.getSession(HibernateDaoSupport.java:142)
          at com.htdz.card.admin.dao.DrawDao.insertDraw(DrawDao.java:35)

          也就是調(diào)用到DrawDao里insertDraw()的這句時(shí)報(bào)錯(cuò):session = this.getSession();

          業(yè)務(wù)類里調(diào)用這個(gè)Dao都能正常使用,為什么從測試類里調(diào)用就報(bào)這個(gè)錯(cuò)誤?
          DrawDao繼承了HibernateDaoSupport了啊。

          posted @ 2009-04-30 10:40 §朱家二少§ 閱讀(685) | 評(píng)論 (0)編輯 收藏

          Struts2文件上傳

               摘要: 以一個(gè)圖片上傳為例子,演示了文件上傳的各個(gè)方面。包括客戶端控制,服務(wù)端控制,圖片預(yù)覽,圖片壓縮等。  閱讀全文

          posted @ 2009-04-05 13:04 §朱家二少§ 閱讀(2939) | 評(píng)論 (2)編輯 收藏

          ajax[省市區(qū)]3級(jí)聯(lián)動(dòng)

               摘要: 運(yùn)用了Ajax的服務(wù)端框架Dwr實(shí)現(xiàn)3級(jí)聯(lián)動(dòng).系統(tǒng)處于SSH架構(gòu)上   閱讀全文

          posted @ 2009-04-04 01:42 §朱家二少§ 閱讀(323) | 評(píng)論 (0)編輯 收藏

          MD5加密

          /**
          * MD5加密
          *
          * @param s
          *            要加密的字符串(不可為null)
          *
          * @return 返回加密后的密文
          */
          public final static String MD5(String pwd) {
             String Digest = "";
             try {
               MessageDigest currentAlgorithm = MessageDigest.getInstance("md5");
               currentAlgorithm.reset();
               byte[] mess = pwd.getBytes();// 獲取要加密的byte數(shù)組
               byte[] hash = currentAlgorithm.digest(mess);// 獲取加密以后的byte數(shù)組
               for (int i = 0; i < hash.length; i++) {
                  int v = hash[i];
                  if (v < 0) {
                      v = 256 + v; // 如果 v < 0 > 0 的數(shù),否則不好轉(zhuǎn)換成字符,哪有ASC代碼<0的呀
                  }
                  if (v < 16) {
                      Digest += "0"; // 如果該v<1616進(jìn)制數(shù)就是只有個(gè)位,例如15轉(zhuǎn)換就成長度就相等了
                  }
                  Digest += Integer.toString(v, 16).toUpperCase();
               }
               mess = null;
               hash = null;
             } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
             }
             return Digest;
          }

          posted @ 2009-04-04 01:28 §朱家二少§ 閱讀(229) | 評(píng)論 (0)編輯 收藏

          自定義圖形驗(yàn)證碼標(biāo)簽

          JSP頁面上引入:
          <%@ taglib uri="/htdz-tag" prefix="htdz-tag"%>
          使用自定義圖形驗(yàn)證碼標(biāo)簽自動(dòng)生成驗(yàn)證碼:
          <htdz-tag:CheckCodeTag />

          以下為詳細(xì)定義過程:
          <?xml version="1.0" encoding="UTF-8" ?>
          <taglib xmlns="http://java.sun.com/xml/ns/j2ee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
           version="2.0">
              <description>htdz tag</description>
              <display-name>htdz tag</display-name>
              <tlib-version>1.0</tlib-version>
              <short-name>htdz-tag</short-name>
              <uri>/htdz-tag</uri>

              <tag>

              <!--   
              驗(yàn)證碼控件
              使用說明:
              jsp中使用范例:
              1.無參數(shù):<htdz-tag:CheckCodeTag/>
              2.全部參數(shù):<htdz-tag:CheckCodeTag id="checkCodeImg" height="18" width="58"/>
             
              如果用戶未設(shè)置參數(shù),則默認(rèn)值分別為:height="18" width="58"
              -->
              <description>驗(yàn)證碼控件</description>
              <name>CheckCodeTag</name>
              <tag-class>com.htdz.util.tag.CheckCodeTag</tag-class>
              <body-content>JSP</body-content>
              <attribute>
                  <description>id</description>
                  <name>id</name>
                  <required>false</required>
                  <rtexprvalue>true</rtexprvalue>
              </attribute>
              <attribute>
                  <description>高</description>
                  <name>height</name>
                  <required>false</required>
                  <rtexprvalue>true</rtexprvalue>
              </attribute>
              <attribute>
                  <description>寬</description>
                  <name>width</name>
                  <required>false</required>
                  <rtexprvalue>true</rtexprvalue>
              </attribute>

              </tag>
          <taglib/>

          CheckCodeTag.java:

          public class CheckCodeTag extends TagSupport {
              private String id;
              private String height;
              private String width;

              public CheckCodeTag() {
              }

              @SuppressWarnings( { "unchecked", "static-access" })
              public int doStartTag() throws JspException {
                  StringBuffer html = new StringBuffer();
                  if (height == null || height.length() == 0)
                      height = "18";
                  if (width == null || width.length() == 0)
                      width = "60";
                  html.append("<img alt=\"重新獲取驗(yàn)證碼\"");
                  if (id != null && id.length() > 0) {
                      html.append(" id=\"");
                      html.append(id);
                      html.append("\"");
                  }
                  html.append(" height=\"");
                  html.append(height);
                  html.append("\" width=\"");
                  html.append(width);
                  html.append("\" src=\"/checkCodeImg\" onclick=\"this.src='/checkCodeImg?now='+new Date();\" style=\"cursor: pointer\" />");
                  try {
                      pageContext.getOut().println(html.toString());
                  } catch (Exception e) {
                      throw new JspException(e.getMessage());
                  }
                  return this.SKIP_BODY;
              }

              public void setId(String id) {
                  this.id = id;
              }

              public void setHeight(String height) {
                  this.height = height;
              }

              public void setWidth(String width) {
                  this.width = width;
              }
          }



          web.xml:
              <servlet>
                  <servlet-name>img</servlet-name>
                  <servlet-class>com.htdz.util.CheckCodeImg</servlet-class>
              </servlet>
              <servlet-mapping>
                  <servlet-name>img</servlet-name>
                  <url-pattern>/checkCodeImg</url-pattern>
              </servlet-mapping>


          CheckCodeImg.java:

          public class CheckCodeImg extends HttpServlet {
              private Font mFont = new Font("Arial black", Font.PLAIN, 16);

              public void init() throws ServletException {
                  super.init();
              }

              Color getRandColor(int fc, int bc) {
                  Random random = new Random();
                  if (fc > 255)
                      fc = 255;
                  if (bc > 255)
                      bc = 255;
                  int r = fc + random.nextInt(bc - fc);
                  int g = fc + random.nextInt(bc - fc);
                  int b = fc + random.nextInt(bc - fc);
                  return new Color(r, g, b);
              }

              public void service(HttpServletRequest request, HttpServletResponse response)
                  throws ServletException, IOException {
                  response.setHeader("Pragma", "No-cache");
                  response.setHeader("Cache-Control", "no-cache");
                  response.setDateHeader("Expires", 0);
                  response.setContentType("image/png");

                  int width = 60, height = 18;
                  BufferedImage image = new BufferedImage(width, height,
                  BufferedImage.TYPE_INT_RGB);

                  Graphics g = image.getGraphics();
                  Random random = new Random();
                  g.setColor(getRandColor(200, 250));
                  g.fillRect(1, 1, width - 1, height - 1);
                  g.setColor(new Color(102, 102, 102));
                  g.drawRect(0, 0, width - 1, height - 1);
                  g.setFont(mFont);

                  g.setColor(getRandColor(160, 200));
                  for (int i = 0; i < 155; i++) {
                      int x = random.nextInt(width - 1);
                      int y = random.nextInt(height - 1);
                      int xl = random.nextInt(6) + 1;
                      int yl = random.nextInt(12) + 1;
                      g.drawLine(x, y, x + xl, y + yl);
                  }
                  for (int i = 0; i < 70; i++) {
                      int x = random.nextInt(width - 1);
                      int y = random.nextInt(height - 1);
                      int xl = random.nextInt(12) + 1;
                      int yl = random.nextInt(6) + 1;
                      g.drawLine(x, y, x - xl, y - yl);
                  }

                  char[] codes = { // 'a','b','c','d','e','f','h','k','m','n','r','s','t','x','y',//15個(gè)
                                               '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
                                           // 'A','B','C','D','E','F','G','H','K','N','S','T','X','Y'//14
                  };
                  String sRand = "";
                  for (int i = 0; i < 4; i++) {
                      int j = random.nextInt(10);
                      String tmp = String.valueOf(codes[j]);
                      sRand += tmp;
                      g.setColor(new Color(20 + random.nextInt(110), 20 + random
                      .nextInt(110), 20 + random.nextInt(110)));
                      g.drawString(tmp, 15 * i + 2, 15);
                  }

                  HttpSession session = request.getSession(true);
                  session.setAttribute(MyConstant.SESSION_CHECKCODE, sRand);
                  g.dispose();
                  ImageIO.write(image, "PNG", response.getOutputStream());
             }
          }

          posted @ 2009-04-04 00:52 §朱家二少§ 閱讀(422) | 評(píng)論 (1)編輯 收藏

          自定義分頁標(biāo)簽

               摘要: 近期在做的S2SH項(xiàng)目,因?yàn)槎嗵幱玫椒猪摚珺OSS要求小弟將其抽象出來。小弟不才,實(shí)際參與開發(fā)的經(jīng)驗(yàn)也就1年。
          于是花了點(diǎn)時(shí)間將其做成自定義標(biāo)簽供所有需要分頁的業(yè)務(wù)調(diào)用。小結(jié)一下,供新手參考。  閱讀全文

          posted @ 2009-04-04 00:23 §朱家二少§ 閱讀(3484) | 評(píng)論 (5)編輯 收藏

          Dwr使用引導(dǎo)

               摘要: DWR(Direct Web Remoting)是Ajax的服務(wù)端框架,它是一個(gè)開源的類庫,可以幫助開發(fā)人員開發(fā)包含AJAX技術(shù)的網(wǎng)站.它可以允許在瀏覽器里的代碼使用運(yùn)行在WEB服務(wù)器上的JAVA函數(shù),就像它就在瀏覽器里一樣.
            閱讀全文

          posted @ 2009-03-08 00:38 §朱家二少§ 閱讀(408) | 評(píng)論 (0)編輯 收藏

          Dom高級(jí)樣式編程

               摘要: 1.內(nèi)聯(lián)樣式:(直接通過HTML的style特性來分配的樣式)
          Dom采用style對(duì)象來管理元素的CSS樣式。
          2.訪問樣式表
          使用style對(duì)象可以方便地獲取某個(gè)有style特性的元素的CSS樣式。但它無法表示由CSS
          規(guī)則或在style特性外部定義類所定義的元素CSS樣式。

            閱讀全文

          posted @ 2009-03-06 09:13 §朱家二少§ 閱讀(275) | 評(píng)論 (0)編輯 收藏

          Dom技術(shù)基礎(chǔ)

               摘要: DOM Level1:對(duì)文檔節(jié)點(diǎn)進(jìn)行訪問以及增,刪,改
          Node接口定義了一些所有節(jié)點(diǎn)類型都包含的特性和方法
            閱讀全文

          posted @ 2009-03-06 09:11 §朱家二少§ 閱讀(420) | 評(píng)論 (0)編輯 收藏

          Dwr過濾器與異常處理

               摘要:





            閱讀全文

          posted @ 2009-03-05 23:06 §朱家二少§ 閱讀(1532) | 評(píng)論 (0)編輯 收藏

          Dwr批處理

               摘要: 你可以使用batch來批量的執(zhí)行遠(yuǎn)程調(diào)用。這樣可以減少與服務(wù)器的交互次數(shù),所以可以提交反應(yīng)速度。
          一個(gè)batch以 DWREngine.beginBatch() 開始 ,并以 DWREngine.endBatch() 結(jié)束。當(dāng)
          DWREngine.endBatch() 被調(diào)用,我們就結(jié)束了遠(yuǎn)程調(diào)用的分組,這樣DWR就在一次與服務(wù)器的交互中
          執(zhí)行它們。
            閱讀全文

          posted @ 2009-03-05 22:59 §朱家二少§ 閱讀(228) | 評(píng)論 (0)編輯 收藏

          Spring聲明式事務(wù)管理

               摘要: Spring對(duì)聲明式事務(wù)管理的支持式通過它的AOP框架來實(shí)現(xiàn)的。
          要在Spring應(yīng)用中使用聲明式事務(wù),你得用TransactionProxyFactoryBean。
          它的目的式將方法包裝在事務(wù)的上下文中。  閱讀全文

          posted @ 2009-03-05 22:48 §朱家二少§ 閱讀(249) | 評(píng)論 (0)編輯 收藏

          S2SH項(xiàng)目搭建流程

               摘要: 08年年底開始,公司新項(xiàng)目開始采用struts2了。往后大部分項(xiàng)目采用Strust2,Hibernate,Spring架構(gòu)。
          項(xiàng)目中忙里偷閑,整理點(diǎn)東西供分享(不完善之處將盡快修補(bǔ)):  閱讀全文

          posted @ 2009-03-05 22:36 §朱家二少§ 閱讀(4044) | 評(píng)論 (6)編輯 收藏

          Spring定時(shí)器

               摘要: Java的Timer類和OpenSymphony的Quartz調(diào)度器是兩個(gè)流行的調(diào)度API。Spring為這兩個(gè)調(diào)度器提供了一個(gè)抽象層,使你可以更容易地使用它們。
          簡單總結(jié)下在Spring架構(gòu)下,Java Timer調(diào)度任務(wù)的使用  閱讀全文

          posted @ 2009-03-05 14:26 §朱家二少§ 閱讀(609) | 評(píng)論 (0)編輯 收藏

          HQL查詢必知

               摘要: 不知道HQL查詢返回類型著實(shí)給Hibernate新手帶來了不少麻煩。這里我簡單對(duì)其做了個(gè)總結(jié)。希望能幫上點(diǎn)忙。  閱讀全文

          posted @ 2009-03-05 14:20 §朱家二少§ 閱讀(191) | 評(píng)論 (0)編輯 收藏

          Struts2國際化

               摘要: struts2的國際化分三種情況:前臺(tái)頁面的國際化,Action類中的國際化,配置文件的國際化。
          首先指定全局的國際化資源文件:
            閱讀全文

          posted @ 2009-03-05 10:38 §朱家二少§ 閱讀(695) | 評(píng)論 (0)編輯 收藏

          Struts2核心攔截器

               摘要: 當(dāng)struts2經(jīng)過輸入校驗(yàn)階段(struts2系列---輸入校驗(yàn))后,接著將調(diào)用execute()業(yè)務(wù)邏輯方法。有時(shí)我們需要在執(zhí)行execute()方法之前先進(jìn)行比如登陸驗(yàn)證之類的判斷。只有登陸過的用戶才能進(jìn)行業(yè)務(wù)操作。這時(shí)就需要用到Struts2的攔截器Interceptor。  閱讀全文

          posted @ 2009-03-05 10:35 §朱家二少§ 閱讀(433) | 評(píng)論 (0)編輯 收藏

          Struts2輸入校驗(yàn)

               摘要: Struts2提供了客戶端校驗(yàn)與服務(wù)端校驗(yàn)。這里只是做個(gè)簡易的總結(jié)。

          1.)服務(wù)端校驗(yàn):
          當(dāng)程序流程進(jìn)入到Action類,并經(jīng)歷了struts2的類型轉(zhuǎn)換階段后。接著進(jìn)入struts2的輸入校驗(yàn)階段。
          struts2的輸入校驗(yàn)可自己編碼進(jìn)行校驗(yàn),也可通過有規(guī)則命名的配置文件進(jìn)行校驗(yàn)。
            閱讀全文

          posted @ 2009-03-05 10:31 §朱家二少§ 閱讀(1090) | 評(píng)論 (0)編輯 收藏

          主站蜘蛛池模板: 平昌县| 永川市| 青岛市| 舒兰市| 海城市| 揭东县| 高碑店市| 永泰县| 济宁市| 壤塘县| 惠来县| 伊金霍洛旗| 台州市| 哈巴河县| 集安市| 大方县| 铁力市| 容城县| 浮梁县| 萨迦县| 泰和县| 镇原县| 乡宁县| 张家港市| 五家渠市| 大城县| 太仆寺旗| 广东省| 荔浦县| 濮阳市| 永川市| 怀集县| 四川省| 冕宁县| 团风县| 龙口市| 白山市| 威宁| 平南县| 曲阳县| 松潘县|