首页上一页 1 下一页尾页 1 条记录 1/1页
hibernate应用开发完全手册中物资管理系统的疑问
发表在JavaWeb图书答疑
2009-07-16
是否精华
是
否
版块置顶:
是
否
在书中18章的物资管理系统中,按照书上指导的实现步骤,在添加新系统用户的时候不论添加成功还是失败都提示该用户信息已经存在关键的代码如下
public ActionForward userAdd(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
UserForm userForm = (UserForm) form;
System.out.println("表单中获取的Instorage:"+userForm.getSetInstorage());
int rtn=userDAO.insert(userForm);//为什么这里在一次插入成功之后会进行第二次插入?
if(rtn==2){
request.setAttribute("error","该用户信息已经存在!");//为什么插入新用户信息之后这里依然会执行,而且是在下一个条件判断成功之后再次执行
System.out.print(rtn);
System.out.println("error");
return mapping.findForward("error");
}else if(rtn==1){
request.setAttribute("error","该用户信息添加成功!");
System.out.print(rtn);
System.out.println("success");
return mapping.findForward("useraddok");//插入新用户信息这里没有进行页面跳转,WHY?
}else{
request.setAttribute("error","用户信息添加失败!");
return mapping.findForward("error");
}
在struts.xml文件中对应跳转配置为<forward name="useraddok" path="/user_ok.jsp?para=1" />
这里是userDAO中的相应操作实现
private Session session = null;
public List query(String strif) {
session=MySession.openSession(); //打开Session
String hql = "";
if (strif != "all" && strif != null && strif != "") { //条件查询
hql = "FROM UserForm user WHERE " + strif +
"";
} else { //查询全部数据
hql = "FROM UserForm user";
}
System.out.println(hql);
List list=null;
try{
Query query = session.createQuery(hql);
list = query.list();
}catch(Exception e){
e.printStackTrace();
}finally{
if (session != null && session.isOpen()) {
MySession.closeSession(session);
}//关闭Session
}
return list;
}
//保存用户信息
public int insert(UserForm userForm) {
int ret = 0;
Transaction tx = null;
String str="name='" + userForm.getName() + "'";
List list = query(str);
if (list.size()> 0) { //存在该信息
ret = 2;
} else {
session=MySession.openSession(); //打开Session
try {
tx = session.beginTransaction();
session.save(userForm);
tx.commit();
ret = 1;
} catch (Exception e) {
if (tx != null) {
tx.rollback();//为什么要进行tx.rollback操作,能实现什么?
}
e.printStackTrace();
System.out.println("添加用户信息时的错误信息:" + e.getMessage());
return ret = 0;
} finally {
if (session != null && session.isOpen()) {
MySession.closeSession(session);
}//关闭Session
}
}
return ret;
}
public ActionForward userAdd(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
UserForm userForm = (UserForm) form;
System.out.println("表单中获取的Instorage:"+userForm.getSetInstorage());
int rtn=userDAO.insert(userForm);//为什么这里在一次插入成功之后会进行第二次插入?
if(rtn==2){
request.setAttribute("error","该用户信息已经存在!");//为什么插入新用户信息之后这里依然会执行,而且是在下一个条件判断成功之后再次执行
System.out.print(rtn);
System.out.println("error");
return mapping.findForward("error");
}else if(rtn==1){
request.setAttribute("error","该用户信息添加成功!");
System.out.print(rtn);
System.out.println("success");
return mapping.findForward("useraddok");//插入新用户信息这里没有进行页面跳转,WHY?
}else{
request.setAttribute("error","用户信息添加失败!");
return mapping.findForward("error");
}
在struts.xml文件中对应跳转配置为<forward name="useraddok" path="/user_ok.jsp?para=1" />
这里是userDAO中的相应操作实现
private Session session = null;
public List query(String strif) {
session=MySession.openSession(); //打开Session
String hql = "";
if (strif != "all" && strif != null && strif != "") { //条件查询
hql = "FROM UserForm user WHERE " + strif +
"";
} else { //查询全部数据
hql = "FROM UserForm user";
}
System.out.println(hql);
List list=null;
try{
Query query = session.createQuery(hql);
list = query.list();
}catch(Exception e){
e.printStackTrace();
}finally{
if (session != null && session.isOpen()) {
MySession.closeSession(session);
}//关闭Session
}
return list;
}
//保存用户信息
public int insert(UserForm userForm) {
int ret = 0;
Transaction tx = null;
String str="name='" + userForm.getName() + "'";
List list = query(str);
if (list.size()> 0) { //存在该信息
ret = 2;
} else {
session=MySession.openSession(); //打开Session
try {
tx = session.beginTransaction();
session.save(userForm);
tx.commit();
ret = 1;
} catch (Exception e) {
if (tx != null) {
tx.rollback();//为什么要进行tx.rollback操作,能实现什么?
}
e.printStackTrace();
System.out.println("添加用户信息时的错误信息:" + e.getMessage());
return ret = 0;
} finally {
if (session != null && session.isOpen()) {
MySession.closeSession(session);
}//关闭Session
}
}
return ret;
}