一、旅游线路的查询
1.分析
2.前台要传递参数rname
3.修改后台代码
修改RouteServlet
@WebServlet("/route/*")
public class RouteServlet extends BaseServlet {
private RouteService routeService = new RouteServiceImpl();
public void pageQuery(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String currentPageStr = request.getParameter("currentPage");
String pageSizeStr = request.getParameter("pageSize");
String cidStr = request.getParameter("cid");
//接受rname 线路名称
String rname = request.getParameter("rname");
rname = new String(rname.getBytes("iso-8859-1"),"utf-8");//给rname编码,要不然后期会乱码
int cid = 0;
if(cidStr != null && cidStr.length() > 0 && !"null".equals(cidStr)){
//cidStr除了不为空、长度不为0以外,现在要cidStr还不能是字符串"null"(如果url是route_list.html?cid=null&rname=xxx)那么此时cidStr就是字符串"null"
cid = Integer.parseInt(cidStr);
}
int currentPage = 0;
if(currentPageStr != null && currentPageStr.length() > 0){
currentPage = Integer.parseInt(currentPageStr);
}else{
currentPage = 1;
}
int pageSize = 0;
if(pageSizeStr != null && pageSizeStr.length() > 0){
pageSize = Integer.parseInt(pageSizeStr);
}else{
pageSize = 5;
}
PageBean<Route> pb = routeService.pageQuery(cid, currentPage, pageSize,rname);
writeValue(pb,response);
}
}
修改RoutesSrvice
public interface RouteService {
//根据类别进行分页查询
public PageBean<Route> pageQuery(int cid,int currentPage,int pageSize,String rname);
}
public class RouteServiceImpl implements RouteService {
private RouteDao routeDao = new RouteDaoImpl();
private RouteImgDao routeImgDao = new RouteImgDaoImpl();
private SellerDao sellerDao = new SellerDaoImpl();
@Override
public PageBean<Route> pageQuery(int cid, int currentPage, int pageSize,String rname ) {
//封装PageBean
PageBean<Route> pb = new PageBean<Route>();
//设置当前页码
pb.setCurrentPage(currentPage);
//设置每页显示条数
pb.setPageSize(pageSize);
//设置总记录数
int totalCount = routeDao.findTotalCount(cid,rname);
pb.setTotalCount(totalCount);
//设置当前页显示的数据集合
int start = (currentPage - 1) * pageSize;//开始的记录数
List<Route> list = routeDao.findByPage(cid,start,pageSize,rname);
pb.setList(list);
//设置总页数 = 总记录数/每页显示条数
int totalPage = totalCount % pageSize == 0 ? totalCount / pageSize :(totalCount / pageSize) + 1 ;
pb.setTotalPage(totalPage);
return pb;
}
}
修改RouteDao
public interface RouteDao {
//根据cid查询总记录数
public int findTotalCount(int cid,String rname);
//根据cid,start,pageSize查询当前页的数据集合
public List<Route> findByPage(int cid , int start , int pageSize,String rname);
}
public class RouteDaoImpl implements RouteDao {
private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
@Override
public int findTotalCount(int cid,String rname) {
//select count(*) from tab_route where 1=1 and cid = ? and rname like ?
//定义sql模板
String sql = "select count(*) from tab_route where 1=1 ";
StringBuilder sb = new StringBuilder(sql);
List params = new ArrayList();//条件们
//判断参数是否有值
if(cid != 0){
sb.append( " and cid = ? ");
params.add(cid);//添加?对应的值
}
if(rname != null && rname.length() > 0){
sb.append(" and rname like ? ");
params.add("%"+rname+"%");
}
sql = sb.toString();
return template.queryForObject(sql,Integer.class,params.toArray());
}
@Override
public List<Route> findByPage(int cid, int start, int pageSize,String rname) {
//String sql = "select * from tab_route where cid = ? and rname like ? limit ? , ?";
//定义sql模板
String sql = " select * from tab_route where 1 = 1 ";
StringBuilder sb = new StringBuilder(sql);
List params = new ArrayList();//条件们
//判断参数是否有值
if(cid != 0){
sb.append( " and cid = ? ");
params.add(cid);//添加?对应的值
}
if(rname != null && rname.length() > 0){
sb.append(" and rname like ? ");
params.add("%"+rname+"%");
}
sb.append(" limit ? , ? ");//分页条件
sql = sb.toString();
params.add(start);
params.add(pageSize);
return template.query(sql,new BeanPropertyRowMapper<Route>(Route.class),params.toArray());
}
}
4.前台代码的实现
修改route_list.html
<script>
$(function () {
//跳转过来的路径http://localhost/travel/route_list.html?cid=5&rname=xxx
var cid = getParameter("cid");//获取cid的参数值
var rname = getParameter("rname");//获取rname的参数值
//判断rname如果不为null或者""
if(rname){
rname = window.decodeURIComponent(rname);//进行url解码(要不然看到的汉字就是乱码)
}
//当页码加载完成后,调用load方法,发送ajax请求加载数据
load(cid,null,rname); //load()里面要传递三个参数
});
//load()里面要传递三个参数
function load(cid ,currentPage,rname){
$.get("route/pageQuery",{cid:cid,currentPage:currentPage,rname:rname},function (pb) {
$("#totalPage").html(pb.totalPage);
$("#totalCount").html(pb.totalCount);
var lis = "";
var fristPage = '<li οnclick="javascipt:load('+cid+',1,\''+rname+'\')"><a href="javascript:void(0)">首页</a></li>';
//传递load(cid,1,西安)
var beforeNum = pb.currentPage - 1;
if(beforeNum <= 0){
beforeNum = 1;
}
var beforePage = '<li οnclick="javascipt:load('+cid+' , '+beforeNum+' , \''+rname+'\')" class="threeword"><a href="javascript:void(0)">上一页</a></li>';
//load()里面要传递三个参数
lis += fristPage;
lis += beforePage;
var begin;
var end ;
if(pb.totalPage < 10){
begin = 1;
end = pb.totalPage;
}else{
begin = pb.currentPage - 5 ;
end = pb.currentPage + 4 ;
if(begin < 1){
begin = 1;
end = begin + 9;
}
if(end > pb.totalPage){
end = pb.totalPage;
begin = end - 9 ;
}
}
for (var i = begin; i <= end ; i++) {
var li;
if(pb.currentPage == i){
li = '<li class="curPage" οnclick="javascipt:load('+cid+','+i+',\''+rname+'\')"><a href="javascript:void(0)">'+i+'</a></li>';
}else{
li = '<li οnclick="javascipt:load('+cid+','+i+',\''+rname+'\')"><a href="javascript:void(0)">'+i+'</a></li>';
}
lis += li;
}
var lastPage = '<li class="threeword"><a href="javascript:;">末页</a></li>';
var nextPage = '<li class="threeword"><a href="javascript:;">下一页</a></li>';
lis += nextPage;
lis += lastPage;
$("#pageNum").html(lis);
var route_lis = "";
for (var i = 0; i < pb.list.length; i++) {
//获取{rid:1,rname:"xxx"}
var route = pb.list[i];
var li = '<li>\n' +
' <div class="img"><img src="'+route.rimage+'" style="width: 299px;"></div>\n' +
' <div class="text1">\n' +
' <p>'+route.rname+'</p>\n' +
' <br/>\n' +
' <p>'+route.routeIntroduce+'</p>\n' +
' </div>\n' +
' <div class="price">\n' +
' <p class="price_num">\n' +
' <span>¥</span>\n' +
' <span>'+route.price+'</span>\n' +
' <span>起</span>\n' +
' </p>\n' +
' <p><a href="route_detail.html?rid='+route.rid+'">查看详情</a></p>\n' +
' </div>\n' +
' </li>';
route_lis += li;
}
$("#route").html(route_lis);
});
}
</script>
5.运行结果
但是如果你要是从首页访问时没有点击“国内旅游”而是直接搜索“西安”,那么cid此时就没有传参
二、旅游线路详情查询
1.分析
2.后台代码
route对象
public class Route implements Serializable {
private int rid;//线路id,必输
private String rname;//线路名称,必输
private double price;//价格,必输
private String routeIntroduce;//线路介绍
private String rflag; //是否上架,必输,0代表没有上架,1代表是上架
private String rdate; //上架时间
private String isThemeTour;//是否主题旅游,必输,0代表不是,1代表是
private int count;//收藏数量
private int cid;//所属分类,必输
private String rimage;//缩略图
private int sid;//所属商家
private String sourceId;//抓取数据的来源id
private Category category;//所属分类
private Seller seller;//所属商家
private List<RouteImg> routeImgList;//商品详情图片列表
// 无参构造方法
public Route(){}
//有参构造方法
public Route(int rid, String rname, double price, String routeIntroduce, String rflag, String rdate, String isThemeTour, int count, int cid, String rimage, int sid, String sourceId) {
this.rid = rid;
this.rname = rname;
this.price = price;
this.routeIntroduce = routeIntroduce;
this.rflag = rflag;
this.rdate = rdate;
this.isThemeTour = isThemeTour;
this.count = count;
this.cid = cid;
this.rimage = rimage;
this.sid = sid;
this.sourceId = sourceId;
}
public List<RouteImg> getRouteImgList() {
return routeImgList;
}
public void setRouteImgList(List<RouteImg> routeImgList) {
this.routeImgList = routeImgList;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
public Seller getSeller() {
return seller;
}
public void setSeller(Seller seller) {
this.seller = seller;
}
public String getSourceId() {
return sourceId;
}
public void setSourceId(String sourceId) {
this.sourceId = sourceId;
}
public int getRid() {
return rid;
}
public void setRid(int rid) {
this.rid = rid;
}
public String getRname() {
return rname;
}
public void setRname(String rname) {
this.rname = rname;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getRouteIntroduce() {
return routeIntroduce;
}
public void setRouteIntroduce(String routeIntroduce) {
this.routeIntroduce = routeIntroduce;
}
public String getRflag() {
return rflag;
}
public void setRflag(String rflag) {
this.rflag = rflag;
}
public String getRdate() {
return rdate;
}
public void setRdate(String rdate) {
this.rdate = rdate;
}
public String getIsThemeTour() {
return isThemeTour;
}
public void setIsThemeTour(String isThemeTour) {
this.isThemeTour = isThemeTour;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public String getRimage() {
return rimage;
}
public void setRimage(String rimage) {
this.rimage = rimage;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
}
修改routeServlet
@WebServlet("/route/*")
public class RouteServlet extends BaseServlet {
private RouteService routeService = new RouteServiceImpl();
// 根据id查询一个旅游线路的详细信息
public void findOne(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.接收id
String rid = request.getParameter("rid");
//2.调用service查询route对象
Route route = routeService.findOne(rid);
//3.转为json写回客户端
writeValue(route,response);
}
}
修改routeService层
public interface RouteService {
//根据id查询
public Route findOne(String rid);
}
public class RouteServiceImpl implements RouteService {
private RouteDao routeDao = new RouteDaoImpl();
private RouteImgDao routeImgDao = new RouteImgDaoImpl();
private SellerDao sellerDao = new SellerDaoImpl();
@Override
public Route findOne(String rid) {
//1.根据id去route表中查询route对象
Route route = routeDao.findOne(Integer.parseInt(rid));
//2.根据route的id 查询图片集合信息
List<RouteImg> routeImgList = routeImgDao.findByRid(route.getRid());
//2.2将集合设置到route对象
route.setRouteImgList(routeImgList);
//3.根据route的sid(商家id)查询商家对象
Seller seller = sellerDao.findById(route.getSid());
route.setSeller(seller);
return route;
}
}
修改routeDao层
public interface RouteDao {
//根据id查询
public Route findOne(int rid);
}
public class RouteDaoImpl implements RouteDao {
private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
@Override
public Route findOne(int rid) {
String sql = "select * from tab_route where rid = ?";
return template.queryForObject(sql,new BeanPropertyRowMapper<Route>(Route.class),rid);
}
}
新加RouteImgDao
public interface RouteImgDao {
//根据route的id查询图片
public List<RouteImg> findByRid(int rid);
}
public class RouteImgDaoImpl implements RouteImgDao {
private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
@Override
public List<RouteImg> findByRid(int rid) {
String sql = "select * from tab_route_img where rid = ? ";
return template.query(sql,new BeanPropertyRowMapper<RouteImg>(RouteImg.class),rid);
}
}
新加SellerDao
public interface SellerDao {
//根据id查询
public Seller findById(int id);
}
public class SellerDaoImpl implements SellerDao {
private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
@Override
public Seller findById(int id) {
String sql = "select * from tab_seller where sid = ? ";
return template.queryForObject(sql,new BeanPropertyRowMapper<Seller>(Seller.class),id);
}
}
3.前台代码的编写
route_detail.html
$(function () {
//1.获取rid
var rid = getParameter("rid");
//2.发送请求请求 route/findOne
$.get("route/findOne",{rid:rid},function (route) {
//3.解析数据填充html
$("#rname").html(route.rname);
$("#routeIntroduce").html(route.routeIntroduce);
$("#price").html("¥"+route.price);
$("#sname").html(route.seller.sname);
$("#consphone").html(route.seller.consphone);
$("#address").html(route.seller.address);
/* 图片展示 */
var ddstr = '<a class="up_img up_img_disable"></a>';
//遍历routeImgList
for (var i = 0; i < route.routeImgList.length; i++) {
var astr ;
if(i >= 4){ //只显示前四个
astr = '<a title="" class="little_img" data-bigpic="'+route.routeImgList[i].bigPic+'" style="display:none;">\n' +
' <img src="'+route.routeImgList[i].smallPic+'">\n' +
' </a>';
}else{ //大于4的要隐藏
astr = '<a title="" class="little_img" data-bigpic="'+route.routeImgList[i].bigPic+'">\n' +
' <img src="'+route.routeImgList[i].smallPic+'">\n' +
' </a>';
}
ddstr += astr;
}
ddstr+='<a class="down_img down_img_disable" style="margin-bottom: 0;"></a>';
$("#dd").html(ddstr);
//图片展示和切换代码调用
goImg();
});
});
感谢浏览和收藏