Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java EE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 关于利用hibernate+struts做的修改密码
titisan





发贴: 2
积分: 0
于 2008-08-02 09:55 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
老师叫做个登录界面,要求是用户进去后可以修改密码。由于之前没学过JAVA,临时学的myEclipse,在网上找了段关于修改密码的代码,改改倒是可以用了。用户输入新密码并确认后,页面也是跳转到了success的页面,但是数据库里的密码还是旧密码,没有更新。请各位大侠指教,该怎么办?数据库用的是mySQL。关于修改密码的action如下:
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.yourcompany.struts.action;

import java.util.ArrayList;

import javax.jms.JMSException;
import javax.jms.Session;
import javax.security.auth.login.Configuration;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import org.hibernate.*;

import com.yourcompany.struts.form.ChangePasswordForm;

import dao.HibernateSessionFactory;
import dao.Yonghu;

/**
* MyEclipse Struts
* Creation date: 07-30-2008
*
* XDoclet definition:
* @struts.action path="/changePassword" name="changePasswordForm" input="/changePassword.jsp" scope="request" validate="true"
* @struts.action-forward name="failed" path="/passwordfailed.jsp"
* @struts.action-forward name="success" path="/changesuccess.jsp"
*/
public class ChangePasswordAction extends Action {
  /*
   * Generated Methods
   */

  /**
   * Method execute
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @return ActionForward
   * @throws Exception
   */
  public ActionForward execute(ActionMapping mapping, ActionForm form,
      HttpServletRequest request, HttpServletResponse response) throws Exception {
    ChangePasswordForm changePasswordForm = (ChangePasswordForm) form;// TODO Auto-generated method stub
    
    // 得到用户输入的用户名,密码,新密码和新密码确认
String username = changePasswordForm.getUsername();
String password = changePasswordForm.getPassword();
String newPassword = changePasswordForm.getNewPassword();
String confirmPassword = changePasswordForm.getConfirmPassword();

// 判断新密码和新密码确认是否一致
if (newPassword != null && confirmPassword != null) {

//Not the same
if (!newPassword.equals(confirmPassword)) {
   System.out.println("Your password entris did not match!");
return mapping.findForward("failed");
}

} else {

//若新密码和新密码确认为空,返回错误信息,并要求用户重新输入
System.out.println("Please enter the new password twice!");
return mapping.findForward("failed");
}
//若新密码通过,则判断是否有此用户,以及其密码是否正确

if (username != null) {
// Whether it's a valid user
try {

//SessionFactory sf = new Configuration().configure().buildSessionFactory();
  
//   org.hibernate.classic.Session session = sf.openSession();
// Transaction tx = session.beginTransaction();
org.hibernate.Session session = HibernateSessionFactory.getSession();
// 在数据库中寻找此用户
String sqlQuery = "select u from Yonghu u where u.xh='"+ username + "'";
Query lQuery = ((org.hibernate.Session) session).createQuery(sqlQuery);
ArrayList userList = (ArrayList) lQuery.list();
Yonghu yonghu = new Yonghu();

// 存在此用户
if ((null != userList) && (userList.size() > 0)) {
yonghu = (Yonghu) userList.get(0);
if (!yonghu.getMm().equals(password)) {
   //changePasswordForm.setErrorMessage("Invalid user or incorrect password!");
return mapping.findForward("failed");
} else {

// 如果存在此用户,并且密码正确,则修改密码为新密码
if (newPassword != null) {
yonghu.setMm(newPassword);
//将新密码保存到数据库中
((org.hibernate.Session) session).save(yonghu);
} else {

System.out.println("Please enter the old password!");

return mapping.findForward("failed");


}

}

} else {

// 不存在此用户

System.out.println("Invalid user or incorrect password!");

return mapping.findForward("failed");

}



session.close();

} catch (HibernateException e) {

e.printStackTrace();

System.out.println("Error!");

return mapping.findForward("failed");

}

} else {

System.out.println("Please enter the username!");

return mapping.findForward("failed");

}

// 成功完成密码修改

return mapping.findForward("success");

}

}

多谢。



作者 Re:关于利用hibernate+struts做的修改密码 [Re:titisan]
titisan





发贴: 2
积分: 0
于 2008-08-03 12:55 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
问题解决了,缺了个transaction


作者 Re:关于利用hibernate+struts做的修改密码 [Re:titisan]
laeal





发贴: 1
积分: 0
于 2008-09-01 22:25 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
String username = changePasswordForm.getUsername();
String password = changePasswordForm.getPassword();
String newPassword = changePasswordForm.getNewPassword();

只是从FORM中找到新密码

没有存到数据库中

用SESSION的DAO持久化把新密码存到数据库




flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923