Java45旅游案例:数据回显,注销/退出,首页类别显示,精选
Posted 码农编程录
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java45旅游案例:数据回显,注销/退出,首页类别显示,精选相关的知识,希望对你有一定的参考价值。
文章目录
1.登陆数据回显
如上前端写在header.html中。
1.1 前端
//header.html
<!-- 头部 start -->
<header id="header2">
<div class="top_banner">
<img src="images/top_banner.jpg" alt="">
</div>
<div class="shortcut">
<!-- 未登录状态 -->
<div class="login_out">
<a href="login.html">登录</a>
<a href="register.html">注册</a>
</div>
<!-- 登录状态 -->
<div class="login">
<span id="name">欢迎回来,admin</span>
<a href="myfavorite.html" class="collection">我的收藏</a>
<a href="javascript:;" onclick="method02()">注销</a>
</div>
</div>
<div class="header_wrap">
<div class="topbar">
<div class="logo">
<a href="/"><img src="images/logo.jpg" alt=""></a>
</div>
<div class="search">
<input name="" type="text" placeholder="请输入路线名称" class="search_input" autocomplete="off">
<a href="javascript:;" class="search-button" id="search">搜索</a>
</div>
<div class="hottel">
<div class="hot_pic">
<img src="images/hot_tel.jpg" alt="">
</div>
<div class="hot_tel">
<p class="hot_time">客服热线(9:00-6:00)</p>
<p class="hot_num">400-618-9090</p>
</div>
</div>
</div>
</div>
</header>
<!-- 头部 end -->
<!-- 首页导航 -->
<div class="navitem">
<ul class="nav">
<!--TODO : 类别-->
<!-- <li class="nav-active"><a href="index.html">首页</a></li>
<li><a href="route_list.html">门票</a></li>
<li><a href="route_list.html">酒店</a></li>
<li><a href="route_list.html">香港车票</a></li>
<li><a href="route_list.html">出境游</a></li>
<li><a href="route_list.html">国内游</a></li>
<li><a href="route_list.html">港澳游</a></li>
<li><a href="route_list.html">抱团定制</a></li>
<li><a href="route_list.html">全球自由行</a></li>
<li><a href="favoriterank.html">收藏排行榜</a></li>-->
</ul>
</div>
<!--111111111111111111111111111111111111111111111111111111111111111111111111111111111111-->
<script>
$(function () {
$.get("/UserServlet?method=isLogin","",function (result) {
// debugger;
if(result.flag){
$(".login_out").hide() //相应模块隐藏掉,其他模块正常显示
$("#name").html("欢迎回来" + result.username)
} else {
$(".login").hide()
}
},"json")
})
</script>
<script>
function method02() {
$.get("/UserServlet?method=logout","",function (result) {
if(result.flag){
location.href = "/login.html"
}
},"json")
}
</script>
<script>
$(function () {
$.get("/CategoryServlet?method=queryAllCategory","",function (result) {
//TODO: 响应回来的数据填充到页面
// console.log(result)
/*
* 字符串拼接 : 阅读性很差 (现在ES5)
* ES6 : EcmaScrpit:Settings - 搜索language - JavaScript - EcmaScrpit6
* */
/* var content = "<li class=\\"nav-active\\"><a href=\\"index.html\\">首页</a></li>"
$(result).each(function (index,element) {
content += "<li><a href=\\"route_list.html\\">"+element.cname+"</a></li>"
})
content += "<li><a href=\\"favoriterank.html\\">收藏排行榜</a></li>"*/
//如下es6语法:双引号不需要转移,拼接用${}替换"+ +"
var content = `<li class="nav-active"><a href="index.html">首页</a></li>`
$(result).each(function (index,element) {
content += `<li><a href="/route_list.html?cid=${element.cid}">${element.cname}</a></li>`
})
content += `<li><a href="favoriterank.html">收藏排行榜</a></li>`
$(".nav").html(content)
},"json")
})
</script>
<script src="js/getParameter.js"></script>
<script>
//TODO:
$("#search").click(function () {
//搜索按钮被点击的时候,获取输入框中的关键字
var keyword = $(".search_input").val()
var cid = getParameter("cid");
if(cid != null && cid != "null"){
location.href = `/route_list.html?cid=${cid}&keyword=${keyword}`
}else{
location.href = "/route_list.html?keyword=" + keyword
}
})
</script>
2.登陆案例_注销/退出
用户注册:将用户信息保存在session里。页面数据回显:从服务器里session获取数据判断。
3.首页类别显示
如下首页和收藏排行榜都是固定的,中间和tab_category表字段一一对应,代码重构后只剩BaseServlet和UserServlet,但类别显示业务不和UserServlet用同一张表,所以不能写在UserServlet里。
3.1 web
package com.heima.travel.web;
import com.heima.travel.bean.Category;
import com.heima.travel.service.CategoryService;
import com.heima.travel.service.impl.CategoryServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(urlPatterns = "/CategoryServlet")
public class CategoryServlet extends BaseServlet {
// 查询所有的商品类别
protected void queryAllCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
CategoryService service = new CategoryServiceImpl();
String json = service.findAll();
response.getWriter().print(json);
}
}
3.2 service
package com.heima.travel.service;
public interface CategoryService {
String findAll(); //查询所有的类别
}
package com.heima.travel.service.impl;
import com.alibaba.fastjson.JSON;
import com.heima.travel.bean.Category;
import com.heima.travel.dao.CategoryDao;
import com.heima.travel.dao.impl.CategoryDaoImpl;
import com.heima.travel.service.CategoryService;
import com.heima.travel.utils.JedisUtils;
import redis.clients.jedis.Jedis;
import java.util.List;
public class CategoryServiceImpl implements CategoryService {
public static final String CATEGORY_LIST_CACHE = "category_list_cache";
@Override
public String findAll() {
//先从redis中取() //redis服务端需要手动开, mysql服务端自动开
Jedis jedis = JedisUtils.getJedis();
String json = jedis.get(CATEGORY_LIST_CACHE);
if(json == null){
CategoryDao dao = new CategoryDaoImpl(); //再从mysql数据库中取
List<Category> list = dao.findAll();
json = JSON.toJSONString(list);
jedis.set(CATEGORY_LIST_CACHE,json); //记得往redis中存一份
System.out.println("从mysql中");
}else{
System.out.println("从缓存中");
}
jedis.close(); //千万要还jedis连接
return json;
}
}
3.3 dao
package com.heima.travel.dao;
import com.heima.travel.bean.Category;
import java.util.List;
public interface CategoryDao {
List<Category> findAll();
}
package com.heima.travel.dao.impl;
import com.heima.travel.bean.Category;
import com.heima.travel.dao.CategoryDao;
import com.heima.travel.utils.C3p0Utils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
public class CategoryDaoImpl implements CategoryDao {
@Override
public List<Category> findAll() {
String sql = "select * from tab_category";
JdbcTemplate template = C3p0Utils.getTemplate();
List<Category> list = template.query(sql, new BeanPropertyRowMapper<>(Category.class));
return list;
}
}
4.精选
tab_category表是一方,tab_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;//商品详情图片列表
4.1 web
package com.heima.travel.web;
import com.heima.travel.service.RouteService;
import com.heima.travel.service.impl.RouteServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(urlPatterns = "/RouteServlet")
public class RouteServlet extends BaseServlet{
RouteService service = new RouteServiceImpl();
//精选线路查询
protected void routeSelection(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String json = service.findRouteSelection();
response.getWriter().print(json);
}
//TODO: 旅游线路列表分页查询
protected void routePageQuerySession实现用户登录案例