本篇只涉及到两个实体类 Administer 和 Book ,两者之间没什么联系,不同用户登陆之后看到的/操作的都是一样的book列表,但实际业务中,针对于不同用户登陆后所看到的和操作的都应该是不一样的。本篇作为过渡,下一篇将实现一个简单的个人博客项目,相当于“一对多”(User 和 Blog),还会涉及到拦截器,注册验证(Ajax)。
有什么问题欢迎到评论区评论,我必将及时回复!!!
导包和配置详解请见我的上一篇博客。本篇基本上就是代码(为缩减篇幅,Java中导包即import语句全部省略)!
下图是我随手画的一张分析图:
●项目目录结构 (src+WebContent)
●web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>SSHDemo</display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<!-- spring -->
<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>
<!-- 扩大session作用范围 -->
<filter>
<filter-name>openSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- struts2 -->
<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>
</web-app>
●applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
<!-- 配置核心事务管理器 -->
<bean name="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 以后配置大量事务时,需要使用通配符配置增删改查 -->
<!-- 以方法为单位,指定方法应该有什么事务属性,isolation隔离级别,propagation传播行为,read-only是否只读 -->
<tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="add*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="motified*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="remove*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="get*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/>
<tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置将通知织入目标对象 -->
<aop:config>
<!-- 配置切点 -->
<aop:pointcut expression="execution(* com.yx.service.impl.*ServiceImpl.*(..))" id="txPointCut"/>
<!-- 配置切面 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config>
<!-- 配置hibernate基本信息-->
<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 将连接池注入到sessionFactory中,hibernate会通过连接池 获得连接-->
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<!-- 数据库方言必须配置,下面三个可选 -->
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- 引入ORM元数据,指定ORM元数据所在的包路径,Spring会自动读取包中的所有配置 -->
<property name="mappingDirectoryLocations" value="classpath:com/yx/domain"></property>
</bean>
<!-- 指定Spring读取db.properties配置 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置c3p0连接池 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- action -->
<!-- 注意,Action对象作用范围一定是多例的,这样才符合Struts2架构 -->
<bean name="administerAction" class="com.yx.action.AdministerAction" scope="prototype">
<property name="administerService" ref="administerService"></property>
</bean>
<bean name="bookAction" class="com.yx.action.BookAction" scope="prototype">
<property name="bookService" ref="bookService"></property>
</bean>
<!-- service层 -->
<bean name="administerService" class="com.yx.service.impl.AdministerServiceImpl">
<!-- 注入administerDao -->
<property name="administerDao" ref="administerDao"></property>
</bean>
<bean name="bookService" class="com.yx.service.impl.BookServiceImpl">
<!-- 注入bookDao -->
<property name="bookDao" ref="bookDao"></property>
</bean>
<!-- dao层 -->
<bean name="administerDao" class="com.yx.dao.impl.AdministerDaoImpl">
<!-- 注入sessionFactory -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean name="bookDao" class="com.yx.dao.impl.BookDaoImpl">
<!-- 注入sessionFactory -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
●db.properties,连接数据库信息
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///test
jdbc.user=root
jdbc.password=123456
●实体类Administer和Book(为缩减篇幅,get和set、toString方法在此省略)
● ORM(Administer.hbm.xml 和 Book.hbm.xml)
● 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.objectFactory = spring ——将Action的创建交给Spring(默认关闭)
struts.objectFactory.spring.autoWire = name ——Spring负责自动装配Action的依赖属性-->
<constant name="struts.objectFactory" value="spring"></constant>
<package name="SHHDemo" namespace="/" extends="struts-default">
<global-exception-mappings>
<exception-mapping result="error" exception="java.lang.RuntimeException"></exception-mapping>
</global-exception-mappings>
<action name="AdministerAction_*" class="administerAction" method="{1}">
<result name="toHome" type="redirect">/index.jsp</result>
<result name="error">/login.jsp</result>
</action>
<action name="BookAction_*" class="bookAction" method="{1}">
<!-- 分页显示所有图书信息 -->
<result name="BOOKLIST">/show.jsp</result>
<!-- 调到修改页面时,书的详细信息 -->
<result name="BOOKDETAIL">/update.jsp</result>
<!-- 修改 -->
<result name="UPDATESUCCESS" type="redirect">BookAction_findBookList</result>
<!-- 添加 -->
<result name="ADDSUCCESS" type="redirect">BookAction_findBookList</result>
<!-- 删除,重定向到show -->
<result name="DELETESUCCESS" type="redirect">BookAction_findBookList</result>
</action>
</package>
</struts>
●登录的AdministerAction类:
package com.yx.action;
public class AdministerAction extends ActionSupport implements ModelDriven<Administer> {
private IAdministerService administerService;
private Administer administer = new Administer();
public void setAdministerService(IAdministerService administerService) {
this.administerService = administerService;
}
public String login() {
//1.调用Service执行登录逻辑
Administer admin = administerService.getByCodePassword(administer);
//2.将返回的User对象放入session域
ActionContext.getContext().getSession().put("administer", admin);
//3.重定向到项目首页
return "toHome";
}
@Override
public Administer getModel() {
// TODO Auto-generated method stub
return administer;
}
}
●Administer在Service层的接口和实现类
AdministerServiceImpl
package com.yx.service.impl;
public class AdministerServiceImpl implements IAdministerService {
private IAdministerDao administerDao;
public void setAdministerDao(IAdministerDao administerDao) {
this.administerDao = administerDao;
}
//登录时验证管理员的身份和密码
@Override
public Administer getByCodePassword(Administer administer) {
//1.根据登录名称查询登录用户
Administer existAdminister = administerDao.getByAdministerCode(administer.getAdminister_code());
//2.判断用户是否存在,不存在=》抛出异常,提示用户名不存在
if(existAdminister == null) {
throw new RuntimeException("该管理员不存在!");
}
//3.判断用户密码是否正确,不正确=》抛出异常,提示密码错误
if(!existAdminister.getAdminister_password().equals(administer.getAdminister_password())){
throw new RuntimeException("您的密码错误,请重试!");
}
//4.返回查询到的用户对象
return existAdminister;
}
}
●Administer在Dao层的接口和实现类
AdministerDaoImpl
package com.yx.dao.impl;
public class AdministerDaoImpl extends HibernateDaoSupport implements IAdministerDao {
//查找id为administer_code的对象
@Override
public Administer getByAdministerCode(String administer_code) {
return getHibernateTemplate().execute(new HibernateCallback<Administer>() {
@Override
public Administer doInHibernate(Session session) throws HibernateException {
String hql = "from Administer where administer_code = ?";
Query query = session.createQuery(hql);
query.setParameter(0, administer_code);
Administer administer = (Administer) query.uniqueResult();
return administer;
}
});
}
}
●增删改、分页查询的BookAction类:
package com.yx.action;
public class BookAction extends ActionSupport {
private IBookService bookService;
private int currentPage; //当前页
private List<Book> booklist;
private Book book;
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
public List<Book> getBooklist() {
return booklist;
}
public void setBooklist(List<Book> booklist) {
this.booklist = booklist;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public void setBookService(IBookService bookService) {
this.bookService = bookService;
}
//获取所有图书列表,并分页显示
public String findBookList() {
booklist = bookService.getBookList(currentPage);
ActionContext.getContext().put("bar", bookService.getBar(currentPage));
return "BOOKLIST";
}
public String add() {//添加一本书
System.out.println(111);
bookService.addABook(book);
System.out.println(222);
return "ADDSUCCESS";
}
public String delete() { //删除一本书(指定id)
bookService.deleteBookById(book.getBook_id());
return "DELETESUCCESS";
}
public String detail() { //找一本书(指定id)
book = bookService.findBookById(book.getBook_id());
return "BOOKDETAIL";
}
public String update() { //更新找到的这本书
bookService.updateBookTable(book);
System.out.println("update222");
return "UPDATESUCCESS";
}
}
●Book在Service层的接口和实现类
BookServiceImpl
package com.yx.service.impl;
public class BookServiceImpl implements IBookService {
private IBookDao bookDao;
public static int PAGESIZE = 7;
public void setBookDao(IBookDao bookDao) {
this.bookDao = bookDao;
}
//查询图书的总记录数
@Override
public int getCount() {
int count = bookDao.getCount();
return count;
}
//根据图书总记录数,算出需要显示的总页数
@Override
public int pageNum() {
int c = getCount();
return c%PAGESIZE == 0 ? c/PAGESIZE : (c/PAGESIZE+1);
}
//页面下方页码超链接
@Override
public String getBar(int cpage) {
String string = "";
for(int i=1;i<=pageNum();i++) {
if(i != cpage) {
string += "<a href='BookAction_findBookList?currentPage="+i+"'>"+i+"</a> ";
}else {
string += i + " ";
}
}
return string;
}
//分页获取图书列表
@Override
public List<Book> getBookList(int currentPage) {
List<Book> books = bookDao.findBookPaging(currentPage, PAGESIZE);
return books;
}
//添加一本书
@Override
public void addABook(Book book) {
bookDao.addABook(book);
}
//按照图书编号删除一本书
@Override
public void deleteBookById(Long book_id) {
bookDao.deleteABook(book_id);
}
//按照图书编号找一本书
@Override
public Book findBookById(Long book_id) {
Book book = bookDao.findBookById(book_id);
return book;
}
//修改
@Override
public void updateBookTable(Book book) {
bookDao.updateBook(book);
}
}
●Book在Dao层的接口和实现类
BookDaoImpl
package com.yx.dao.impl;
public class BookDaoImpl extends HibernateDaoSupport implements IBookDao {
//分页查询
@Override
public List<Book> findBookPaging(final int page, final int pageSize) {
List<Book> list = (List<Book>)getHibernateTemplate().execute(new HibernateCallback<Object>() {
@Override
public Object doInHibernate(Session session) throws HibernateException {
//在方法体中使用session对象
String hql = "from Book";
Query query = session.createQuery(hql);
int begin = (page - 1)*pageSize;
query.setFirstResult(begin);
query.setMaxResults(pageSize);
return query.list();
}
});
return list;
}
//查询图书的总记录数
@Override
public int getCount() {
String hql = "select count(*) from Book";
List list = getHibernateTemplate().find(hql);
int count = Integer.parseInt(list.get(0).toString());
return count;
}
@Override
public void addABook(Book book) {
getHibernateTemplate().save(book);
}
@Override
public void deleteABook(Long book_id) {
Book book = getHibernateTemplate().get(Book.class, book_id);
getHibernateTemplate().delete(book);
}
@Override
public Book findBookById(Long book_id) {
Book book = getHibernateTemplate().get(Book.class, book_id);
return book;
}
@Override
public void updateBook(Book book) {
getHibernateTemplate().update(book);
}
}
下面是jsp
●登录 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>login页面</title>
</head>
<body>
<h3 align="center">管理员登录界面</h3>
<form action="${pageContext.request.contextPath}/AdministerAction_login" method="post">
<table border="1" bgcolor="pink" align="center">
<tr>
<td>管理员账号:</td>
<td><input type="text" name="administer_code" value="${administer_code}"/></td>
</tr>
<tr>
<td>管理员密码:</td>
<td><input type="password" name="administer_password" value="${administer_password}"/></td>
</tr>
</table>
<p align="center">
<input type="submit" value="登录">
<input type="reset" value="重置">
</p>
<p align="center"><font color="red"><s:property value="exception.message"/></font></p>
</form>
</body>
</html>
index.jsp相当于一个过渡页面,登录成功后跳转到index页,通过超链接并带上currentPage=1的参数跳转到分页查询页!
●show.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>show</title>
</head>
<body>
<h2 align="center">图书信息</h2>
<table border="1" align="center" width="70%">
<tr bgcolor="Coral" align="center">
<th>图书ID</th>
<th>图书编号</th>
<th>图书名称</th>
<th>图书库存</th>
<th>图书价格</th>
<th>图书作者</th>
<th colspan="2">操 作</th>
</tr>
<s:iterator value="booklist">
<tr align="center" bgcolor="pink">
<td>${book_id}</td>
<td><s:property value="book_code"/></td>
<td>《<s:property value="book_name"/>》</td>
<td><s:property value="book_count"/></td>
<td><s:property value="book_price"/></td>
<td><s:property value="book_author"/></td>
<td><a href="${pageContext.request.contextPath}/BookAction_detail?book.book_id=${book_id}">修改</a></td>
<td><a href="${pageContext.request.contextPath}/BookAction_delete?book.book_id=${book_id}">删除</a></td>
</tr>
</s:iterator>
</table>
<p align="center"><a href="${pageContext.request.contextPath}/add.jsp">添加图书</a></p>
<br>
<p align="center">${bar}</p>
</body>
</html>
●add.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>add</title>
</head>
<body>
<h2 align="center">添加图书</h2>
<form action="${pageContext.request.contextPath}/BookAction_add" method="post">
<table align="center" bgcolor="pink">
<tr>
<td bgcolor="Coral">图书编号:</td>
<td><input name="book.book_code" type="text"> </td>
</tr>
<tr>
<td bgcolor="Coral">图书名称:</td>
<td><input name="book.book_name" type="text"></td>
</tr>
<tr>
<td bgcolor="Coral">图书库存:</td>
<td><input name="book.book_count" type="text"></td>
</tr>
<tr>
<td bgcolor="Coral">图书价格:</td>
<td><input name="book.book_price" type="text"></td>
</tr>
<tr>
<td bgcolor="Coral">图书作者:</td>
<td><input name="book.book_author" type="text"></td>
</tr>
</table>
<p align="center">
<input type="submit" value="添加">
<input type="reset" value="重置">
</p>
</form>
</body>
</html>
●update.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>update</title>
</head>
<body>
<h2 align="center">修改图书信息</h2>
<form action="${pageContext.request.contextPath}/BookAction_update" method="post">
<table align="center" bgcolor="pink">
<tr>
<td bgcolor="Coral">图书ID:</td>
<td align="center">
<input name="book.book_id" type="text" value="${book.book_id}">
</td>
</tr>
<tr>
<td bgcolor="Coral">图书编号:</td>
<td><input name="book.book_code" type="text" value="${book.book_code}"></td>
</tr>
<tr>
<td bgcolor="Coral">图书名称:</td>
<td><input name="book.book_name" type="text" value="${book.book_name}"></td>
</tr>
<tr>
<td bgcolor="Coral">图书库存:</td>
<td><input name="book.book_count" type="text" value="${book.book_count}"></td>
</tr>
<tr>
<td bgcolor="Coral">图书价格:</td>
<td><input name="book.book_price" type="text" value="${book.book_price}"></td>
</tr>
<tr>
<td bgcolor="Coral">图书作者:</td>
<td><input name="book.book_author" type="text" value="${book.book_author}"></td>
</tr>
</table>
<p align="center">
<input type="submit" value="修 改">
<input type="reset" value="重 置">
</p>
</form>
</body>
</html>
点击修改后,“图书库存”改成了1111:
上面就是所有的代码啦!
有什么问题欢迎各位到评论区评论,我必将及时回复!!!