综合案例-黑马旅游网_分类数据展示功能
分析
效果
后端代码实现
CategoryDao接口
package com.bai.dao; import com.bai.domain.Category; import java.util.Calendar; import java.util.List; public interface CategoryDao { /** * 查询所有 * @return */ public List<Category> findAll(); }
CategoryDaoImpl实现类
package com.bai.dao.impl; import com.bai.dao.CategoryDao; import com.bai.domain.Category; import com.bai.util.JDBCUtils; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import java.util.List; public class CategoryDaoImpl implements CategoryDao { private JdbcTemplate template=new JdbcTemplate(JDBCUtils.getDataSource()); @Override public List<Category> findAll() { String sql="select * from tab_category"; return template.query(sql,new BeanPropertyRowMapper<Category>(Category.class)); } }
categoryService接口
package com.bai.service; import com.bai.domain.Category; import java.util.List; public interface categoryService { public List<Category> findAll(); }
categoryServiceImpl实现类
package com.bai.service.impl; import com.bai.dao.CategoryDao; import com.bai.dao.impl.CategoryDaoImpl; import com.bai.domain.Category; import com.bai.service.categoryService; import java.util.List; public class categoryServiceImpl implements categoryService { private CategoryDao categoryDao = new CategoryDaoImpl(); @Override public List<Category> findAll() { return categoryDao.findAll(); } }
BaseServlet类 此类添加了两个方法
/** * 直接将传入的对象序列化为json 并且写回客户端 * @param obj * @param response * @throws IOException */ public void writeValue(Object obj,HttpServletResponse response) throws IOException { ObjectMapper mapper = new ObjectMapper(); response.setContentType("application/json;charset=utf-8"); mapper.writeValue(response.getOutputStream(),obj); } /** * 将传入等待对象序列化为json 返回 * @param obj * @return */ public String writeValueAsString(Object obj) throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); return mapper.writeValueAsString(obj); }
CategoryServlet类
package com.bai.web.servlet; import com.bai.domain.Category; import com.bai.service.categoryService; import com.bai.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; import java.util.List; @WebServlet("/category/*") public class CategoryServlet extends BaseServlet { private final categoryService service = new categoryServiceImpl(); /** * 查询所有 * * @param req * @param resp * @throws ServletException * @throws IOException */ public void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { List<Category> sc = service.findAll(); writeValue(sc,resp); } }
前端代码实现
hader.html加载后 发送ajax请求 请求category/findAll
<!-- 头部 start --> <script> $(function () { //查询用户的数据 $.get("findUserServlet",{},function (data) { var msg = "欢迎回来,"+data.name; $("#span_username").html(msg); }); //查询分类信息 $.get("findAllServlet",{},function (data) { var lis='<li class="nav-active"><a href="index.html">首页</a></li>'; //变量数组 拼接字符串 for (var i = 0;i< data.length;i++){ var li='<li><a href="route_list.html">'+data[i].cname+'</a></li>'; lis+=li; } lis+='<li><a href="favoriterank.html">收藏排行榜</a></li>'; //将list字符串 设置到ul的html内容中 $("#category").html(lis); }) }); </script> <header id="header"> <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="span_username"></span> <a href="myfavorite.html" class="collection">我的收藏</a> <a href="javascript:location.href='/exitServlet';">退出</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">搜索</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 id="category" class="nav"> <!--<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>标签:findAll,List,public,案例,旅游网,import,com,黑马,bai From: https://www.cnblogs.com/aimz01/p/16624967.html