使用Struts整合Hibernate实现用户登录

一、Dynamic Web Project

struct必须是动态网页,而Hibernate可以使用静态网页实现,两者结合使用动态。

在创建动态网页时,记得勾选Generate web.xml deployment descripor,创建web.xml配置文件。
如果你忘记勾选,也可以这样:
Dynamic Web Project右击->Java EE Tools->Generate deployment 就可以了。

二、导入两者需要的jar包。

在WebContent->WEB-INF下的lib目录中
struts需要的jar包:

struts需要的jar包
和hibernate需要的jar包:hibernate需要的jar包
记得build Path

三、配置xml文件

配置web.xml文件的作用就是,实现过滤器,成为struts2的入口。
不出意外,应该可以直接粘:

<?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/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>struts</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <filter>
   <filter-name>struts2</filter-name>
   <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping><!-- 过滤器 struts的入口 -->
   <filter-name>struts2</filter-name>
   <url-pattern>/*</url-pattern>  
  </filter-mapping>
</web-app>

四、hibernate.cfg.xml

放在src目录下。
hibernate.cfg.xml:作用是连接数据库,然后找到数据库中的表。
然后,数据库中的表会映射实体类。

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

<session-factory>
	<property name="connection.username">root</property><!--root是数据库的用户名-->
	<property name="connection.url">
		jdbc:mysql://localhost:3306/hibernate <!-- 注意:这里的hibernate,是数据库的名字,将要连接的数据库-->
	</property>
	<property name="dialect">
		org.hibernate.dialect.MySQLDialect
	</property>
	<property name="connection.password">123</property><!--123代表是数据库的密码-->
	<property name="connection.driver_class">
		com.mysql.jdbc.Driver
	</property>
	<property name="show_sql">true</property>
	<property name="format_sql">true</property>
	<mapping resource="com/tjetc/domain/User.hbm.xml" /><!--User.hbm.xml是实体类映射对象,下面会有-->

</session-factory>

</hibernate-configuration>

user数据库

User实体类这样写:
和数据库对应

package com.tjetc.domain;

public class User {
	private int id;
	private String name;
	private String password;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", password=" + password + "]";
	}
		
}
}

五、User.hbm.xml

实体类映射对象
放在这里:实体类映射对象
这样写:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping><!-- 实体类对应的映射文件 -->
	<class name="com.tjetc.domain.User" table="user"><!-- name:实体类类的全路径 table:是数据库对应的表名 -->
		<id name="id">
			<generator class="native"/>  <!-- 自动生长 主键 -->
		</id>
		<property name="name" /><!-- 非主键列 -->
		<property name="password"/>
	</class>
</hibernate-mapping>

六、创建登录页面

在WebContent目录下创建。

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>
	<form action="login" method="post">
	用户名:<input type="text" name="name"/>
	密码:<input type="password" name="password"/>
	<input type="submit" value="提交"/>
	</form>
</body>
</html>

其中的action=“login” 是将表单内容传入到login函数中,如下:

七、创建LoginAction.java

在这里插入图片描述
代码如下:
作用获取用户输入的用户名和密码

package com.tjetc.action;

import org.apache.struts2.ServletActionContext;

import com.tjetc.dao.UserDao;
import com.tjetc.domain.User;

public class LoginAction {
	private String name;
	private String password;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String login() {//在这里接收用户输入的用户名和密码
		System.out.println(name);
		System.out.println(password);
		//创建UserDao对象
				UserDao userDao = new UserDao();
				User user=userDao.getByUsername(name);	//这里用到UserDao是操作数据库的,下面会有。
				System.out.println(user);
				if (user==null) {//没有改用户名
					System.out.println("该用户名不存在");
					return "login";			
				}else{
					if (!password.equals(user.getPassword())) {
						System.out.println("密码错误");
						return "login";	
					}else {
						ServletActionContext.getRequest().getSession().setAttribute("user", user);
						return "success";
					}
				}
	}
}


八、配置struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<package name="test" namespace="/" extends="struts-default">
		<action name="login" class="com/tjetc/action/LoginAction.java" method="login"><!--class是LoginAction,method是其中的login-->
			<result name="success">/success.jsp</result><!--login() 函数返回success的话,就会跳转到success.jsp页面-->
			<result name="login" type="redirect">/login.jsp</result>!--login() 函数返回login的话,就会跳转到login.jsp页面-->
		</action>
	</package>
</struts>

简易设置success.jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>
登录成功!
</body>
</html>

此时运行login.jsp
输入用户名,密码。点击提交,出现如下画面,则测试成功。
测试成功

九、使用数据库,进行数据访问

呜呜呜呜,因为mysql的配置和包的问题卡了超级久。。。
我的mysql是8.0.19的,那些包是5.几的,所有一些方面就存在一些问题。
最后的时候会说一下。
下面是UserDao

package com.tjetc.dao;

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

import com.tjetc.domain.User;

public class UserDao {
	public User getByUsername(String username) {
		SessionFactory factory = new Configuration().configure().buildSessionFactory();
		Session session = factory.openSession();
		User user = (User) session.createQuery("from User u where u.name=?").setString(0, username).uniqueResult();
		session.close();
		return user;
	}
}
//第一句是加载映射,创建会话工程对象
//第二句是创建会话对象
//第三句是在mysql中寻找name=用户输入的name的User对象(name不重复)
//关闭会话
//返回user

在mysql有数据的情况,运行login登录,没问题的话,你超厉害的!!!

我遇到的问题。呜呜呜

关于mysql8.0和5.0的问题。
首先我的是8.0.19.老师给的都是5的包。
这时候,把lib下的mysql-connector-java-5.1.7-bin.jar包换成mysql-connector-java-8.0.13.jar。注意要把5.1的删干净。把8.0的build path。

然后在hibernate.cfg.xml这个连接数据库的配置文件中,把这句话
jdbc:mysql://localhost:3306/hibernate
(hibernate是数据库名称)
换成
jdbc:mysql://localhost:3306/hibernate?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT

然后,把下面的驱动 com.mysql.jdbc.Driver换成
com.mysql.cj.jdbc.Driver
在这里插入图片描述
这大概就是我心酸的历程吧。5555555
努力呀。

一个集坚强与自信于一身的菇凉。

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

淡雅的惆怅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值