设为首页 收藏本站
查看: 1091|回复: 0

[经验分享] Struts2+spring+MyBatis增删改查操作(1)

[复制链接]

尚未签到

发表于 2016-11-28 09:00:50 | 显示全部楼层 |阅读模式
主页面

http://localhost:8080/book/index.jsp



index.jsp页面代码
<%@ page language="java" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>图书管理页面</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<a href="addBook.jsp">添加图书</a>
<a href="book/viewSBook.action">浏览图书</a>
</body>
</html>
浏览图书操作
http://localhost:8080/book/book/viewSBook.action

viewBook.jsp页面代码
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>浏览图书</title>
</head>
<body>
<table align="center" border="1" style="width:80%;">
<tr>
<th colspan="7" align="center">库存图书</th>
</tr>
<tr>
<td align="left" colspan="7"><a href="<%=request.getContextPath()%>/addBook.jsp">添加新书</a></td>
</tr>
<tr>
<td>书名</td>
<td>作者</td>
<td>价格</td>
<td>库存量</td>
<td>ISBN号</td>
<td>出版社</td>
<td>操作</td>
</tr>
<s:iterator value="bookList">
<tr>
<td>
<s:property value="title"/>
</td>
<td>
<s:property value="author"/>
</td>
<td>
<s:property value="price"/>
</td>
<td>
<s:property value="total"/>
</td>
<td>
<s:property value="isbn"/>
</td>
<td>
<s:property value="publisher"/>
</td>
<td>
<a href="<%=request.getContextPath()%>/sbook/modifySBook.action?bookId=${id}">修改信息</a>
<a href="<%=request.getContextPath()%>/sbook/removeSBook.action?bookId=${id}">删除</a>
</td>
</tr>
</s:iterator>
<s:property value="tips"/>
</table>
</body>
</html>
点击修改按钮则进入修改页面

bookMsg.jsp页面代码
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>修改图书信息</title>
</head>
<body>
<s:form action="/sbook/updateSBook.action">
<s:hidden name="sbook.id">${id}</s:hidden>
<s:textfield name="sbook.title" label="书名" readonly="true">${title}</s:textfield>
<s:textfield name="sbook.author" label="作者">${author}</s:textfield>
<s:textfield name="sbook.price" label="价格">${price}</s:textfield>
<s:textfield name="sbook.total" label="总量">${total}</s:textfield>
<s:textfield name="sbook.isbn" label="ISBN">${isbn}</s:textfield>
<s:textfield name="sbook.publisher" label="出版社">${publisher}</s:textfield>
<s:submit/>
</s:form>
<s:property value="tips" />
</body>
</html>
点击删除操作,直接将对应记录删除

增加操作
http://localhost:8080/book/addBook.jsp

addBook.jsp页面代码
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>添加图书</title>
</head>
<body>
<s:property value="tips"/>
<s:form action="addSBook" >
<s:textfield name="sbook.title" label="书名"/>
<s:textfield name="sbook.author" label="作者"/>
<s:textfield name="sbook.price" label="价格"/>
<s:textfield name="sbook.total" label="数量"/>
<s:textfield name="sbook.isbn" label="ISBN号"/>
<s:textfield name="sbook.publisher" label="出版社"/>
<s:submit value="添加"/>
</s:form>
<a href="<%=request.getContextPath() %>/viewSBook.action">查看现有图书</a>
</body>
</html>
配置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>
<include file="struts-default.xml" />
<package name="book" extends="struts-default" >
<!-- 添加图书信息 -->
<action name="addSBook" class="com.dou.book.action.SBookAction" method="addSBook">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
<!-- 查看全部图书信息 -->
<action name="viewSBook" class="com.dou.book.action.SBookAction" method="viewSBook">
<result name="success">/viewBook.jsp</result>
<result name="error">/error.jsp</result>
</action>
<!-- 修改指定图书信息 -->
<action name="modifySBook" class="com.dou.book.action.SBookAction" method="modifySBook">
<result name="success">/bookMsg.jsp</result>
<result name="error">/error.jsp</result>
</action>
<!-- 修改指定图书信息 -->
<action name="updateSBook" class="com.dou.book.action.SBookAction" method="updateSBook">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
<!-- 删除指定图书信息 -->
<action name="removeSBook" class="com.dou.book.action.SBookAction" method="removeSBook">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
实现com.dou.book.action.SBookAction类中的addSBook方法
package com.dou.book.action;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.dou.book.data.pojo.SBook;
import com.dou.book.data.services.ISBookServices;
@SuppressWarnings("unchecked")
public class SBookAction {
@Autowired
ISBookServices bookServices;
private SBook sbook;
private String tips;
private String bookId;
private List bookList;
/**
* 添加图书信息
*
* @return 返回添加是否成功
*/
public String addSBook() {
String result = "error";
try {
bookServices.saveBook(sbook);
this.setTips("添加成功");
result = "success";
} catch (Exception e) {
e.printStackTrace();
this.setTips("系统出现问题");
}
return result;
}
/**
* 查看所有图书
*
* @return
*/
public String viewSBook() {
String result = "error";
try {
bookList = bookServices.findAllBook();
result = "success";
} catch (Exception e) {
e.printStackTrace();
this.setTips("系统出现问题,请稍后访问");
}
return result;
}
/**
* 修改图书信息
*
* @return
*/
public String modifySBook() {
String result = "error";
try {
sbook = bookServices.getBookById(Integer
.parseInt(this.getBookId()));
result = "success";
} catch (Exception e) {
e.printStackTrace();
this.setTips("系统出现问题");
}
return result;
}
public String updateSBook() {
String result = "error";
try {
bookServices.updateBook(sbook);
result = "success";
} catch (Exception e) {
e.printStackTrace();
this.setTips("更新操作失败");
}
return result;
}
/**
* 删除图书
*
* @return
*/
public String removeSBook() {
String result = "error";
try {
bookServices.removeBook(Integer.parseInt(this.getBookId()));
result = "success";
} catch (Exception e) {
e.printStackTrace();
this.setTips("删除操作失败");
}
return result;
}
public SBook getSbook() {
return sbook;
}
public void setSbook(SBook sbook) {
this.sbook = sbook;
}
public List getBookList() {
return bookList;
}
public void setBookList(List bookList) {
this.bookList = bookList;
}
public String getTips() {
return tips;
}
public void setTips(String tips) {
this.tips = tips;
}
public String getBookId() {
return bookId;
}
public void setBookId(String bookId) {
this.bookId = bookId;
}
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-306418-1-1.html 上篇帖子: spring多数据源的处理 mybatis实现跨库查询 下篇帖子: Maven + SpringMVC + Mybatis【绝非原创,单纯整理】【一】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表