一、门户书籍分类列表
1、CategoryDao
package com.xieying.dao;
import java.sql.SQLException;
import java.util.List;
import com.xieying.entity.Category;
import com.xieying.util.BaseDao;
import com.xieying.util.PageBean;
public class CategoryDao extends BaseDao<Category>{
public List<Category> list(Category category,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
String sql ="select * from t_easyui_category";
return super.executeQuery(sql, Category.class, pageBean);
}
}
2、CategoryAction
package com.xieying.web;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.xieying.dao.CategoryDao;
import com.xieying.entity.Category;
import com.xieying.framework.ActionSupport;
import com.xieying.framework.ModelDriver;
import com.xieying.util.ResponseUtil;
public class CategoryAction extends ActionSupport implements ModelDriver<Category> {
private CategoryDao categoryDao = new CategoryDao();
private Category category = new Category();
@Override
public Category getModel() {
return category;
}
public String combobox(HttpServletRequest req,HttpServletResponse resp) {
try {
List<Category> list = this.categoryDao.list(null, null);
ResponseUtil.writeJSON(resp, list);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
3、index.js
// 当浏览器访问主界面,ajax要加载书籍列表的数据
$.ajax({
url:ctx+'/category.action?methodName=combobox',
success:function(data){
data = eval('('+data+')');
for(i in data){
$(".list-group").append('<li class="list-group-item">'+data[i].name+'</li>');
$(".c-category li").eq(0).addClass('bg-color1');
$(".c-category li:gt(0)").addClass('bg-color2');
$(".c-category li:gt(0)").hover(function() {
$(this).addClass('bg-opacity');
}, function() {
$(this).removeClass('bg-opacity');
});
}
}
});
然后把jsp界面原有的书籍列表注释,就得到了以下结果
二、新书上架、热门书籍展示
1、BookDao
//新书上架
public List<Book> news(Book book,PageBean pagebean) throws InstantiationException, IllegalAccessException, SQLException{
String sql = "select * from t_easyui_book where state =2 order by deploytime desc limit 10";
return super.executeQuery(sql, Book.class, pagebean);
}
//热销书籍
public List<Book> hots(Book book,PageBean pagebean) throws InstantiationException, IllegalAccessException, SQLException{
String sql = "select * from t_easyui_book where state =2 order by sales desc limit 12";
return super.executeQuery(sql, Book.class, pagebean);
}
2、BookAction
public String news(HttpServletRequest req,HttpServletResponse resp) throws Exception {
try {
List<Book> news = this.bd.news(null, null);
ResponseUtil.writeJSON(resp, Result.ok(news));
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public String hots(HttpServletRequest req,HttpServletResponse resp) throws Exception {
try {
List<Book> hots = this.bd.hots(null, null);
ResponseUtil.writeJSON(resp, Result.ok(hots));
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
3、index.js
$(function(){
var ctx = $("#ctx").val();
// 加载新书(递归)
$.ajax({
url:ctx+'/book.action?methodName=news',
success:function(data){
data = eval('('+data+')');
// 给news container 所属的div添加row或者列
appendBookDiv($(".news"),data.data,0,"");
}
});
// 加载热销书籍(递归)
$.ajax({
url:ctx+'/book.action?methodName=hots',
success:function(data){
data = eval('('+data+')');
// 给news container 所属的div添加row或者列
appendBookDiv($(".hots"),data.data,0,"");
}
});
})
/*
* 1、给哪个div追加html内容(jquery对象)
* 2、最佳内容的数据来源
* 3、标识当前是第几行内容
* 4、最终追加的html
*/
function appendBookDiv($node,data,level,htmlstr){
// 第一行:0-5
var start = level * 6;
// data的数据12条
var len = data.length;
htmlstr += '<div class="row book">';
for(i=start;i<start+6;i++){
if(i==len) break;
htmlstr += '<div class="col-sm-2" style="width: 90px;">';
htmlstr += '<img src="'+data[i].image+'" >';
htmlstr += '<p>'+data[i].name+'</p>';
htmlstr += '<b>¥'+data[i].price+'</b>';
htmlstr += '</div>';
}
htmlstr += '</div>';
level++;
if(start+6>=len){
$node.append(htmlstr);
}else{
appendBookDiv($node,data,level,htmlstr);
}
}
新书上架效果:
热销书籍效果: