学习博客:javaweb图书模块的编写---添加图书功能的实现

这里要注意一个点:
表单重复提交:会发生在我们从servlet到图书列表管理的页面。
当用户提交完请求,浏览器会记录下最后一次请求的全部信息。当用户按下功能键FS,就会发起浏览器记录的最后一次请求。|
所以我们这里需要使用重定向。
BookServlet 的编写:

package cn.zsp.web;

import cn.zsp.pojo.Book;
import cn.zsp.service.impl.BookServiceImpl;
import cn.zsp.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class BookServlet extends  BaseServlet{
        private BookServiceImpl bookService =new BookServiceImpl();
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        1,获取请求的参数==封装成为Book对象
            Book book = WebUtils.copyParamToBean(req.getParameterMap(),
                    new Book());
//        2,调用BookService. addBook()保存图书
            bookService.addBook(book);
//        3,跳到图书列表页面
//            req.getRequestDispatcher("manager/bookServlet?action=list");
//        这里使用转发如果用户在完成页面重复按f5会重复添加所以我们这里使用请求重定向来实现跳转
        resp.sendRedirect(req.getContextPath()+"/manager/bookServlet" +
                "?action=list");
    }
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        通过BookService查询全部图书
//        把全部图书保存到Request域中
//        请求转发到/pages/manager/book_ manager. jsp页面
        List<Book>  books = bookService.queryBooks();
        req.setAttribute("books",books);
        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req,resp);
    }
}

图书添加功能的jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>编辑图书</title>
	<%@include file="/pages/common/head.jsp"%><style type="text/css">
	h1 {
		text-align: center;
		margin-top: 200px;
	}
	
	h1 a {
		color:red;
	}
	
	input {
		text-align: center;
	}
</style>
</head>
<body>
		<div id="header">
			<img class="logo_img" alt="" src="../../static/img/logo.gif" >
			<span class="wel_word">编辑图书</span>
			<%@include file="/pages/common/manager_menu.jsp"%>
		</div>
		
		<div id="main">
			<form action="	manager/bookServlet" method="get">
				<input type="hidden" name="action" value="add">
				<table>
					<tr>
						<td>名称</td>
						<td>价格</td>
						<td>作者</td>
						<td>销量</td>
						<td>库存</td>
						<td colspan="2">操作</td>
					</tr>		
					<tr>
						<td><input name="name" type="text" value="时间简史"/></td>
						<td><input name="price" type="text" value="30.00"/></td>
						<td><input name="author" type="text" value="霍金"/></td>
						<td><input name="sales" type="text" value="200"/></td>
						<td><input name="stock" type="text" value="300"/></td>
						<td><input type="submit" value="提交"/></td>
					</tr>	
				</table>
			</form>
			
	
		</div>
		
		<div id="bottom">
			<%@include file="/pages/common/footer.jsp"%></div>
</body>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值