第十天了,今天在上完工作室的课之后马上就动手开始实践了,主要做了在网页上进行模糊查询的功能,并将结果进行分页展示(与前面学的分页结合起来了)。
效果如下
部分核心代码展示
recordInit("")//初始化搜索页面
$("#query-bookName").click(function (){
var bookName=$("#querykey").val();
alert(bookName);
recordInit(bookName)
})
function recordInit(bookName){
$.getJSON(countURL,{bookName:bookName},function (data){
if(data.success){
alert(data.totalCounts)
pageInit(data.totalCounts,5,bookName);
}
})
}
function pageInfo(pageIndex,pageSize,bookName){
var bookURL = "/shopping_war_exploded/probookid.do";
$.getJSON(bookURL,{pageIndex:pageIndex,pageSize: pageSize,bookName:bookName},function (data){
if(data.success){
alert(data.productList)
var productList = data.productList;
var htmlval = '';
productList.map(function (item){
htmlval+='<li class="row recom-1"\n' +
' style="margin-top:5px;margin-bottom:5px;display:block;height:242px;background-color:#f8f8f8">\n' +
' <div class="col-md-3" style="line-height:242px;">\n' +
' <a href="' +item.bookImage+ '"target="_blank" alt="点击看大图" class="img-thumbnail"><img\n' +
' src="'+'.'+item.bookImage+'" class="imgBook"/></a>\n' +
' </div>\n' +
' <div class="col-md-9" style="">\n' +
' <div class="row">\n' +
' <ul>\n' +
' <li><span style="font-weight:bold;font-size:14px;line-height:24px;">'+item.bookName+'</span></li>\n' +
' <li><span class="search_now_price">' +item.discount+'</span><span\n' +
' class="search_pre_price">' +item.price +'</span></li>\n' +
'\n' +
' <li><span style="color:blue">' +item.bookAuthor +'</span>' +
' <span style="color:blue">' +item.publishingName +'</span>\n' +
' <span style="color:blue" >' +item.bookPublishTime +'</span></li>\n' +
' <li>ISBN:<span style="color:blue">' +item.bookIsbn +'</span></li>\n' +
' <li>所属分类:<span style="color:blue">' +item.bookTypeName +'</span></li>\n' +
' <li><p style="height:60px;overflow:hidden">\n' +
' ' +item.bookIntroduction +'<</p>\n' +
' </li>\n' +
' </ul>\n' +
' </div>\n' +
' <div class="row" style="">\n' +
' <button type="button" class="btn btn-primary">收藏</button>\n' +
' <button type="button" class="btn btn-success">加入购物车</button>\n' +
' <button type="button" class="btn btn-danger">一键购买</button>\n' +
' </div>\n' +
' </div>\n' +
' </li>'
})
$("#content-body").html(htmlval);
}
})
}
/**
* 分页展示
* @param totalCounts 总的记录数
* @param pagesize 每一页的大小
*/
function pageInit(totalCounts,pagesize,bookName){
$("#pagination").jqPaginator({
totalCounts: totalCounts,//总的记录数
pageSize: pagesize,//每一页的大小
totalPages: Math.ceil (totalCounts/pagesize),//总的页码数
visiblePages: 10,//可见页
currentPage: 1,//当前页
first: '<li class="first"><a href="javascript:void(0);">首页<\/a><\/li>',
prev: '<li class="prev"><a href="javascript:void(0);">前一页<\/a><\/li>',
next: '<li class="next"><a href="javascript:void(0);">后一页<\/a><\/li>',
last: '<li class="last"><a href="javascript:void(0);">尾页<\/a><\/li>',
page: '<li class="page"><a href="javascript:void(0);">{{page}}<\/a><\/li>',
onPageChange: function (pageIndex) {//响应页面变化的响应函数
alert("hhh")
$("#text").html(pageIndex)
pageInfo(pageIndex,this.pageSize,bookName);
}
});
}
@WebServlet(name = "QueryProductBookIdServlet", value = "/probookid.do")
public class QueryProductBookIdServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String strPageSize=request.getParameter("pageSize");
String strPageIndex=request.getParameter("pageIndex");//当前页
String bookName=request.getParameter("bookName");
//System.out.println(strPageIndex);
//System.out.println(strPageSize);
int pageSize,pageIndex;
try{
pageIndex=Integer.parseInt(strPageIndex);
pageSize=Integer.parseInt(strPageSize);
} catch (NumberFormatException e)
{
pageIndex=1;
pageSize=5;
}
pageIndex=Integer.parseInt(strPageIndex);
int rowIndex=(pageIndex-1)*pageSize;
ProductService productService=new ImplProductService();
List<ProductDomain> list = null;
try {
list = productService.queryProductOrderID(rowIndex,pageSize,bookName);
Map<String,Object> model = new HashMap<String,Object>();
model.put("success",true);
model.put("productList",list);
response.setContentType("application/json;charset=utf-8");
PrintWriter out = response.getWriter();
ObjectMapper objectMapper=new ObjectMapper();
out.println(objectMapper.writeValueAsString(model));
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}
每日一题,其实一开始我也没看懂这个题目,没有头绪,看了别人的解析后,就发现要做这道题首先得理解丑数是怎么得来的,因为它的因子只有2,3,5,所以一个丑数会等于另一个丑数*2或*3或*5得到,那就只要从第一个丑数1开始*2、*3、*5再 把得到的2,3,5中最小的2加入丑数数组中,创建三个队列(*2的,*3的,*5的)再把最小的加入丑数数组中,以此类推。
import java.util.*;
public class Solution {
public int GetUglyNumber_Solution(int index) {
//通过计算容易得0-6的丑数分别为0-6
if(index < 7)
return index;
ArrayList<Integer> list =new ArrayList<Integer>();
list.add(1);//把第一个丑数1加进去
int n2=0,n3=0,n5=0;//开始都取第一个值
while(list.size()<index)
{
//选出三个队列头最小的数
int m2=list.get(n2)*2;//得到*2的队列
int m3=list.get(n3)*3;//得到*3的队列
int m5=list.get(n5)*5;//得到*5的队列
int min=Math.min(m2,Math.min(m3,m5));
list.add(min);
if(min==m2) //最小值在哪个队列头,ni++
n2++;
if(min==m3)
n3++;
if(min==m5)
n5++;
}
return list.get(list.size()-1);
}
}