§封Ja葬va§

2009年3月8日 #

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 §朱家二少§ 阅读(676) | 评论 (0)编辑 收藏

Struts2文件上传

     摘要: 以一个图片上传为例子,演示了文件上传的各个方面。包括客户端控制,服务端控制,图片预览,图片压缩等。  阅读全文

posted @ 2009-04-05 13:04 §朱家二少§ 阅读(2929) | 评论 (2)编辑 收藏

ajax[省市区]3级联动

     摘要: 运用了Ajax的服务端框架Dwr实现3级联动.系统处于SSH架构上   阅读全文

posted @ 2009-04-04 01:42 §朱家二少§ 阅读(314) | 评论 (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 §朱家二少§ 阅读(220) | 评论 (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 §朱家二少§ 阅读(408) | 评论 (1)编辑 收藏

自定义分页标签

     摘要: 近期在做的S2SH项目,因为多处用到分页,BOSS要求小弟将其抽象出来。小弟不才,实际参与开发的经验也就1年。
于是花了点时间将其做成自定义标签供所有需要分页的业务调用。小结一下,供新手参考。  阅读全文

posted @ 2009-04-04 00:23 §朱家二少§ 阅读(3466) | 评论 (5)编辑 收藏

Dwr使用引导

     摘要: DWR(Direct Web Remoting)是Ajax的服务端框架,它是一个开源的类库,可以帮助开发人员开发包含AJAX技术的网站.它可以允许在浏览器里的代码使用运行在WEB服务器上的JAVA函数,就像它就在浏览器里一样.
  阅读全文

posted @ 2009-03-08 00:38 §朱家二少§ 阅读(400) | 评论 (0)编辑 收藏