Struts2+spring+MyBatis增删改查操作(1)

来源:互联网 发布:pc街霸5取得数据失败 编辑:程序博客网 时间:2024/04/28 02:06

   主页面 

  

     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;
    }
}

 

 

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 发现老公在卧室安了摄像头怎么办 憋的尿又没有厕所的时候怎么办 一岁宝宝拉屎总是拉出血怎么办 孩子鼻窦炎鼻子堵有白色鼻涕怎么办 家里阳台上老是有蝙蝠趴着怎么办 空调太冷在空调房里该怎么办 分手了怎么办不要挽回要重新吸引 过敏体质没打疫苗的孩子入学怎么办 遇到不认识的小姐姐问我问题怎么办 小孩孑脖子洛忱了痛怎么办 挤奶把乳腺挤肿了不出奶怎么办 遇到好兄弟在背后捅刀子怎么办 面对出轨还不想离婚的老公怎么办 法院执行局把案划错了不承认怎么办 手机nfc功能被手机壳挡住怎么办 飞信短信登录验证码达到上限怎么办 手机自带短信软件没了怎么办 老婆出轨孩子又3个不知道怎么办 骑电动车摔跤小脚趾疼有点肿怎么办 第一次太疼了有心理阴影了怎么办 天梭机械表调了时间忽然慢怎么办 支付宝信用住到酒店没房间怎么办 拳头打在硬上骨头肿了怎么办 领导决策出现了失误你该怎么办 我的直销团队走到瓶颈了怎么办 孕妇8个月便秘大便带血怎么办 爱他美金装冲泡有泡沫怎么办 做雅思听力时理解不了原文怎么办 英国读语言班不想读研了怎么办 英国读研本科学位不够分怎么办 英国华威大学读研没分到宿舍怎么办 英国留学申请大学成绩没通过怎么办 很想上专升本但是专科挂科怎么办 英国学校申请的gpa太低了怎么办 博士面试通过后导师不签名怎么办 资助出国留学协议书写错了怎么办 人生最低谷运气最差的时候怎么办 孩子出国留学一年后想回国怎么办 领导给我娃的钱我怎么办 在西浦大三成绩不好影响申研怎么办 日本国想在中国开分公司要怎么办