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);
/
* 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";
}
}
运行出了两个错误
Error:(237, 17) java: 找不到符号
符号: 变量 pb
位置: 类 cn.njcit.goods.book.web.servlet.BookServlet
Error:(238, 40) java: 找不到符号
符号: 变量 pb
位置: 类 cn.njcit.goods.book.web.servlet.BookServlet
/*
* 5. 给PageBean设置url,保存PageBean,转发到/jsps/book/list.jsp
*/
pb.setUrl(url);
req.setAttribute("pb", pb);
return "f:/jsps/book/list.jsp";
错误提示已经很明显了,找不到符号 变量pb。 你的第5步中pb变量没有定义
但是我上面的方法也是这么定义的 就没错啊
package cn.njcit.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);
// /
// * 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";
// }
//}
/*
* 4. 使用pc和cid调用service#findByCategory得到PageBean
*/
PageBean pb = bookService.findByBname(bname, pc);
你上面方法都定义了pb变量啊, 你的findByCombination方法里可没有定义啊