基于struts2和hibernate实现登录和注册功能

网友投稿 292 2023-03-22


基于struts2和hibernate实现登录和注册功能

本文实例为大家分享了struts2和hibernate实现登录和注册功能,供大家参考,具体内容如下

1、该项目使用mysql数据库,数据库名为test,表名info,如图所示:

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

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

struts2

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

struts2

/*

index.jsp

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

struts2

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

struts2

/*

index.jsp

3、编写视图组件(JSP页面)

(1)登录页面login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<%@ taglib prefix="s" uri="/struts-tags" %>







注册

(2)登陆成功页面success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<%@ taglib prefix="s" uri="/struts-tags" %>

欢迎,登陆成功!

(3)注册页面register.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<%@ taglib prefix="s" uri="/struts-tags" %>







  

返回

4、业务控制器Action

(1)登录页面对应的业务控制器LoginAction.java

其中,重写valiadate()方法,进行手工验证

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", "该用户尚未注册");

}else{

UserInfoPO ui=new UserInfoPO();

for(int i=0;i

ui=(UserInfoPO) list.get(i);

if(this.getUserName().equals(ui.getUserName())){

if(ui.getPassword().equals(this.getPassword())){

message=SUCCESS;

}else{

addFieldError("password", "登录密码不正确");

}

}

}

}

}

}

public String execute() throws Exception{

return message;

}

}

(2)注册页面对应的业务控制器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

ui=(UserInfoPO) list.get(i);

if(ui.getUserName().equals(this.getUserName())){

addFieldError("userName", "用户名已存在!");

}

}

}

if(this.getPassword1()==null||this.getPassword1().length()==0){

addFieldError("password1", "登录密码不许为空!");

}else if(this.getPassword2()==null||this.getPassword2().length()==0){

addFieldError("password2", "重复密码不许为空!");

}else if(!this.getPassword1().equals(this.getPassword2())){

addFieldError("password2", "两次密码不一致!");

}

}

public UserInfoPO userInfo(){

UserInfoPO info=new UserInfoPO();

info.setUserName(this.getUserName());

info.setPassword(this.getPassword1());

return info;

}

public String execute() throws Exception{

LoginRegisterInfo lr=new LoginRegisterInfo();

String ri=lr.saveInfo(userInfo());

if(ri.equals("success")){

mess=SUCCESS;

}

return mess;

}

}

5、在struts.xml中配置Action

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

/login.jsp

/register.jsp

/register.jsp

/success.jsp

/login.jsp

/login.jsp

6、Hibernate的配置文件

使用Hibernate需要通过Hibernate的配置文件加载数据库驱动以及与数据建立连接,配置文件为hibernate.cfg.xml

"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

com.mysql.jdbc.Driver

jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8

root

org.hibernate.dialect.MySQL5Dialect

7、加载上面Hibernate配置文件的类(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对象以及对应的映射文件(在同一个包下)

(1)PO对象的类UserInfoPO.Java

package PO;

/*

* PO对象(持久化对象)的类,与数据库相对应

*/

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对应的映射文件UserInfoPO.hbm.xml

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

</hibernate-mapping>

9、完成登录和注册业务功能

将登录和注册业务功能封装到类LoginRegisterInfo(JavaBean)中

数据库操作类LoginRegisterInfo.java:

package loginRegisterDao;

/*

* 登录和注册业务功能,封装到这个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("LoginRxNeKRArrUIegisterInfo类中有异常,异常为::"+e);

e.printStackTrace();

return null;

}

}

public void message(String mess){

int type=JOptionPane.YES_NO_OPTION;

String title="提示信息";

JOptionPane.showMessageDialog(null, mess,title,type);

}

}

ui=(UserInfoPO) list.get(i);

if(this.getUserName().equals(ui.getUserName())){

if(ui.getPassword().equals(this.getPassword())){

message=SUCCESS;

}else{

addFieldError("password", "登录密码不正确");

}

}

}

}

}

}

public String execute() throws Exception{

return message;

}

}

(2)注册页面对应的业务控制器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

ui=(UserInfoPO) list.get(i);

if(ui.getUserName().equals(this.getUserName())){

addFieldError("userName", "用户名已存在!");

}

}

}

if(this.getPassword1()==null||this.getPassword1().length()==0){

addFieldError("password1", "登录密码不许为空!");

}else if(this.getPassword2()==null||this.getPassword2().length()==0){

addFieldError("password2", "重复密码不许为空!");

}else if(!this.getPassword1().equals(this.getPassword2())){

addFieldError("password2", "两次密码不一致!");

}

}

public UserInfoPO userInfo(){

UserInfoPO info=new UserInfoPO();

info.setUserName(this.getUserName());

info.setPassword(this.getPassword1());

return info;

}

public String execute() throws Exception{

LoginRegisterInfo lr=new LoginRegisterInfo();

String ri=lr.saveInfo(userInfo());

if(ri.equals("success")){

mess=SUCCESS;

}

return mess;

}

}

5、在struts.xml中配置Action

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

/login.jsp

/register.jsp

/register.jsp

/success.jsp

/login.jsp

/login.jsp

6、Hibernate的配置文件

使用Hibernate需要通过Hibernate的配置文件加载数据库驱动以及与数据建立连接,配置文件为hibernate.cfg.xml

"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

com.mysql.jdbc.Driver

jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8

root

org.hibernate.dialect.MySQL5Dialect

7、加载上面Hibernate配置文件的类(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对象以及对应的映射文件(在同一个包下)

(1)PO对象的类UserInfoPO.Java

package PO;

/*

* PO对象(持久化对象)的类,与数据库相对应

*/

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对应的映射文件UserInfoPO.hbm.xml

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

</hibernate-mapping>

9、完成登录和注册业务功能

将登录和注册业务功能封装到类LoginRegisterInfo(JavaBean)中

数据库操作类LoginRegisterInfo.java:

package loginRegisterDao;

/*

* 登录和注册业务功能,封装到这个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("LoginRxNeKRArrUIegisterInfo类中有异常,异常为::"+e);

e.printStackTrace();

return null;

}

}

public void message(String mess){

int type=JOptionPane.YES_NO_OPTION;

String title="提示信息";

JOptionPane.showMessageDialog(null, mess,title,type);

}

}

ui=(UserInfoPO) list.get(i);

if(ui.getUserName().equals(this.getUserName())){

addFieldError("userName", "用户名已存在!");

}

}

}

if(this.getPassword1()==null||this.getPassword1().length()==0){

addFieldError("password1", "登录密码不许为空!");

}else if(this.getPassword2()==null||this.getPassword2().length()==0){

addFieldError("password2", "重复密码不许为空!");

}else if(!this.getPassword1().equals(this.getPassword2())){

addFieldError("password2", "两次密码不一致!");

}

}

public UserInfoPO userInfo(){

UserInfoPO info=new UserInfoPO();

info.setUserName(this.getUserName());

info.setPassword(this.getPassword1());

return info;

}

public String execute() throws Exception{

LoginRegisterInfo lr=new LoginRegisterInfo();

String ri=lr.saveInfo(userInfo());

if(ri.equals("success")){

mess=SUCCESS;

}

return mess;

}

}

5、在struts.xml中配置Action

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

/login.jsp

/register.jsp

/register.jsp

/success.jsp

/login.jsp

/login.jsp

6、Hibernate的配置文件

使用Hibernate需要通过Hibernate的配置文件加载数据库驱动以及与数据建立连接,配置文件为hibernate.cfg.xml

"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

com.mysql.jdbc.Driver

jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8

root

org.hibernate.dialect.MySQL5Dialect

7、加载上面Hibernate配置文件的类(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对象以及对应的映射文件(在同一个包下)

(1)PO对象的类UserInfoPO.Java

package PO;

/*

* PO对象(持久化对象)的类,与数据库相对应

*/

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对应的映射文件UserInfoPO.hbm.xml

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

</hibernate-mapping>

9、完成登录和注册业务功能

将登录和注册业务功能封装到类LoginRegisterInfo(JavaBean)中

数据库操作类LoginRegisterInfo.java:

package loginRegisterDao;

/*

* 登录和注册业务功能,封装到这个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("LoginRxNeKRArrUIegisterInfo类中有异常,异常为::"+e);

e.printStackTrace();

return null;

}

}

public void message(String mess){

int type=JOptionPane.YES_NO_OPTION;

String title="提示信息";

JOptionPane.showMessageDialog(null, mess,title,type);

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:微信小程序template模板实例详解
下一篇:Spring MVC结合Spring Data JPA实现按条件查询和分页
相关文章

 发表评论

暂时没有评论,来抢沙发吧~