真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

基于struts2和hibernate實(shí)現(xiàn)登錄和注冊(cè)功能

本文實(shí)例為大家分享了struts2和hibernate實(shí)現(xiàn)登錄和注冊(cè)功能,供大家參考,具體內(nèi)容如下

10年積累的成都做網(wǎng)站、成都網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問(wèn)題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有廈門(mén)免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

1、該項(xiàng)目使用MySQL數(shù)據(jù)庫(kù),數(shù)據(jù)庫(kù)名為test,表名info,如圖所示:

基于struts2和hibernate實(shí)現(xiàn)登錄和注冊(cè)功能

 2、配置web.xml(Struts2使用) 

<?xml version="1.0" encoding="UTF-8"?>

  
  
    struts2
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  
  
    struts2
    /*
  
  
  
 
  index.jsp
 



  3、編寫(xiě)視圖組件(JSP頁(yè)面)

(1)登錄頁(yè)面login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>



<s:text name="基于SH的登錄注冊(cè)系統(tǒng)" />


  
    





注冊(cè)

(2)登陸成功頁(yè)面success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>




Insert title here


  
歡迎,登陸成功!

(3)注冊(cè)頁(yè)面register.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>



Insert title here


  
    





  
返回

4、業(yè)務(wù)控制器Action

(1)登錄頁(yè)面對(duì)應(yīng)的業(yè)務(wù)控制器LoginAction.java 

其中,重寫(xiě)valiadate()方法,進(jìn)行手工驗(yàn)證

package loginRegisterAction;

import java.util.List;

import loginRegisterDao.LoginRegisterInfo;

import PO.UserInfoPO;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{

  private String userName;
  private String password;
  private String message="error";
  private List list;
  
  public String getUserName() {
    return userName;
  }
  public void setUserName(String userName) {
    this.userName = userName;
  }
  public String getPassword() {
    return password;
  }
  public void setPassword(String password) {
    this.password = password;
  }
  
  public void validate(){
    if(this.getUserName()==null||this.getUserName().length()==0){
      addFieldError("userName", "用戶名不能為空!");
    }else{
      LoginRegisterInfo info= new LoginRegisterInfo();
      list=info.queryInfo("userName", this.getUserName());
      if(list.size()==0){
        addFieldError("userName", "該用戶尚未注冊(cè)");
      }else{
        UserInfoPO ui=new UserInfoPO();
        for(int i=0;i

(2)注冊(cè)頁(yè)面對(duì)應(yīng)的業(yè)務(wù)控制器RegisterAction.java

package loginRegisterAction;

import java.util.List;

import loginRegisterDao.LoginRegisterInfo;

import PO.UserInfoPO;

import com.opensymphony.xwork2.ActionSupport;

public class RegisterAction extends ActionSupport {

  private String userName;
  private String password1;
  private String password2;
  private String mess=ERROR;  //ERROR等同于"error"
  private List list;
  
  public String getUserName() {
    return userName;
  }
  public void setUserName(String userName) {
    this.userName = userName;
  }
  public String getPassword1() {
    return password1;
  }
  public void setPassword1(String password1) {
    this.password1 = password1;
  }
  public String getPassword2() {
    return password2;
  }
  public void setPassword2(String password2) {
    this.password2 = password2;
  }
  
  public void validate(){
    if(this.getUserName()==null||this.getUserName().length()==0){
      addFieldError("userName", "用戶名不能為空!");
    }else{
      LoginRegisterInfo info= new LoginRegisterInfo();
      list=info.queryInfo("userName", this.getUserName());
      UserInfoPO ui=new UserInfoPO();
      for(int i=0;i

5、在struts.xml中配置Action


 

  
    
      /login.jsp
      /register.jsp
      /register.jsp
    
    
      /success.jsp
      /login.jsp
      /login.jsp
    
  

6、Hibernate的配置文件

使用Hibernate需要通過(guò)Hibernate的配置文件加載數(shù)據(jù)庫(kù)驅(qū)動(dòng)以及與數(shù)據(jù)建立連接,配置文件為hibernate.cfg.xml 




  
    com.mysql.jdbc.Driver
    jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
    root
    
    
    org.hibernate.dialect.MySQL5Dialect
    
    
    
  


 7、加載上面Hibernate配置文件的類(lèi)(HIbernateSessionFactory.java)

package addHibernateFile;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateSessionFactory {

  private SessionFactory sessionFactory;
  
  public HibernateSessionFactory(){
    
  }
  
  public SessionFactory config(){
    try{
      Configuration configuration= new Configuration();
      Configuration configure=configuration.configure("hibernate.cfg.xml");
      return configure.buildSessionFactory();
    }catch(Exception e){
    e.getMessage();
    return null;
    }
  }
  
  public Session getSession(){
    sessionFactory=config();
    return sessionFactory.openSession();
  }
  
}

8、PO對(duì)象以及對(duì)應(yīng)的映射文件(在同一個(gè)包下)

(1)PO對(duì)象的類(lèi)UserInfoPO.Java

package PO;
/*
 * PO對(duì)象(持久化對(duì)象)的類(lèi),與數(shù)據(jù)庫(kù)相對(duì)應(yīng)
 */
public class UserInfoPO {

  private int id;
  private String userName;
  private String password;
  
  public int getId() {
    return id;
  }
  public void setId(int id) {
    this.id = id;
  }
  public String getUserName() {
    return userName;
  }
  public void setUserName(String userName) {
    this.userName = userName;
  }
  public String getPassword() {
    return password;
  }
  public void setPassword(String password) {
    this.password = password;
  }
  
  
  
  
}

(2) PO對(duì)應(yīng)的映射文件UserInfoPO.hbm.xml

 <?xml version="1.0" encoding="utf-8"?>





  
  

    
    
      
      
    
    
    
      
    
    
    
      
    
  
  



9、完成登錄和注冊(cè)業(yè)務(wù)功能

將登錄和注冊(cè)業(yè)務(wù)功能封裝到類(lèi)LoginRegisterInfo(JavaBean)中

數(shù)據(jù)庫(kù)操作類(lèi)LoginRegisterInfo.java:

package loginRegisterDao;
/*
 * 登錄和注冊(cè)業(yè)務(wù)功能,封裝到這個(gè)JavaBean
 */
import java.util.List;

import javax.swing.JOptionPane;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import PO.UserInfoPO;
import addHibernateFile.HibernateSessionFactory;

public class LoginRegisterInfo {

  private Session session;
  private Transaction transaction;
  private Query query;
  HibernateSessionFactory getSession;
  
  public LoginRegisterInfo(){
  }
  
  public String saveInfo(UserInfoPO info){
    String mess="error";
    getSession=new HibernateSessionFactory();
    session=getSession.getSession();
    try{
      transaction=session.beginTransaction();
      session.save(info);
      transaction.commit();
      mess="success";
      return mess;
    }catch(Exception e){
      message("RegisterInfo.error:"+e);
      e.printStackTrace();
      return null;
    }
  }
  
  public List queryInfo(String type,Object value){
    getSession=new HibernateSessionFactory();
    session=getSession.getSession();
    try{
      String hqlsql="from UserInfoPO as u where u.userName=?";
      query=session.createQuery(hqlsql);
      query.setParameter(0, value);
      List list=query.list();
      transaction=session.beginTransaction();
      transaction.commit();
      return list;
    }catch(Exception e){
      message("LoginRegisterInfo類(lèi)中有異常,異常為::"+e);
      e.printStackTrace();
      return null;
      }
  }
  
  
  public void message(String mess){
    int type=JOptionPane.YES_NO_OPTION;
    String title="提示信息";
    JOptionPane.showMessageDialog(null, mess,title,type);
  }
  
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。


本文名稱(chēng):基于struts2和hibernate實(shí)現(xiàn)登錄和注冊(cè)功能
網(wǎng)站URL:http://weahome.cn/article/johcos.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部