环境:
1>Struts:2.3.20
2>Spring:4.3.5
3>hibernate:5.2.6
4>jdk:1.8
5>tomcat:8.0
1.框架构成图(简易版)
2.系统目录设计
3.数据库设计 一张表 SSHUSER 主健 ID
4.项目代码
com.liu.action
LoginAction
package com.liu.action;
import com.liu.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
/**
* 用户登陆控制器
*/
public class LoginAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private String username;
private String password;
private String usertype;
private UserService userService;
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
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 getUsertype() {
return usertype;
}
public void setUsertype(String usertype) {
this.usertype = usertype;
}
@Override
public String execute() {
clearActionErrors();
try{
if(userService.chkUser(username, password)!=null){
return SUCCESS;
}else{
addActionError("用户名或密码不正确!");
return INPUT;
}
}catch(Exception e){
e.printStackTrace();
return ERROR;
}
}
}
UserAction
package com.liu.action;
import com.liu.entity.User;
import com.liu.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
/**
* 用户追加控制器
*/
public class UserAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 2L;
private UserService userService;
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
@Override
public String execute() {
try{
User u = new User();
u.setName(user.getName());
u.setPassword(user.getPassword());
u.setType(user.getType());
userService.save(u);
return SUCCESS;
}catch(Exception e){
e.printStackTrace();
return ERROR;
}
}
}
UserDeleteAction
package com.liu.action;
import com.liu.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
/**
* 用户更新控制器
*/
public class UserDeleteAction extends ActionSupport{
private UserService userService;
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
/**
*
*/
private static final long serialVersionUID = 3L;
@Override
public String execute() {
try{
userService.delete(id);
return SUCCESS;
}catch(Exception e){
e.printStackTrace();
return ERROR;
}
}
}
UserInfoAction
package com.liu.action;
import org.apache.struts2.ServletActionContext;
import com.liu.entity.User;
import com.liu.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
/**
* 用户信息控制器
*/
public class UserInfoAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 7L;
private UserService userService;
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
public String execute() {
try{
User user = userService.findById(id);
ServletActionContext.getRequest().setAttribute("user", user);
return SUCCESS;
}catch(Exception e){
e.printStackTrace();
return ERROR;
}
}
}
UserQueryAction
package com.liu.action;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.liu.entity.User;
import com.liu.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
/**
* 用户查询控制器
*/
public class UserQueryAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 4L;
private UserService userService;
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
@Override
public String execute() {
try{
List<User> userlist = userService.findAll();
ServletActionContext.getRequest().setAttribute("userlist",userlist);
return SUCCESS;
}catch(Exception e){
return ERROR;
}
}
}
UserUpdateAction
package com.liu.action;
import com.liu.entity.User;
import com.liu.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
/**
* 用户更新控制器
*/
public class UserUpdateAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 5L;
private UserService userService;
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
@Override
public String execute() {
try{
if(userService.findById(user.getId())!=null){
userService.update(user);
return SUCCESS;
}
addActionMessage("user not exist!");
return INPUT;
}catch(Exception e){
e.printStackTrace();
return ERROR;
}
}
}
com.liu.dao
IBaseDAO
package com.liu.dao;
import java.util.List;
/**
* DAO基本操作接口
*/
public interface IBaseDAO<T> {
void save(T t) throws Exception;
void delete(T t) throws Exception;
void update(T t) throws Exception;
List<T> searchAll(String hql) throws Exception;
List<T> searchAllByCondition(String hql,String[] paramName,Object[] paramValue) throws Exception;
T searchOneByCondition(String hql,String[] paramName,Object[] paramValue) throws Exception;
}
BaseDAO
package com.liu.dao;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
/**
* DAO基本操作接口实现类
*/
@SuppressWarnings("all")
public abstract class BaseDAO<T> implements IBaseDAO<T>{
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public void save(T t) throws Exception {
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
session.save(t);
session.getTransaction().commit();
}
public void delete(T t) throws Exception {
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
session.delete(t);
session.getTransaction().commit();
}
public void update(T t) throws Exception {
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
session.update(t);
session.getTransaction().commit();
}
public List<T> searchAll(String hql) throws Exception {
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
List<T> resultList = (List<T>)session.createQuery(hql).getResultList();
session.getTransaction().commit();
return resultList;
}
public List<T> searchAllByCondition(String hql,String[] paramName, Object[] paramValue) throws Exception {
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
Query query = session.createQuery(hql);
for(int i=0;i<paramName.length;i++){
query.setParameter(paramName[i], paramValue[i]);
}
List<T> resultList = (List<T>)query.getResultList();
session.getTransaction().commit();
return resultList;
}
public T searchOneByCondition(String hql,String[] paramName, Object[] paramValue) throws Exception {
T t = null;
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
Query query = session.createQuery(hql);
for(int i=0;i<paramName.length;i++){
query.setParameter(paramName[i], paramValue[i]);
}
List list = query.getResultList();
if(!list.isEmpty()){
t = (T)list.get(0);
}
session.getTransaction().commit();
return t;
}
}
IUserDAO
package com.liu.dao;
import java.util.List;
import com.liu.entity.User;
/**
* UserDAO基本操作接口
*/
public interface IUserDAO extends IBaseDAO<User> {
void save(User user) throws Exception;
void delete(User user) throws Exception;
void update(User user) throws Exception;
User SearchOneById(int id) throws Exception;
User SearchOneByName(String name) throws Exception;
User SearchOneByCondition(String[] paramName,String[] paramValue) throws Exception;
List<User> searchAll() throws Exception;
}
UserDAO
package com.liu.dao;
import java.util.List;
import com.liu.entity.User;
import com.liu.util.UserUtil;
/**
* UserDAO实现类
*/
public class UserDAO extends BaseDAO<User> implements IUserDAO {
public List<User> searchAll() throws Exception {
String hql = "from User";
List<User> userList = super.searchAll(hql);
if(!userList.isEmpty()){
for(User user : userList){
user.setPassword(UserUtil.hiddenString(user.getPassword()));
}
}
return userList;
}
public User SearchOneById(int id) throws Exception {
String[] paramName = {"id"};
Integer[] paramValue = {id};
String hql = "from User where id=:id";
return this.searchOneByCondition(hql, paramName, paramValue);
}
public User SearchOneByName(String name) throws Exception {
String[] paramName = {"name"};
String[] paramValue = {name};
String hql = "from User where name=:name";
return this.searchOneByCondition(hql, paramName, paramValue);
}
public User SearchOneByCondition(String[] paramName, String[] paramValue) throws Exception {
StringBuilder hqlsb = new StringBuilder();
hqlsb.append("from User where ");
for(int i=0;i<paramName.length;i++){
if(i!=paramName.length-1){
hqlsb.append(paramName[i]+"=:" + paramName[i] + " and ");
}else{
hqlsb.append(paramName[i]+"=:" + paramName[i]);
}
}
return this.searchOneByCondition(hqlsb.toString(), paramName, paramValue);
}
@Override
public void save(User user) throws Exception {
user.setPassword(UserUtil.getBase64String(user.getPassword()));
super.save(user);
}
@Override
public void update(User user) throws Exception {
user.setPassword(UserUtil.getBase64String(user.getPassword()));
super.update(user);
}
}
com.liu.entity
User
package com.liu.entity;
import javax.persistence.Entity;
import javax.persistence.Id;
/**
* User表实例类
*/
@Entity
public class User {
@Id
private int id;
private String name;
private String password;
private String type;
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;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
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">
<!-- Generated 2017-1-12 16:56:19 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.liu.entity.User" table="SSHUSER">
<id name="id" type="int">
<column name="ID"/>
<generator class="identity"/>
</id>
<property generated="never" lazy="false" length="50" name="name" type="java.lang.String">
<column name="NAME"/>
</property>
<property generated="never" lazy="false" length="100" name="password" type="java.lang.String">
<column name="PASSWORD"/>
</property>
<property generated="never" lazy="false" length="10" name="type" type="java.lang.String">
<column name="TYPE"/>
</property>
</class>
</hibernate-mapping>
com.liu.service
IUserService
package com.liu.service;
import java.util.List;
import com.liu.entity.User;
/**
* UserService接口
*/
public interface IUserService {
void save(User user) throws Exception;
User getUser(String name) throws Exception;
void delete(int id) throws Exception;
void update(User user) throws Exception;
User findById(int id) throws Exception;
List<User> findAll() throws Exception;
User chkUser(String name,String password) throws Exception;
}
UserService
package com.liu.service;
import java.util.List;
import com.liu.dao.IUserDAO;
import com.liu.entity.User;
import com.liu.util.UserUtil;
/**
* UserService接口实现类
*/
public class UserService implements IUserService {
private IUserDAO userDAO;
public IUserDAO getUserDAO() {
return userDAO;
}
public void setUserDAO(IUserDAO userDAO) {
this.userDAO = userDAO;
}
public void save(User user) throws Exception {
if(userDAO!=null){
userDAO.save(user);
}
}
public User getUser(String name) throws Exception {
return userDAO.SearchOneByName(name);
}
public void delete(int id) throws Exception {
User user = userDAO.SearchOneById(id);
if(user!=null){
userDAO.delete(user);
}
}
public void update(User user) throws Exception {
if(userDAO.SearchOneById(user.getId())!=null){
userDAO.update(user);
}
}
public User findById(int id) throws Exception {
return userDAO.SearchOneById(id);
}
public List<User> findAll() throws Exception {
return userDAO.searchAll();
}
public User chkUser(String name, String password) throws Exception {
String[] paramName = {"name","password"};
String[] paramValue = {name,UserUtil.getBase64String(password)};
User user = userDAO.SearchOneByCondition(paramName, paramValue);
return user;
}
}
com.liu.util
UserUtil
package com.liu.util;
import java.security.*;
/**
* 工具类
*/
public class UserUtil {
public static String getBase64String(String inString){
try{
MessageDigest md = MessageDigest.getInstance("MD5");
sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
return encoder.encode(md.digest(inString.getBytes("utf8")));
}catch(Exception e){
return null;
}
}
public static String hiddenString(String inString){
return "***";
}
}
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>
<constant name="struts.objectFactory" value="spring" />
<constant name="struts.custom.i18n.resources" value="msgResource" />
<package name="default" namespace="/" extends="struts-default">
<action name="login" class="loginAction">
<result name="success" type="redirect">/userquery.action</result>
<result name="error">/error.jsp</result>
<result name="input">/login.jsp</result>
</action>
<action name="userquery" class="userQueryAction">
<result name="success">/query.jsp</result>
<result name="error">/error.jsp</result>
</action>
<action name="user" class="userAction">
<result name="success" type="redirect">/userquery.action</result>
<result name="error">/error.jsp</result>
</action>
<action name="userdelete" class="userDeleteAction">
<result name="success" type="redirect">/userquery.action</result>
<result name="error">/error.jsp</result>
</action>
<action name="userupdate" class="userUpdateAction">
<result name="success" type="redirect">/userquery.action</result>
<result name="error">/error.jsp</result>
<result name="input">/update.jsp</result>
</action>
<action name="userinfo" class="userInfoAction">
<result name="success">/update.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
<!--<constant name="struts.ui.theme" value="simple" />
<constant name="struts.ui.templateDir" value="template" />
<constant name="struts.ui.templateSuffix" value="ftl" />-->
</struts>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property>
<property name="url" value="jdbc:sqlserver://SKY-20140109AJI:1433;databaseName=fxcl"></property>
<property name="username" value="fxcl"></property>
<property name="password" value="fxcl"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/liu/entity/User.hbm.xml</value>
</list>
</property>
</bean>
<bean id="userDAO" class="com.liu.dao.UserDAO">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="userService" class="com.liu.service.UserService">
<property name="userDAO" ref="userDAO"></property>
</bean>
<!--login-->
<bean id="loginAction" class="com.liu.action.LoginAction">
<property name="userService" ref="userService"></property>
</bean>
<!--userquery-->
<bean id="userQueryAction" class="com.liu.action.UserQueryAction">
<property name="userService" ref="userService"></property>
</bean>
<!--user-->
<bean id="userAction" class="com.liu.action.UserAction">
<property name="userService" ref="userService"></property>
</bean>
<!--delete-->
<bean id="userDeleteAction" class="com.liu.action.UserDeleteAction">
<property name="userService" ref="userService"></property>
</bean>
<!--update-->
<bean id="userInfoAction" class="com.liu.action.UserInfoAction">
<property name="userService" ref="userService"></property>
</bean>
<bean id="userUpdateAction" class="com.liu.action.UserUpdateAction">
<property name="userService" ref="userService"></property>
</bean>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>UserManagerMent</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
jsp
error.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>错误</title>
</head>
<body>
系统出现了错误,请联系系统管理员<br>
<s:property value="#exception.message"/>
</body>
</html>
login.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>用户登陆</title>
</head>
<body>
<center>
<s:form action="login" method="post" namespace="/">
<tr>
<td colspan="2" align="center">
<h1>用户登陆</h1><br>
</td>
</tr>
<s:textfield name ="username" label="用户名"></s:textfield>
<s:password name="password" label="密码"></s:password>
<s:submit value="登陆 "></s:submit>
<tr>
<td colspan="2" align="center">
<s:actionerror />
</td>
</tr>
</s:form>
</center>
</body>
</html>
query.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>显示用户信息</title>
</head>
<body>
<center>
<h1>用户信息</h1>
<table border="1" width="600">
<tr>
<th>用户ID</th>
<th>用户名</th>
<th>密码</th>
<th>用户类型</th>
<th>删除操作</th>
<th>更新操作</th>
</tr>
<s:iterator value ="#request.userlist" id="ul">
<tr>
<td><s:property value="#ul.id"></s:property></td>
<td><s:property value="#ul.name"></s:property></td>
<td><s:property value="#ul.password"></s:property></td>
<td><s:property value="#ul.type"></s:property></td>
<td><a href="userdelete.action?id=<s:property value='#ul.id' />">删除</a></td>
<td><a href="userinfo.action?id=<s:property value='#ul.id' />">更新</a></td>
</tr>
</s:iterator>
</table>
<br>
<a href="save.jsp">添加用户</a>
</center>
</body>
</html>
save.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>添加用户</title>
</head>
<body>
<center>
<s:form action="user" method="post" namespace="/" >
<tr>
<td colspan="2" align="center">
<h1>欢迎注册</h1><br>
<s:property value="#exception.message"/>
</td>
</tr>
<s:textfield name="user.name" label="用户名" required="true" />
<s:textfield name="user.password" label="密码" required="true" />
<s:textfield name="user.type" label="类型" required="true" />
<s:submit value="注册"></s:submit>
</s:form>
</center>
</body>
</html>
update.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>修改用户信息</title>
</head>
<body>
<center>
<s:form action="userupdate" method="POST" namespace="/">
<tr>
<td colspan ="2" align="center">
<h1>修改用户信息</h1>
<s:actionerror />
</td>
</tr>
<s:hidden name="user.id" value="%{#request.user.id}" />
<s:textfield name="user.name" label="用户名" required="true" value="%{#request.user.name}" />
<s:textfield name="user.password" label="密码" required="true" />
<s:textfield name="user.type" label="类型" required="true" value="%{#request.user.type}" />
<s:submit value="更新"></s:submit><s:reset value="重置"></s:reset>
</s:form>
</center>
</body>
</html>
项目效果图