struts+Hibernate设计用户登陆代码

| No Comments | No TrackBacks

Jsp代码:
 
Login:
 
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
 
<html>
    <head>
    <html:base/>
    <html:errors/>
       <title>JSP for loginForm form</title>
    </head>
    <body>
    <html:base/>
    <html:errors/>
       <html:form action="/login">
           uname &nbsp: <html:text property="uname" size="10"/><html:errors property="uname"/><br/>
           passwd : <html:password property="passwd" size="10"/><html:errors property="passwd"/><br/>
          
           <html:submit/><html:reset/>
           ${err}
       </html:form>
       <html:link page="/register.do">注册新用户</html:link>
    </body>
</html>
 
 
Register:
 
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
 
<html>
 
    <head>
   
    <html:base/>
    <html:errors/>
   
       <title>JSP for registerForm form</title>
    </head>
    <body>
       <html:form action="/register">
           passwd : <html:password property="passwd"/><html:errors property="passwd"/><br/>
           uname : <html:text property="uname"/><html:errors property="uname"/><br/>
          
           <html:submit value="确定"/><html:cancel value="取消"/>
       <html:link page="/login.jsp">回到首页</html:link>
       </html:form>
    </body>
</html>
Success:
 
<%@ page language="java" import="java.util.*"contentType="text/html;charset=gb2312"%>
 
 
<html>
  <head>
   
   
    <title>My JSP 'success.jsp' starting page</title>
   
  
  </head>
  <body>
    success login!welcome to this jsp!!${user.uname}
</html>
 
loginAction:
 
package com.vo.action;
 
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 com.dao.userMgrDAO;
import com.vo.UserInfo;
 
import com.vo.form.LoginForm;
 
 
public class LoginAction extends Action {
 
       public ActionForward execute(
              ActionMapping mapping,
              ActionForm form,
              HttpServletRequest request,
              HttpServletResponse response) {
             
              LoginForm loginForm = (LoginForm) form;
              String uname=loginForm.getUname();
              String passwd=loginForm.getPasswd();
              userMgrDAO udao=new userMgrDAO();
              UserInfo u=udao.vilidateUserInfo(uname,passwd);
              if(u!=null){
                     request.setAttribute("user",u);
                     return mapping.findForward("success");
              }
              else {
                     return mapping.findForward("failed");
              }
 
loginForm:
package com.vo.form;
 
import javax.servlet.http.HttpServletRequest;
 
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
 
 
public class LoginForm extends ActionForm {
 
      
       private String passwd="123";
 
      
       private String uname="ximencf";
 
      
 
      
       public ActionErrors validate(
              ActionMapping mapping,
              HttpServletRequest request) {
          ActionErrors err=new ActionErrors();
         
          if(this.uname==null||this.uname.length()<1){
              err.add(ActionMessages.GLOBAL_MESSAGE,new ActionMessage("uname.err"));
             
          }
          if(this.passwd==null||this.passwd.length()<1){
              err.add(ActionMessages.GLOBAL_MESSAGE,new ActionMessage("pwd.err"));
             
          }
            
 
          return err;
       }
 
      
       public void reset(ActionMapping mapping, HttpServletRequest request) {
              this.passwd=null;
              this.uname=null;
       }
 
       public String getPasswd() {
              return passwd;
       }
 
      
       public void setPasswd(String passwd) {
              this.passwd = passwd;
       }
 
      
       public String getUname() {
              return uname;
       }
 
      
       public void setUname(String uname) {
              this.uname = uname;
       }
 
}
 
RegisterAction:
 
package com.vo.action;
 
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 com.dao.userMgrDAO;
import com.vo.UserInfo;
import com.vo.form.RegisterForm;
 
 
public class RegisterAction extends Action {
 
 
       public ActionForward execute(
              ActionMapping mapping,
              ActionForm form,
              HttpServletRequest request,
              HttpServletResponse response) {
              RegisterForm registerForm = (RegisterForm) form;
              userMgrDAO udao=new userMgrDAO();
           String uname=registerForm.getUname();
           String passwd=registerForm.getPasswd();
           UserInfo u=new UserInfo();
           u.setPasswd(passwd);
           u.setUname(uname);
           
              if(udao.registerUserInfo(u)){
                    
                     return mapping.findForward("success");
              }
          
              else{
                     return mapping.findForward("failed");
              }
       }
 
}
registerForm:
package com.vo.form;
 
import javax.servlet.http.HttpServletRequest;
 
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
 
 
public class RegisterForm extends ActionForm {
 
      
       private String passwd;
 
      
       private String uname;
 
      
       public ActionErrors validate(
              ActionMapping mapping,
              HttpServletRequest request) {
 
              ActionErrors err=new ActionErrors();
       
        if(this.uname==null||this.uname.length()<1){
               err.add(ActionMessages.GLOBAL_MESSAGE,new ActionMessage("uname.err"));
              
        }
        if(this.passwd==null||this.passwd.length()<1){
               err.add(ActionMessages.GLOBAL_MESSAGE,new ActionMessage("pwd.err"));
              
        }
             
 
        return err;
       }
 
       /**
        * Method reset
        * @param mapping
        * @param request
        */
       public void reset(ActionMapping mapping, HttpServletRequest request) {
 
             
       }
 
      
       public String getPasswd() {
              return passwd;
       }
 
      
       public void setPasswd(String passwd) {
              this.passwd = passwd;
       }
 
      
       public String getUname() {
              return uname;
       }
 
       public void setUname(String uname) {
              this.uname = uname;
       }
 
}

Dao:
package com.dao;
 
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
 
import com.vo.HibernateSessionFactory;
import com.vo.UserInfo;
 
 
public class userMgrDAO  {
       private Session session;
       private Transaction trx;
 
       public boolean registerUserInfo(UserInfo u){
             
              try{
                     session=HibernateSessionFactory.currentSession();
                     trx=session.beginTransaction();
                     session.save(u);
                     
                     trx.commit();
                    
              }
              catch(HibernateException ex){
                     if(trx!=null)trx.rollback();
                     ex.printStackTrace();
              }
              finally{
                     if(session!=null)
                            session.close();
              }
             
             
              return false;
       }
       public UserInfo vilidateUserInfo(String uname,String passwd){
              UserInfo us=null;
              try{
                     session=HibernateSessionFactory.currentSession();
                     trx=session.beginTransaction();
                     us=(UserInfo)session.createQuery("from UserInfo u where u.uname='"+uname+"' and u.passwd='"+passwd+"'").uniqueResult();
            System.out.print(us.getUname());              
                     trx.commit();
                     return us;
              }
              catch(HibernateException ex){
                     if(trx!=null)trx.rollback();
                     ex.printStackTrace();
              }
              finally{
                     if(session!=null)
                            session.close();
              }
              return us;
       }
      
       public userMgrDAO() {
             
       }
      
}

来源:风睿网

No TrackBacks

TrackBack URL: http://www.wujianrong.com/mt-tb.cgi/1645

Leave a comment

About this Entry

This page contains a single entry by kevinwu published on January 25, 2007 10:34 PM.

Hibernate代码生成工具 设计全攻略 was the previous entry in this blog.

使用 AppFuse 快速构建 J2EE 应用 is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.