分类数据展示功能_实现_后台代码、分类数据展示功能_实现_前台代码
分类数据展示功能_实现_后台代码
CategoryServlet
@WebServlet("/categoryServlet")
public class CategoryServlet extends BaseServlet {
/**
*查询所以的方法
*/
private CategoryService service = new CategoryServiceImpl();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 调用service查询所以
List<Category> finAll = service.finAll();
// 序列化json返回
// ObjectMapper mapper = new ObjectMapper();
// resp.setContentType("application/json;charset=utf-8");
// mapper.writeValue(resp.getOutputStream(),finAll);
writerValue(finAll,resp);
}
}
CategoryService
public interface CategoryService {
public List<Category> finAll();
}
public class CategoryServiceImpl implements CategoryService {
private CategoryDao categoryDao = new CategoryDaoImpl();
@Override
public List<Category> finAll() {
return categoryDao.finAll();
}
}
CategoryDao
public interface CategoryDao {
public List<Category> finAll();
}
public class CategoryDaoImpl implements CategoryDao {
private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
@Override
public List<Category> finAll() {
String sql="select * from tab_category";
return template.query(sql,new BeanPropertyRowMapper<Category>(Category.class));
}
}
分类数据展示功能_实现_前台代码
/**
* 查询分类数据
*/
$.get("category/finAll",{},function(data){
// 拼接搜藏榜的var msg = '<li class="nav-active"><a href="index.html">首页</a></li>-->';
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;
}
// 拼接收藏排行榜的li,<li><a href="favoriterank.html">收藏排行榜</a></li>
lis+='<li><a href="favoriterank.html">收藏排行榜</a></li>';
// 将lis字符串,设置到ul的html内容中
$("#category").html(lis);
})
})