基于Struts2+Hibernate5+Spring5的登录系统

项目的文件结构:
在这里插入图片描述
UserDao.java

package com.dao;
import com.entity.User;
import java.util.List;

public interface UserDao {
	/**
	 * 加载User实例
	 * @参数id知道需要加载的User实例的主键值
	 * @return返回加载的User实例
	 */
	User get(Integer id);
	
	/**
	 * 保存User实例
	 * @参数User知道需要保存的User实例
	 * @return返回刚刚保存的User实例的标识属性值
	 */
	Integer save(User user);
	
	/**
	 * 根据用户名查找User
	 * @参数name指定查询的用户名
	 * @return返回用户名对应的全部User
	 */
	
	List<User> findByName(String name);
	
}

UserDaoImpl.java

package com.dao.impl;
import com.dao.UserDao;
import com.entity.User;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;

public class UserDaoImpl implements UserDao{
	//实例化一个HIbernateTemplate对象,用于执行持久化对象
	private HibernateTemplate ht=null;
	//Hibernate持久化操作所需SessionFactory
	private SessionFactory sessionFactory=null;
	//用于依赖注入的setter方法
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory=sessionFactory;
	}
	
	//初始化HibernateTemplate方法
	private HibernateTemplate getHibernateTemplate() {
		if(ht==null) {
			ht=new HibernateTemplate(sessionFactory);
		}
		return ht;
	}
	
	public User get(Integer id) {
		//获取对应表中id为某个值的数据,id为主键索引
		return getHibernateTemplate().get(User.class, id);
	}
	
	public Integer save(User user) {
		return (Integer)getHibernateTemplate().save(user);
	}
	
	public List<User> findByName(String name){
		//根据名称查找匹配的User
		return (List<User>)getHibernateTemplate().find("from user u where u.name=?",name);
	}
}

User.java

package com.entity;
import java.io.Serializable;

public class User implements Serializable{
	private Integer intId;
	private String name;
	private String password;
	public User() {}
	
	public User(Integer intId,String name,String password) {
		this.intId=intId;
		this.name=name;
		this.password=password;
	}

	public Integer getIntId() {
		return intId;
	}

	public void setIntId(Integer intId) {
		this.intId = intId;
	}

	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;
	}
	
	
}


User.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="com.entity.User" table="t_use_info" catalog="test1">
			<id name="intId" type="java.lang.Integer">
				<column name="int_id"></column>
				<generator class="increment"></generator>
			</id>
			<property name="name" type="java.lang.String">
				<column name="name" length="32" not-null="true"></column>
			</property>
			<property name="password" type="java.lang.String">
				<column name="password" length="32" not-null="true"></column>
			</property>
		</class>
	</hibernate-mapping>

LoginServiceAction.java

package com.struts.action;
import com.dao.UserDao;
import com.entity.User;
import com.opensymphony.xwork2.ActionSupport;

import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class LoginServiceAction extends ActionSupport{
	private String username;
	private String password;
	ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
	UserDao userDao=(UserDao)ctx.getBean("userDao");
	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 String execute() throws Exception{
		//查找账号相符的用户
		List<User> userList=userDao.findByName(username);
		//使用简化的for语句对集合进行遍历并比较用户的密码
		for(User user:userList) {
			if(user.getPassword().equals(password)) {
				return SUCCESS;
			}
			else {
				return ERROR;
			}
		}
		return ERROR;
	}
}

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
	<!-- 配置数据库 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
		<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test1"></property>
		<property name="user" value="root"></property>
		<property name="password" value="hjw19990825"></property>
		<!-- 指定连接数据库连接池的最大连接数 -->
		<property name="maxPoolSize" value="40"></property>
		<!-- 指定连接数据库连接池的最小连接数 -->
		<property name="minPoolSize" value="1"></property>
		<!-- 指定连接数据库连接池的初始化连接数 -->
		<property name="initialPoolSize" value="1"></property>
		<!-- 指定连接数据库连接池的链接的最大空闲时间 -->
		<property name="maxIdleTime" value="20"></property>
	</bean>
	<!-- 定义了Hibernate的SessionFactory -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource"></ref>
		</property>
		<property name="mappingResources">
			<list>
				<value>com.entity/User.hbm.xml</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>	
			</props>
		</property>
	</bean>
	<!-- HibernateTemplate类是简化Hibernate数据访问代码的辅助类,可以获取一个Sesison对象 -->
	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
		<property name="sessionFactory">
			<ref bean="sessionFactory"></ref>
		</property>
	</bean>
	<!-- 依赖注入 -->
	<bean id="userDao" class="com.dao.impl.UserDaoImpl">
		<!-- 注入持久化操作所需要的SessionFactory -->
		<property name="sessionFactory">
			<ref bean="sessionFactory"></ref>
		</property>
	</bean>
	</beans>

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="com.struts.avtion" extends="struts-default">
		<action name="login" class="com.struts.action.LoginServiceAction">
			<result name="success">/success.jsp</result>
			<result name="error">/login.jsp</result>
		</action>
	</package>
</struts>

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" >
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/applicationContext.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<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>
	
	<session-config>
		<session-timeout>
			60
		</session-timeout>
	</session-config>
	
	
</web-app>

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>登录页面</title>
	</head>
	<body>
		<h1>欢迎登录:</h1>
		<div>
			<s:form action="login" method="post">
			<br>
				<s:textfield name="username" label="账号"></s:textfield>
				<s:password name="password" label="密码"></s:password>
				<s:submit value="登录"></s:submit>
			</s:form>
		</div>
	</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>JSP Page</title>
	</head>
	<body>
		<h1>欢迎您,<s:property value="username"></s:property>,登录成功!!!</h1>
	</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章