§封Ja葬va§

          2009年3月5日 #

          Junit4測試時出的問題,大家幫我看看

          public class DrawDao extends HibernateDaoSupport {

          /**
          * 添加領用申請
          *
          * @param draw
          *            領用申請信息(不可為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;//業務編號
          String engshort = MyConstant.HTDZ_STR;//商家英文簡稱
          String busishort = CardConstant.CARD_LY_CODE;//業務英文簡稱

          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的方法已經正常。
          對此方法的測試,我是在后期補上的。但是居然報錯。幫我看看:

          Junit4:(不用繼承TestCase)

          public class TestDrawDao{

          private DrawDao drawDao;

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


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

          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)

          也就是調用到DrawDao里insertDraw()的這句時報錯:session = this.getSession();

          業務類里調用這個Dao都能正常使用,為什么從測試類里調用就報這個錯誤?
          DrawDao繼承了HibernateDaoSupport了啊。

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

          Struts2文件上傳

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

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

          ajax[省市區]3級聯動

               摘要: 運用了Ajax的服務端框架Dwr實現3級聯動.系統處于SSH架構上   閱讀全文

          posted @ 2009-04-04 01:42 §朱家二少§ 閱讀(327) | 評論 (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數組
               byte[] hash = currentAlgorithm.digest(mess);// 獲取加密以后的byte數組
               for (int i = 0; i < hash.length; i++) {
                  int v = hash[i];
                  if (v < 0) {
                      v = 256 + v; // 如果 v < 0 > 0 的數,否則不好轉換成字符,哪有ASC代碼<0的呀
                  }
                  if (v < 16) {
                      Digest += "0"; // 如果該v<1616進制數就是只有個位,例如15轉換就成長度就相等了
                  }
                  Digest += Integer.toString(v, 16).toUpperCase();
               }
               mess = null;
               hash = null;
             } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
             }
             return Digest;
          }

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

          自定義圖形驗證碼標簽

          JSP頁面上引入:
          <%@ taglib uri="/htdz-tag" prefix="htdz-tag"%>
          使用自定義圖形驗證碼標簽自動生成驗證碼:
          <htdz-tag:CheckCodeTag />

          以下為詳細定義過程:
          <?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>

              <!--   
              驗證碼控件
              使用說明:
              jsp中使用范例:
              1.無參數:<htdz-tag:CheckCodeTag/>
              2.全部參數:<htdz-tag:CheckCodeTag id="checkCodeImg" height="18" width="58"/>
             
              如果用戶未設置參數,則默認值分別為:height="18" width="58"
              -->
              <description>驗證碼控件</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=\"重新獲取驗證碼\"");
                  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個
                                               '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 §朱家二少§ 閱讀(427) | 評論 (1)編輯 收藏

          自定義分頁標簽

               摘要: 近期在做的S2SH項目,因為多處用到分頁,BOSS要求小弟將其抽象出來。小弟不才,實際參與開發的經驗也就1年。
          于是花了點時間將其做成自定義標簽供所有需要分頁的業務調用。小結一下,供新手參考。  閱讀全文

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

          Dwr使用引導

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

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

          Dom高級樣式編程

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

            閱讀全文

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

          Dom技術基礎

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

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

          Dwr過濾器與異常處理

               摘要:





            閱讀全文

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

          Dwr批處理

               摘要: 你可以使用batch來批量的執行遠程調用。這樣可以減少與服務器的交互次數,所以可以提交反應速度。
          一個batch以 DWREngine.beginBatch() 開始 ,并以 DWREngine.endBatch() 結束。當
          DWREngine.endBatch() 被調用,我們就結束了遠程調用的分組,這樣DWR就在一次與服務器的交互中
          執行它們。
            閱讀全文

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

          Spring聲明式事務管理

               摘要: Spring對聲明式事務管理的支持式通過它的AOP框架來實現的。
          要在Spring應用中使用聲明式事務,你得用TransactionProxyFactoryBean。
          它的目的式將方法包裝在事務的上下文中。  閱讀全文

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

          S2SH項目搭建流程

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

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

          Spring定時器

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

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

          HQL查詢必知

               摘要: 不知道HQL查詢返回類型著實給Hibernate新手帶來了不少麻煩。這里我簡單對其做了個總結。希望能幫上點忙。  閱讀全文

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

          Struts2國際化

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

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

          Struts2核心攔截器

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

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

          Struts2輸入校驗

               摘要: Struts2提供了客戶端校驗與服務端校驗。這里只是做個簡易的總結。

          1.)服務端校驗:
          當程序流程進入到Action類,并經歷了struts2的類型轉換階段后。接著進入struts2的輸入校驗階段。
          struts2的輸入校驗可自己編碼進行校驗,也可通過有規則命名的配置文件進行校驗。
            閱讀全文

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

          主站蜘蛛池模板: 西青区| 无棣县| 开鲁县| 蒙自县| 徐闻县| 兴化市| 图木舒克市| 榆树市| 得荣县| 孟村| 正镶白旗| 富顺县| 谢通门县| 图们市| 阿坝县| 拜城县| 田阳县| 宜君县| 囊谦县| 泰州市| 砀山县| 德清县| 滕州市| 普兰店市| 绥芬河市| 巴林左旗| 牙克石市| 屏南县| 安泽县| 宜川县| 孝昌县| 广灵县| 小金县| 吉水县| 宽城| 根河市| 湘潭市| 礼泉县| 乌拉特前旗| 建宁县| 沙坪坝区|