zoukankan      html  css  js  c++  java
  • (三)查看图书详细信息

    需要注意的三点:

    1. 查看图书的详细信息:

    1). 在超链接后附带 id.

    <a href="bookServlet?method=getBook&pageNo=${bookpage.pageNo }&id=${book.id}">${book.title }</a>

    2). 对从页面传入的请求参数, 若要求改该请求参数为整型的字符串, 类似于 "1". 在 Controller 中的建议处理方案:

    int id = -1;
    try {
    id = Integer.parseInt(idStr);
    } catch (NumberFormatException e) {}
    
    if(id > 0)
    book = bookService.getBook(id);

    3). 查询条件的隐藏域和 JS 代码的可重用性解决方案: 把相同的代码放到一个 JSP 中, 各个页面包含该 JSP 即可:

    <%@ include file="/commons/queryCondition.jsp" %>

    2.具体实现代码

    BookServlet.java

    package com.aff.bookstore.servlet;
    
    import java.io.IOException;
    import java.lang.reflect.Method;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import com.aff.bookstore.domain.Book;
    import com.aff.bookstore.service.BookService;
    import com.aff.bookstore.web.CriteriaBook;
    import com.aff.bookstore.web.Page;
    
    @WebServlet("/bookServlet")
    public class BookServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            doPost(request, response);
        }
    
        private BookService bookService = new BookService();
    
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            String methodName = request.getParameter("method");
            try {
                Method method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class,
                        HttpServletResponse.class);
                method.setAccessible(true);
                method.invoke(this, request, response);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        protected void getBook(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            String idStr = request.getParameter("id");
            int id = -1;
            Book book = null;
    
            try {
                id = Integer.parseInt(idStr);
            } catch (NumberFormatException e) {}
    
            if (id>0) {
                book = bookService.getBook(id);
                if (book == null) {
                    response.sendRedirect(request.getContextPath() + "/errror-1.jsp");
                    return;
                }
            }
            request.setAttribute("book", book);
            request.getRequestDispatcher("/WEB-INF/pages/book.jsp").forward(request, response);
        }
    
        protected void getBooks(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            String pageNoStr = request.getParameter("pageNo");
            String minPriceStr = request.getParameter("minPrice");
            String maxPriceStr = request.getParameter("maxPrice");
    
            int pageNo = 1;
            int minPrice = 0;
            int maxPrice = Integer.MAX_VALUE;
            try {
                pageNo = Integer.parseInt(pageNoStr);
            } catch (Exception e) {
            }
            try {
                minPrice = Integer.parseInt(minPriceStr);
            } catch (Exception e) {
            }
            try {
                maxPrice = Integer.parseInt(maxPriceStr);
            } catch (Exception e) {
            }
            CriteriaBook criteriaBook = new CriteriaBook(minPrice, maxPrice, pageNo);
            Page<Book> page = bookService.getPage(criteriaBook);
    
            request.setAttribute("bookpage", page);
            request.getRequestDispatcher("/WEB-INF/pages/books.jsp").forward(request, response);
        }
    }

    book.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>Insert title here</title>
    <script type="text/javascript" src="script/jquery-1.12.3.js"></script>
    
    <!-- 重用之前写的js和参数的隐藏域 -->
    <%@include file="/commons/queryCondition.jsp" %>
    </head>
    <body>
    
        <center>
         
            <br> <br>
            Title: ${book.title}
            <br> <br>
            Author: ${book.author}
            <br> <br>
            Price: ${book.price}
            <br> <br>
            PublishingDate: ${book.publishingDate}
            <br> <br>
            Remark: ${book.remark}
            <br> <br>
            <a href="bookServlet?method=getBooks&pageNo=${param.pageNo}"> 继续购物</a>
               
        </center>
    </body>
    </html>

    queryCondition.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
        <script type="text/javascript">
             $(function(){
                 $("a").click(function(){
                     var serializeVal = $(":hidden").serialize();
                     var href = this.href+"&"+serializeVal;
                     window.location.href = href;
                     return false; 
                 });
            });  
    </script>
        
        <input type="hidden" name="minPrice"  value="${param.minPrice}"/>
        <input type="hidden" name="maxPrice"  value="${param.maxPrice}"/>
        

    实现效果如下

    All that work will definitely pay off
  • 相关阅读:
    论架构在嵌软设计中的重要性
    妙用typeof关键字
    说说动态内存分配
    3个实用shell脚本,建议收藏!
    GNU C语法扩展(7)
    Ubunt_配置_nfs(文件挂载)
    Ubunt_配置_samba(文件共享)
    Ubunt_配置_tftp(文件传输)
    Ubunt_配置_net
    驱动_Platform平台总线
  • 原文地址:https://www.cnblogs.com/afangfang/p/12907999.html
Copyright © 2011-2022 走看看