基于struts2和hibernate实现登录和注册功能
本文实例为大家分享了struts2和hibernate实现登录和注册功能,供大家参考,具体内容如下
1、该项目使用mysql数据库,数据库名为test,表名info,如图所示:
2、配置web.xml(struts2使用)
<?xml version="1.0" encoding="utf-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.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"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
3、编写视图组件(jsp页面)
(1)登录页面login.jsp
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title><s:text name="基于sh的登录注册系统" /></title> </head> <body bgcolor="#ccccff"> <s:form action="login" method="post"> <br><br><br><br><br><br> <table border="1" align="center" bgcolor="aabbccdd"> <tr> <td> <s:textfield name="username" label="用户名字" size="16" /> </td> </tr> <tr> <td> <s:password name="password" label="用户密码" size="18" /> </td> </tr> <tr> <td colspan="2" align="center"> <s:submit value="登录" /> </td> </tr> <tr> <td colspan="2" align="center"> <s:a href="http://localhost:8080/hibernate01/register.jsp">注册</s:a> </td> </tr> </table> </s:form> </body> </html>
(2)登陆成功页面success.jsp
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>insert title here</title> </head> <body bgcolor="#ccccff"> <hr> <table> <tr> <td> 欢迎<s:property value="username"/>,登陆成功! </td> </tr> </table> <hr> </body> </html>
(3)注册页面register.jsp
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>insert title here</title> </head> <body bgcolor="#ccccff"> <s:form action="register" method="post"> <br><br><br><br><br><br> <table border="1" align="center" bgcolor="aabbccdd"> <tr> <td> <s:textfield name="username" label="用户名字" size="16" /> </td> </tr> <tr> <td> <s:password name="password1" label="用户密码" size="18" /> </td> </tr> <tr> <td> <s:password name="password2" label="再次输入密码" size="18" /> </td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" value="提交" /> <input type="reset" value="清空" /> </td> </tr> <tr> <td colspan="2" align="center"> <s:a href="http://localhost:8080/hibernate01/login.jsp">返回</s:a> </td> </tr> </table> </s:form> </body> </html>
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<list.size();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<list.size();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
<!doctype struts public "-//apache software foundation//dtd struts configuration 2.0//en" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="default" extends="struts-default"> <action name="register" class="loginregisteraction.registeraction"> <result name="success">/login.jsp</result> <result name="input">/register.jsp</result> <result name="error">/register.jsp</result> </action> <action name="login" class="loginregisteraction.loginaction"> <result name="success">/success.jsp</result> <result name="input">/login.jsp</result> <result name="error">/login.jsp</result> </action> </package> </struts>
6、hibernate的配置文件
使用hibernate需要通过hibernate的配置文件加载数据库驱动以及与数据建立连接,配置文件为hibernate.cfg.xml
<!doctype hibernate-configuration public "-//hibernate/hibernate configuration dtd 3.0//en" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.driver_class">com.mysql.jdbc.driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/test?useunicode=true&characterencoding=utf-8</property> <property name="connection.username">root</property> <property name="connection.password"></property> <!-- 指定数据库的方言 --> <property name="dialect">org.hibernate.dialect.mysql5dialect</property> <!-- 加入映射文件 --> <mapping resource="po/userinfopo.hbm.xml"/> </session-factory> </hibernate-configuration>
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
<?xml version="1.0" encoding="utf-8"?> <!doctype hibernate-mapping public "-//hibernate/hibernate mapping dtd 3.0//en" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- 映射文件的根元素 --> <hibernate-mapping> <!-- 配置po对象与数据库中表的对应关系使用class元素,name配置po对象对应的类, table配置该po对象在数据库中对应的表名,catalog配置表对应的数据库名 --> <class name="po.userinfopo" table="info" catalog="test"> <!-- id元素配置po对象与数据库中表的对应id字段,name配置po对象对应的属性,type指定类型 generator元素将主键自动加入序列 --> <id name="id" type="int"> <column name="id"/> <generator class="assigned" /> </id> <property name="username" type="string"> <column name="username" length="30" not-null="true" /> </property> <property name="password" type="string"> <column name="password" length="30" not-null="true" /> </property> </class> </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("loginregisterinfo类中有异常,异常为::"+e); e.printstacktrace(); return null; } } public void message(string mess){ int type=joptionpane.yes_no_option; string title="提示信息"; joptionpane.showmessagedialog(null, mess,title,type); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: Java编程异常处理最佳实践【推荐】
下一篇: Java实现多个文档合并输出到一个文档
推荐阅读
-
基于struts2和hibernate实现登录和注册功能
-
基于jQueryUI和Corethink实现百度的搜索提示功能
-
使用hibernate和struts2实现分页功能的示例
-
flask web开发实战源代码(flask实现web的登录和注册)
-
flask web开发实战源代码(flask实现web的登录和注册)
-
基于websocket实现扫描小程序码登录web页面和微信小程序(双端登录)
-
JavaWeb使用Cookie模拟实现自动登录功能(不需用户名和密码)
-
JavaWeb使用Cookie模拟实现自动登录功能(不需用户名和密码)
-
struts2与cookie 实现自动登录和验证码验证实现代码
-
JavaWeb实现用户登录注册功能实例代码(基于Servlet+JSP+JavaBean模式)