项目的文件结构

讯享网
login.jsp
<%@page contentType="text/html" 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的应用"></s:text></title> </head> <body bgcolor="#CCCCFF"> <s:form action="login" method="post"> <br><br><br><br><br><br> <table border="1" align="center" label="#AABBCCDD"> <tr> <td> <s:textfield name="userName" label="用户名字" size="16" /> </td> </tr> <tr> <td> <s:password name="password" label="用户密码" size="16" /> </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/ch05/register.jsp">注册</s:a> </td> </tr> </table> </s:form> </body> </html>
讯享网
register.jsp
讯享网<%@page contentType="text/html" 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的应用"></s:text></title> </head> <body bgcolor="#CCCCFF"> <s:form action="register" method="post"> <br><br><br><br><br><br> <table border="1" align="center" label="#AABBCCDD"> <tr> <td> <s:textfield name="userName" label="用户名字" size="16" /> </td> </tr> <tr> <td> <s:password name="password1" label="用户密码" size="16" /> </td> </tr> <tr> <td> <s:password name="password2" label="再次输入密码" size="16" /> </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/ch05/login.jsp">返回</s:a> </td> </tr> </table> </s:form> </body> </html>
success.jsp
<%@page contentType="text/html" 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的应用"></s:text></title> </head> <body bgcolor="#CCCCFF"> <hr> <table border="0" align="center" bgcolor="AABBCCDD"> <tr> <td> 欢迎${
userName},登录成功!!! </td> </tr> </table> <hr> </body> </html>
web.xml
讯享网<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaeee/web-app_3_1.xsd" id="WebApp_ID" version="3.1" > <filter> <!-- 配置Struts2核心控制器的名字 --> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <!-- Struts2控制器的名字 --> <filter-name>struts2</filter-name> <!-- 拦截所有的URL请求--> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> </web-app>
hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <!-- Hibernate配置文件的根元素,其他元素应在其中 --> <hibernate-configuration> <!-- 设置初始化Hibernate5参数的元素,其中指定HIbernate5初始化参数, 表明以下的配置是针对session-factory配置,SessionFactory是Hibernate5中的一个接口 这个接口主要负责保存Hibernate5的配置信息以及对Session的操作 --> <session-factory> <!-- 设置连接数据库所用的驱动 --> <property name="hibernate.connection.driver_class"> com.mysql.jdbc.Driver </property> <!-- 设置数据库连接使用的URL --> <property name="hibernate.connection.url"> jdbc:mysql://127.0.0.1:3306/test </property> <!-- 设置数据库的用户名 --> <property name="hibernate.connection.username">root</property> <!-- 设置数据库的密码 --> <property name="hibernate.connection.password">hjw</property> <!-- 设置数据库的方言,每种数据库都有对应的方言 --> <property name="hibernate.dialect"> org.hibernate.dialect.MySQL5Dialect </property> <!-- 设置连接池中的最小连接数 --> <property name="hibernate.c3p0.min_size">10</property> <!-- 设置连接池中的最大连接数 --> <property name="hibernate.c3p0.max_size">50</property> <!-- 设置连接池中某个数据库访问超时时间,超时将被移除,即最大时间 --> <property name="hibernate.c3p0.timeout">120</property> <!-- 设置时间范围内检查所有连接的空闲时间并销毁超时的连接 --> <property name="hibernate.c3p0.idle_test_period">3000</property> <!-- 设置当连接池中的连接耗尽的时候,一次同时获取的连接数 --> <property name="acquireIncrement">5</property> <!-- 设置在从数据库获取新连接失败后重复尝试的次数 --> <property name="acquireRetryAttempts">30</property> <!-- 设置两次连接中间隔时间,单位为毫秒 --> <property name="acquireRettryDelay">1000</property> <!-- 连接关闭时默认将所有未提交的操作回滚 --> <property name="autoCommitOnClose">false</property> <!-- 设置session上下文,thread表示,当前thread中取到session保证是同一个session --> <property name="current_session_content_class">thread</property> <!-- 根据需要自动创建数据表 --> <property name="binernate.hbm2ddl.auto">update</property> <!-- 设置是否将Hibernate5发送给数据库的SQL语句,有助于迅速解决问题 --> <property name="hibernate.format_sql">true</property> <!-- 加入映射文件,可以加入多个映射文件 --> <mapping resource="PO/UserInfoPO.hbm.xml"/> </session-factory> </hibernate-configuration>
struts.xml
讯享网<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <include file="example.xml"></include> <package name="default" extends="struts-default"> <action name="register" class="loginRegisterAction.ResisterAction"> <result name="success">/login.jsp</result> <result name="error">/register.jsp</result> <result name="input">/register.jsp</result> </action> <action name="login" class="loginRegisterAction.LoginAction"> <result name="success">/success.jsp</result> <result name="error">/login.jsp</result> <result name="input">/login.jsp</result> </action> </package> </struts>
HibernateSessionFactory.java

package addHibernateFile; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateSessionFactory {
private static SessionFactory sessionFactory; public static Session getSession() {
return getSessionFactory().openSession(); } public static SessionFactory getSessionFactory() {
if(sessionFactory==null||sessionFactory.isClosed()) {
sessionFactory=new Configuration().configure().buildSessionFactory(); } return sessionFactory; } }
LoginAction.java
讯享网package loginRegisterAction; import com.opensymphony.xwork2.ActionSupport; import loginResisterDao.LoginResisterInfo; import PO.UserInfoPO; import java.util.List; import org.eclipse.jdt.internal.compiler.ast.ThisReference; 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("字段名","错误信息")给一个字段(属性)添加错误消息 addFieldError("userName", "用户名不能为空!!!"); } else {
LoginResisterInfo info=new LoginResisterInfo(); list=info.queryInfo("userName",this.getUserName()); if(list.size()==0) {
addFieldError("userName", "该用户尚未注册!"); } else {
UserInfoPO ui=new UserInfoPO(); int count=0; for(int i=0;i<list.size();i++) {
count++; 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; } }
RegisterAction.java
package loginRegisterAction; import PO.UserInfoPO; import com.opensymphony.xwork2.ActionSupport; import com.sun.org.apache.regexp.internal.REUtil; import loginResisterDao.LoginResisterInfo; import java.util.List; import org.eclipse.jdt.internal.compiler.ast.ThisReference; public class ResisterAction extends ActionSupport{
private String userName; private String password1; private String password2; private String mess="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 {
LoginResisterInfo info=new LoginResisterInfo(); 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{
LoginResisterInfo lr=new LoginResisterInfo(); String ri=lr.saveInfo(userInfo()); if(ri.equals("success")) {
mess="success"; } return mess; } }
LoginResisterInfo.java
讯享网package loginResisterDao; import addHibernateFile.HibernateSessionFactory; import PO.UserInfoPO; import java.util.List; import javax.swing.JOptionPane; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; import com.sun.corba.se.impl.protocol.giopmsgheaders.Message; public class LoginResisterInfo {
private Session session; private Transaction transaction; private Query query; HibernateSessionFactory getSession; public LoginResisterInfo() {
} //将注册的信息写入数据库 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) {
// TODO: handle exception message("RegisterInfo.error:"+e); e.printStackTrace(); return null; } } //查询数据库 public List queryInfo(String type,Object value) {
getSession=new HibernateSessionFactory(); session=getSession.getSession(); try {
String hql="from UserInfoPO as u where u.userName=?"; //createQuery():对查询语句的执行 query=session.createQuery(hql); //setParameter():对动态参数的绑定 query.setParameter(0,value); List list=query.list(); transaction=session.beginTransaction(); transaction.commit(); return list; }catch (Exception e) {
// TODO: handle exception 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); } }
UserInfoPO.java
package 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; } }
UserInfoPO.hbm.xml
讯享网<?xml version="1.0" encoding="UTF-8"?> <!-- 解析文件的DTD --> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- 映射文件的根目录 --> <hibernate-mapping> <class name="PO.UserInfoPO" table="info" catalog="test"> <id name="id" type="int"> <column name="id"></column> <generator class="increment"></generator> </id> <property name="userName" type="string"> <column name="userName" length="30" not-null="true"></column> </property> <property name="password" type="string"> <column name="password" length="30" not-null="true"></column> </property> </class> </hibernate-mapping>
运行结果:
登录页面:

注册页面:

登录成功页面:

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/47486.html