SSH+JSP+MYSQL(spring4.2.4+struts2.3.24+hibernate4.1.7)登录以及简单增删查改的网站实例

1 篇文章 0 订阅

一、项目介绍

搭建ssh框架花了我差不多一个星期的时间。。。最终发现可能是我导入的包太多了,有一些包导入了如果没用到是会报错的。查了无数次的百度才终于搭好ssh框架。
此项目为实现基本的用户登录以及用户的增删查改操作,如有不对欢迎各位指错,谢谢。

二、下载ssh的jar包

本项目使用的ssh版本为spring4.2.4+struts2.3.24+hibernate4.1.7,服务器为Tomcat8.5。

ssh文件下载:https://download.csdn.net/download/w77775292/10824979

Tomcat8.5服务器官网下载:https://tomcat.apache.org/download-80.cgi

三、运行环境

jdk8+tomcat8.5+mysql+eclipse+ssh(spring4.2.4+struts2.3.24+hibernate4.1.7)

四、项目流程

1.项目结构

项目结构
结构

2.配置文件

1.spring配置(ApplicationContext.xml)

1.添加命名空间

<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring配置文件的根元素 -->
<beans xmlns="http://www.springframework.org/schema/beans"
	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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!--添加bean-->

</beans>

2.配置DataSource

<bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <!-- 配置数据库JDBC驱动 -->
        <property name="driverClassName">
            <value>com.mysql.jdbc.Driver</value>
        </property>
        <!-- 配置数据库连接URL -->
        <property name="url" value="jdbc:mysql://localhost:3306/sshdb?useUnicode=true&amp;characterEncoding=utf-8"></property>
        <!-- 配置数据库用户名 -->
        <property name="username">
            <value>root</value>
        </property>
        <!-- 配置数据库密码 -->
        <property name="password">
            <value>root</value>
        </property>
    </bean>

3.配置SessionFactory

<bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource"/>
        </property>
        <!-- 配置Hibernate属性 -->
        <property name="hibernateProperties">
            <props>
                <!-- 配置数据库方言 -->
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.MySQL5InnoDBDialect
                </prop>
                <!-- 输出运行时生成的SQL语句 -->
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="connection.autocommit">true</prop>
            </props>
        </property>
        <!-- 指定Hibernate映射文件的路径 -->
        <property name="mappingResources">
            <list>
                <value>com/entity/Custom.hbm.xml</value>
            </list>
        </property>
    </bean>

在Spring容器中注册所有的Bean

    <!-- 配置CustomDao -->
    <bean id="customDao" class="com.dao.impl.CustomDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    
    <!-- 配置CustomService -->
    <bean id="customService" class="com.service.impl.CustomServiceImpl">
        <!-- 注入Dao组件 -->
        <property name="customDao" ref="customDao"></property>
    </bean>
    
    <!-- 创建loginAction实例 -->
    <bean id="loginAction" class="com.action.LoginAction">
        <!-- 注入service组件 -->
        <property name="customService" ref="customService"></property>
    </bean>
    
    <!-- 创建queryAction实例 -->
    <bean id="queryAction" class="com.action.QueryAction">
    	<property name="customService" ref="customService"></property>
    </bean>
    
    <!-- 创建addAction实例 -->
    <bean id="addAction" class="com.action.AddAction">
    	<property name="customService" ref="customService"></property>
    </bean>
    
    <!-- 创建deleteAction实例 -->
    <bean id="deleteAction" class="com.action.DeleteAction">
    	<property name="customService" ref="customService"></property>
    </bean>
    
    <!-- 创建updateAction实例 -->
    <bean id="updateAction" class="com.action.UpdateAction">
    	<property name="customService" ref="customService"></property>
    </bean>

2.struts配置(struts.xml)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC  "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  
	"http://struts.apache.org/dtds/struts-2.3.dtd">
    <!-- struts标签 -->
    <struts>
    	<!-- <constant name="struts.convention.result.path" value="/WEB-INF/jsp" /> -->
    	<constant name="struts.devMode" value="true"></constant><!-- 打开开发模式 -->
    	<constant name="struts.objectFacotry" value="spring"></constant><!-- 引入spring框架 -->
    	<!-- 定义包 -->
    	<package name="default" namespace="/" extends="struts-default">
    	
    	    <action name="login" class="loginAction">
    	        <!-- 配置处理结果与视图资源的关系 -->
    	        <result name="success" type="redirect">query</result>
    	        <result name="failure">/error.jsp</result>
    	    </action>
    	    
    	    <action name="add" class="addAction">
    	    	<result name="SUCCESS" type="redirect">query</result>
    	    </action>
    	    
    	    <action name="query" class="queryAction">
    	    	<result name="success">/query.jsp</result>
    	    </action>
    	    
    	    <action name="delete" class="deleteAction">
    	    	<result name="success" type="redirect">query</result>
    	    </action>
    	    
    	    <action name="update" class="updateAction">
    	    	<result name="SUCCESS" type="redirect">query</result>
    	    	<result name="INPUT">/update.jsp</result>
    	    </action>
    	</package>
    </struts>

3.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_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>custom management</display-name>
	<welcome-file-list>
		<welcome-file>login.jsp</welcome-file>
	</welcome-file-list>
	
	<filter>
	    <filter-name>hibernateOpenSessionInViewFilter</filter-name>
	    <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
	    <init-param>
	        <param-name>flushMode</param-name>
	        <param-value>AUTO</param-value>
	    </init-param>
	</filter>
	
	<filter-mapping>
	    <filter-name>hibernateOpenSessionInViewFilter</filter-name>
	    <url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<filter>
	    <filter-name>struts2</filter-name>	<!-- 定义核心Filter名称 -->
	    <filter-class>	<!-- 定义核心Filter的实现类 -->
	        org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
	    </filter-class>
	</filter>
	<filter-mapping>
	    <filter-name>struts2</filter-name>	<!-- 核心Filter名称 -->
	    <url-pattern>/*</url-pattern>	<!-- 配置路径 -->
	</filter-mapping>
	
	<!-- 配置Listener -->
	<listener>
	    <listener-class>
	        org.springframework.web.context.ContextLoaderListener
	    </listener-class>
	</listener>
	
	<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>classpath:ApplicationContext.xml</param-value> 
  </context-param>
</web-app>

4.java代码

com.dao包中CustomDao接口

package com.dao;

import java.util.List;
import com.entity.Custom;

public interface CustomDao {
	public void save(Custom custom);	//添加客户
	public List<Custom> getCustom(String name);	//通过客户名查找客户
	public void delete(int id);	//删除客户
	public void update(Custom custom);	//更新客户
	public Custom findById(int id);	//按id查找客户
	public List<Custom> finAll();	//查找全部客户
}

com.dao.impl包CustomDao实现接口CustomDaoImpl类

package com.dao.impl;

import java.util.List;

import org.hibernate.FlushMode;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate4.HibernateTemplate;

import com.dao.CustomDao;
import com.entity.Custom;

public class CustomDaoImpl implements CustomDao {

	private SessionFactory sessionFactory;	//spring注入sessionFactory
	private HibernateTemplate ht;	//注入hibernate模板
	
	
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public HibernateTemplate getHt() {
		if(ht == null)
		{
			ht = new HibernateTemplate(sessionFactory);
		}
		return ht;
	}

	public void setHt(HibernateTemplate ht) {
		this.ht = ht;
	}

	@Override
	public void save(Custom custom) {
		ht.getSessionFactory().getCurrentSession().setFlushMode(FlushMode.AUTO);
		getHt().save(custom);
		getHt().flush();

	}

	@SuppressWarnings("unchecked")	//对被批注的代码元素内部某些警告保持静默
	@Override
	public List<Custom> getCustom(String name) {
		String hsql = "from Custom where name = '" + name + "'";
		List<Custom> result = (List<Custom>) getHt().find(hsql);
		return result;
	}

	@Override
	public void delete(int id) {
		ht.getSessionFactory().getCurrentSession().setFlushMode(FlushMode.AUTO);
		getHt().delete(findById(id));
		getHt().flush();
	}

	@Override
	public void update(Custom custom) {
		ht.getSessionFactory().getCurrentSession().setFlushMode(FlushMode.AUTO);
		getHt().merge(custom);
		getHt().flush();
	}

	@Override
	public Custom findById(int id) {
		Custom custom = getHt().get(Custom.class, new Integer(id));
		return custom;
	}

	@SuppressWarnings("unchecked")	//对被批注的代码元素内部某些警告保持静默
	@Override
	public List<Custom> finAll() {
		String hsql = "from Custom";
		List<Custom> list = (List<Custom>) getHt().find(hsql);
		return list;
	}
}

com.service包中CustomService接口

package com.service;

import java.util.List;

import com.entity.Custom;

public interface CustomService {
	void saveCustom(Custom custom);		//添加客户
	List<Custom> findCustomByName(String name);	//按客户名查找客户
	void deleteCustom(int id);	//删除客户
	void updateCustom(Custom custom);	//更新客户
	Custom findCustomById(int id);	//按id查找客户
	List<Custom> findAll();	//查找全部客户
}

com.service.impl包CustomService实现接口CustomServiceImpl类

package com.service.impl;

import java.util.List;

import com.dao.CustomDao;
import com.entity.Custom;
import com.service.CustomService;

public class CustomServiceImpl implements CustomService {
	private CustomDao customDao;

	public CustomDao getCustomDao() {
		return customDao;
	}

	public void setCustomDao(CustomDao customDao) {
		this.customDao = customDao;
	}

	@Override
	public void saveCustom(Custom custom) {
		if(customDao.findById(custom.getCid()) == null)
		{
			customDao.save(custom); 	//调用Dao对象保存
		}
	}

	@Override
	public List<Custom> findCustomByName(String name) {
		return customDao.getCustom(name);	//调用Dao组件查询
	}

	@Override
	public void deleteCustom(int id) {
		if(customDao.findById(id) != null)
		{
			customDao.delete(id);	//调用Dao组件删除
		}
	}

	@Override
	public void updateCustom(Custom custom) {
		if(customDao.findById(custom.getCid()) != null)
		{
			customDao.update(custom); 	//调用Dao对象更新
		}

	}

	@Override
	public Custom findCustomById(int id) {
		return customDao.findById(id);	//调用Dao组件查询
	}

	@Override
	public List<Custom> findAll() {
		return customDao.finAll();	//调用Dao组件查询
	}
}

com.entity包下Custom实体类

package com.entity;

public class Custom {
	private int cid;
	private String name;
	private String mobile;
	private String email;
	private String password;
	public int getCid() {
		return cid;
	}
	public void setCid(int cid) {
		this.cid = cid;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getMobile() {
		return mobile;
	}
	public void setMobile(String mobile) {
		this.mobile = mobile;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

action的LoginAction类

package com.action;

import java.util.Iterator;
import java.util.List;

import com.entity.Custom;
import com.opensymphony.xwork2.ActionSupport;
import com.service.CustomService;

public class LoginAction extends ActionSupport{
	private String name;
	private String password;
	
	private CustomService customService;
	
	@Override
	public String execute()
	{
		List<Custom> list = customService.findCustomByName(name);
		Iterator<Custom> it = list.iterator();
		while(it.hasNext())
		{
			Custom custom = it.next();
			if(name.trim().equals(custom.getName()) && 
					password.trim().equals(custom.getPassword())) 
			{
				return "success";
			}
		}
		return "failure";
	}

	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 CustomService getCustomService() {
		return customService;
	}

	public void setCustomService(CustomService customService) {
		this.customService = customService;
	}
	
	
}

添加客户AddAction类

package com.action;

import com.entity.Custom;
import com.opensymphony.xwork2.ActionSupport;
import com.service.CustomService;

public class AddAction extends ActionSupport {
	private CustomService customService;	//业务逻辑组件
											//设置业务逻辑组件

	private Custom newcustom;	//新用户
	
	public CustomService getCustomService() {
		return customService;
	}

	public void setCustomService(CustomService customService) {
		this.customService = customService;
	}

	public Custom getNewcustom() {
		return newcustom;
	}

	public void setNewcustom(Custom newcustom) {
		this.newcustom = newcustom;
	}
	
	@Override
	public String execute() throws Exception {
		Custom custom = new Custom();
		custom.setName(newcustom.getName());
		custom.setPassword(newcustom.getPassword());
		custom.setMobile(newcustom.getMobile());
		custom.setEmail(newcustom.getEmail());
		customService.saveCustom(custom);
		
		return "SUCCESS";
	}
	
}

查询客户QueryAction类

package com.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.entity.Custom;
import com.opensymphony.xwork2.ActionSupport;
import com.service.CustomService;

public class QueryAction extends ActionSupport{
	private CustomService customService;

	public CustomService getCustomService() {
		return customService;
	}

	public void setCustomService(CustomService customService) {
		this.customService = customService;
	}
	
	@Override
	public String execute() throws Exception {
		
		List<Custom> list = customService.findAll();
		ServletActionContext.getRequest().setAttribute("customlist", list);	//把所有客户存放在request范围内
		return "success";
	}
}

修改客户UpdateAction类

package com.action;

import com.entity.Custom;
import com.opensymphony.xwork2.Action;
import com.service.CustomService;

public class UpdateAction implements Action {
	private CustomService customService;
	private Custom custom;
	private int cid;
	
	public Custom getCustom() {
		return custom;
	}
	public void setCustom(Custom custom) {
		this.custom = custom;
	}
	public CustomService getCustomService() {
		return customService;
	}
	public void setCustomService(CustomService customService) {
		this.customService = customService;
	}
	public int getCid() {
		return cid;
	}
	public void setCid(int cid) {
		this.cid = cid;
	}
	
	@Override
	public String execute() throws Exception {
		if(customService.findCustomById(custom.getCid()) != null)	//如果存在该id:
		{
			customService.updateCustom(custom);
			return "SUCCESS";
		}
		return "INPUT";
	}
}

删除客户DeleteAction类

package com.action;

import com.opensymphony.xwork2.ActionSupport;
import com.service.CustomService;

public class DeleteAction extends ActionSupport {
	private CustomService customService;
	private int cid;
	public CustomService getCustomService() {
		return customService;
	}
	public void setCustomService(CustomService customService) {
		this.customService = customService;
	}
	public int getCid() {
		return cid;
	}
	public void setCid(int cid) {
		this.cid = cid;
	}
	
	@Override
	public String execute() throws Exception {
		customService.deleteCustom(cid);
		return "success";
	}
}

5.jsp代码

login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html><!--Bootstrap 使用到的某些 HTML 元素和 CSS 属性需要将页面设置为 HTML5 文档类型-->
<html lang="zh-CN">
<head>
  <title>客户登录</title>
</head>
<body>
<s:form action="login" method="post">
<s:textfield name="name" label="姓名"></s:textfield>
<s:password name="password" label="密码"></s:password>
<s:submit value="提交"></s:submit>
</s:form>
</body>
</html>
add.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html><!--Bootstrap 使用到的某些 HTML 元素和 CSS 属性需要将页面设置为 HTML5 文档类型-->
<html lang="zh-CN">
<head>
  <title>注册</title>
</head>
<body>
	<s:form action="add" method="post">
		<!-- 发送给名为add的action -->
		<!-- 定义表单 -->
		<tr>
			<td colspan="2" align="center">
				<h1>
					<s:text name="欢迎注册"></s:text>
				</h1>
				<br>
				<s:property value="exception.message"/>
			</td>
		</tr>
		<s:textfield name="newcustom.name" label="姓名" required="true"></s:textfield>
		<s:textfield name="newcustom.password" label="密码" required="true"></s:textfield>
		<s:textfield name="newcustom.mobile" label="电话" required="true"></s:textfield>
		<s:textfield name="newcustom.email" label="邮箱" required="true"></s:textfield>
		<s:submit value="提交"></s:submit>
	</s:form>
</body>
</html>
update.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html><!--Bootstrap 使用到的某些 HTML 元素和 CSS 属性需要将页面设置为 HTML5 文档类型-->
<html lang="zh-CN">
<head>
  <title>更新</title>
</head>
<body>
	<s:form action="update" method="post">
		<!-- 定义表单 -->
		<tr>
			<td colspan="2" align="center">
				<h1>
					<s:text name="修改客户信息"></s:text>
				</h1>
				<br>
				<s:actionerror/>	<!-- 格式化文本数据 -->
			</td>
		</tr>
		<td>客户ID:<s:property value="#parameters.cid" /></td>
		<s:textfield name="custom.cid" key="客户ID" required="true"></s:textfield>
		<s:textfield name="custom.name" key="姓名" ></s:textfield>
		<s:password name="custom.password" key="密码" ></</s:password>
		<s:textfield name="custom.mobile" key="电话" ></s:textfield>
		<s:textfield name="custom.email" key="邮箱" ></s:textfield>
		<s:submit value="提交"></s:submit>
		<s:reset value="重置"></s:reset>
		<s:set></s:set>
		
	</s:form>
</body>
</html>
query.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html><!--Bootstrap 使用到的某些 HTML 元素和 CSS 属性需要将页面设置为 HTML5 文档类型-->
<html lang="zh-CN">
<head>
  <title>显示客户信息</title>
</head>
<body>
	<h1>客户信息</h1>
	<table border="1">
		<tr>
			<th>客户ID</th>
			<th>客户名</th>
			<th>客户电话</th>
			<th>客户邮箱</th>
			<th>是否删除</th>
			<th>是否修改</th>
		</tr>
		<s:iterator value="#request.customlist" id="st">
			<!-- 对集合元素迭代 -->
			<tr>
				<td><s:property value="#st.cid"/></td>
				<!-- 定义单元格 -->
				<td><s:property value="#st.name"/></td>
				<td><s:property value="#st.mobile"/></td>
				<td><s:property value="#st.email"/></td>
				<td><a href="delete.action?cid=<s:property value='#st.cid'/>">删除</a></td>
				<td><a href="update.jsp?cid=<s:property value='#st.cid'/>">修改</a></td>
			</tr>
		</s:iterator>
	</table>
	<br>
	<s:a href="add.jsp">添加客户</s:a>
</body>
</html>
error.jsp(用来登录信息错误时显示的界面)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html><!--Bootstrap 使用到的某些 HTML 元素和 CSS 属性需要将页面设置为 HTML5 文档类型-->
<html lang="zh-CN">
<head>
  <title>出错</title>
</head>
<body>
	出错,请<s:a href="/add.jsp">注册</s:a>或返回
</body>
</html>

五、运行截图

登录界面 (login.jsp)

登录

查询界面(query.jsp)

查询

增加页面(add.jsp)

注册

修改界面(update.jsp)

修改

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值