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

   主页面 

  

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

 

 

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值