最近改写别人的Java Web项目练习,遇到这个问题卡着两三天了。
以下是JSP代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>left</title>
<base target="body"/>
<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">
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="<c:url value='/jquery/jquery-1.5.1.js'/>"></script>
<script type="text/javascript" src="<c:url value='/menu/mymenu.js'/>"></script>
<link rel="stylesheet" href="<c:url value='/menu/mymenu.css'/>" type="text/css" media="all">
<link rel="stylesheet" type="text/css" href="<c:url value='/jsps/css/left.css'/>">
<script language="javascript">
/*
* 1. 对象名必须与第一个参数相同!
2. 第二个参数是显示在菜单上的大标题
*/
var bar = new Q6MenuBar("bar", "仙林大学城网上书城");
$(function() {
bar.colorStyle = 4;//指定配色样式,一共0,1,2,3,4
bar.config.imgDir = "<c:url value='/menu/img/'/>";//小工具所需图片的路径
bar.config.radioButton=true;//是否排斥,多个一级分类是否排斥
/*
1. 程序设计:一级分类名称
2. Java Javascript:二级分类名称
3. /goods/jsps/book/list.jsp:点击二级分类后链接到的URL
4. body:链接的内容在哪个框架页中显示
*/
<c:forEach items="${parents}" var="parent">
<c:forEach items="${parent.children}" var="child">
bar.add("${parent.cname}", "${child.cname}", "/goods/BookServlet?method=findByCategory&cid=${child.cid}", "body");
</c:forEach>
</c:forEach>
$("#menu").html(bar.toString());
});
</script>
</head>
<body>
<div id="menu"></div>
</body>
</html>
以下servlet代码
package cn.itcast.goods.book.web.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.itcast.commons.CommonUtils;
import cn.itcast.goods.book.domain.Book;
import cn.itcast.goods.book.service.BookService;
import cn.itcast.goods.pager.PageBean;
import cn.itcast.servlet.BaseServlet;
public class BookServlet extends BaseServlet {
private BookService bookService = new BookService();
/**
* 获取当前页码
*
* @param req
* @return
*/
private int getPc(HttpServletRequest req) {
int pc = 1;
String param = req.getParameter("pc");
if (param != null && !param.trim().isEmpty()) {
try {
pc = Integer.parseInt(param);
} catch (RuntimeException e) {
}
}
return pc;
}
/**
* 截取url,页面中的分页导航中需要使用它做为超链接的目标!
*
* @param req
* @return
*/
/*
* http://localhost:8080/goods/BookServlet?methed=findByCategory&cid=xxx&pc=3
* /goods/BookServlet + methed=findByCategory&cid=xxx&pc=3
*/
private String getUrl(HttpServletRequest req) {
String url = req.getRequestURI() + "?" + req.getQueryString();
/*
* 如果url中存在pc参数,截取掉,如果不存在那就不用截取。
*/
int index = url.lastIndexOf("&pc=");
if (index != -1) {
url = url.substring(0, index);
}
return url;
}
/**
* 按bid查询
*
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
public String load(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String bid = req.getParameter("bid");//获取链接的参数bid
Book book = bookService.load(bid);//通过bid得到book对象
req.setAttribute("book", book);//保存到req中
return "f:/jsps/book/desc.jsp";//转发到desc.jsp
}
/**
* 按分类查
*
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
public String findByCategory(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
/*
* 1. 得到pc:如果页面传递,使用页面的,如果没传,pc=1
*/
int pc = getPc(req);
/*
* 2. 得到url:...
*/
String url = getUrl(req);
/*
* 3. 获取查询条件,本方法就是cid,即分类的id
*/
String cid = req.getParameter("cid");
/*
* 4. 使用pc和cid调用service#findByCategory得到PageBean
*/
PageBean<Book> pb = bookService.findByCategory(cid, pc);
/*
* 5. 给PageBean设置url,保存PageBean,转发到/jsps/book/list.jsp
*/
pb.setUrl(url);
req.setAttribute("pb", pb);
return "f:/jsps/book/list.jsp";
}
/**
* 按作者查
*
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
public String findByAuthor(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
/*
* 1. 得到pc:如果页面传递,使用页面的,如果没传,pc=1
*/
int pc = getPc(req);
/*
* 2. 得到url:...
*/
String url = getUrl(req);
/*
* 3. 获取查询条件,本方法就是cid,即分类的id
*/
String author = req.getParameter("author");
/*
* 4. 使用pc和cid调用service#findByCategory得到PageBean
*/
PageBean<Book> pb = bookService.findByAuthor(author, pc);
/*
* 5. 给PageBean设置url,保存PageBean,转发到/jsps/book/list.jsp
*/
pb.setUrl(url);
req.setAttribute("pb", pb);
return "f:/jsps/book/list.jsp";
}
/**
* 按出版社查询
*
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
public String findByPress(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
/*
* 1. 得到pc:如果页面传递,使用页面的,如果没传,pc=1
*/
int pc = getPc(req);
/*
* 2. 得到url:...
*/
String url = getUrl(req);
/*
* 3. 获取查询条件,本方法就是cid,即分类的id
*/
String press = req.getParameter("press");
/*
* 4. 使用pc和cid调用service#findByCategory得到PageBean
*/
PageBean<Book> pb = bookService.findByPress(press, pc);
/*
* 5. 给PageBean设置url,保存PageBean,转发到/jsps/book/list.jsp
*/
pb.setUrl(url);
req.setAttribute("pb", pb);
return "f:/jsps/book/list.jsp";
}
/**
* 按图名查
*
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
public String findByBname(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
/*
* 1. 得到pc:如果页面传递,使用页面的,如果没传,pc=1
*/
int pc = getPc(req);
/*
* 2. 得到url:...
*/
String url = getUrl(req);
/*
* 3. 获取查询条件,本方法就是cid,即分类的id
*/
String bname = req.getParameter("bname");
/*
* 4. 使用pc和cid调用service#findByCategory得到PageBean
*/
PageBean<Book> pb = bookService.findByBname(bname, pc);
/*
* 5. 给PageBean设置url,保存PageBean,转发到/jsps/book/list.jsp
*/
pb.setUrl(url);
req.setAttribute("pb", pb);
return "f:/jsps/book/list.jsp";
}
/**
* 多条件组合查询
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
public String findByCombination(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
/*
* 1. 得到pc:如果页面传递,使用页面的,如果没传,pc=1
*/
int pc = getPc(req);
/*
* 2. 得到url:...
*/
String url = getUrl(req);
/*
* 3. 获取查询条件,本方法就是cid,即分类的id
*/
Book criteria = CommonUtils.toBean(req.getParameterMap(), Book.class);
PageBean<Book> pb = bookService.findByCombination(criteria, pc);
/*
* 4. 使用pc和cid调用service#findByCategory得到PageBean
*/
/*
* 5. 给PageBean设置url,保存PageBean,转发到/jsps/book/list.jsp
*/
pb.setUrl(url);
req.setAttribute("pb", pb);
return "f:/jsps/book/list.jsp";
}
}
以下是web.xml相关配置
<servlet>
<servlet-name>BookServlet</servlet-name>
<servlet-class>cn.itcast.goods.book.web.servlet.BookServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BookServlet</servlet-name>
<url-pattern>/BookServlet</url-pattern>
</servlet-mapping>
相关文件位置