基於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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章