美文网首页
2018-12-22 页面优化技术-页面静态化、ajax

2018-12-22 页面优化技术-页面静态化、ajax

作者: 培根好吃 | 来源:发表于2018-12-23 16:07 被阅读0次

1.定义一个GoodsDetailVo类来接收从ajax页面传来的数据;

package com.ryan.miaosha.vo;

import com.ryan.miaosha.dao.MiaoshaUser;

public class GoodsDetailVo {
    private MiaoshaUser user;
    private int miaoshaStatus=0;
    private int remainSeconds=0;
    private GoodsVo goods;
    public MiaoshaUser getUser() {
        return user;
    }
    public void setUser(MiaoshaUser user) {
        this.user = user;
    }
    public int getMiaoshaStatus() {
        return miaoshaStatus;
    }
    public void setMiaoshaStatus(int miaoshaStatus) {
        this.miaoshaStatus = miaoshaStatus;
    }
    public int getRemainSeconds() {
        return remainSeconds;
    }
    public void setRemainSeconds(int remainSeconds) {
        this.remainSeconds = remainSeconds;
    }
    public GoodsVo getGoods() {
        return goods;
    }
    public void setGoods(GoodsVo goods) {
        this.goods = goods;
    }
}

1.如何利用ajax获取页面的参数

common.js里面

// 获取url参数
function g_getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r != null) return unescape(r[2]);
    return null;
};
//设定时间格式化函数,使用new Date().format("yyyyMMddhhmmss");  
Date.prototype.format = function (format) {  
    var args = {  
        "M+": this.getMonth() + 1,  
        "d+": this.getDate(),  
        "h+": this.getHours(),  
        "m+": this.getMinutes(),  
        "s+": this.getSeconds(),  
    };  
    if (/(y+)/.test(format))  
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));  
    for (var i in args) {  
        var n = args[i];  
        if (new RegExp("(" + i + ")").test(format))  
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? n : ("00" + n).substr(("" + n).length));  
    }  
    return format;  
};

1.goods_detail.htm

<!DOCTYPE HTML>
<html >
<head>
    <title>商品详情</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <!-- jquery -->
    <script type="text/javascript" src="/js/jquery.min.js"></script>
    <!-- bootstrap -->
    <link rel="stylesheet" type="text/css" href="/bootstrap/css/bootstrap.min.css" />
    <script type="text/javascript" src="/bootstrap/js/bootstrap.min.js"></script>
    <!-- jquery-validator -->
    <script type="text/javascript" src="/jquery-validation/jquery.validate.min.js"></script>
    <script type="text/javascript" src="/jquery-validation/localization/messages_zh.min.js"></script>
    <!-- layer -->
    <script type="text/javascript" src="/layer/layer.js"></script>
    <!-- md5.js -->
    <script type="text/javascript" src="/js/md5.min.js"></script>
    <!-- common.js -->
    <script type="text/javascript" src="/js/common.js"></script>
</head>
<body>

<div class="panel panel-default">
  <div class="panel-heading">秒杀商品详情</div>
  <div class="panel-body">
    <span id="userTip"> 您还没有登录,请登陆后再操作<br/></span>
    <span>没有收货地址的提示。。。</span>
  </div>
  <table class="table" id="goodslist">
    <tr>  
        <td>商品名称</td>  
        <td colspan="3" id="goodsName"></td> 
     </tr>  
     <tr>  
        <td>商品图片</td>  
        <td colspan="3"><img  id="goodsImg" width="200" height="200" /></td>  
     </tr>
     <tr>  
        <td>秒杀开始时间</td>  
        <td id="startTime"></td>
        <td >   
            <input type="hidden" id="remainSeconds" />
            <span id="miaoshaTip"></span>
        </td>
        <td>
        <!--  
            <form id="miaoshaForm" method="post" action="/miaosha/do_miaosha">
                <button class="btn btn-primary btn-block" type="submit" id="buyButton">立即秒杀</button>
                <input type="hidden" name="goodsId"  id="goodsId" />
            </form>-->
            <button class="btn btn-primary btn-block" type="button" id="buyButton"onclick="doMiaosha()">立即秒杀</button>
            <input type="hidden" name="goodsId"  id="goodsId" />
        </td>
     </tr>
     <tr>  
        <td>商品原价</td>  
        <td colspan="3" id="goodsPrice"></td>  
     </tr>
      <tr>  
        <td>秒杀价</td>  
        <td colspan="3"  id="miaoshaPrice"></td>  
     </tr>
     <tr>  
        <td>库存数量</td>  
        <td colspan="3"  id="stockCount"></td>  
     </tr>
  </table>
</div>
</body>
<script>

function doMiaosha(){
    $.ajax({
        url:"/miaosha/do_miaosha",
        type:"POST",
        data:{
            goodsId:$("#goodsId").val(),
        },
        success:function(data){
            if(data.code == 0){
                window.location.href="/order_detail.htm?orderId="+data.data.id;
            }else{
                layer.msg(data.msg);
            }
        },
        error:function(){
            layer.msg("客户端请求有误");
        }
    });
    
}

function render(detail){
    var miaoshaStatus = detail.miaoshaStatus;
    var  remainSeconds = detail.remainSeconds;
    var goods = detail.goods;
    var user = detail.user;
    if(user){
        $("#userTip").hide();
    }
    $("#goodsName").text(goods.goodsName);
    $("#goodsImg").attr("src", goods.goodsImg);
    $("#startTime").text(new Date(goods.startDate).format("yyyy-MM-dd hh:mm:ss"));
    $("#remainSeconds").val(remainSeconds);
    $("#goodsId").val(goods.id);
    $("#goodsPrice").text(goods.goodsPrice);
    $("#miaoshaPrice").text(goods.miaoshaPrice);
    $("#stockCount").text(goods.stockCount);
    countDown();
}

$(function(){
    //countDown();
    getDetail();
});

function getDetail(){
    var goodsId = g_getQueryString("goodsId");
    $.ajax({
        url:"/goods/detail/"+goodsId,
        type:"GET",
        success:function(data){
            if(data.code == 0){
                render(data.data);
            }else{
                layer.msg(data.msg);
            }
        },
        error:function(){
            layer.msg("客户端请求有误");
        }
    });
}

function countDown(){
    var remainSeconds = $("#remainSeconds").val();
    var timeout;
    if(remainSeconds > 0){//秒杀还没开始,倒计时
        $("#buyButton").attr("disabled", true);
       $("#miaoshaTip").html("秒杀倒计时:"+remainSeconds+"秒");
        timeout = setTimeout(function(){
            $("#countDown").text(remainSeconds - 1);
            $("#remainSeconds").val(remainSeconds - 1);
            countDown();
        },1000);
    }else if(remainSeconds == 0){//秒杀进行中
        $("#buyButton").attr("disabled", false);
        if(timeout){
            clearTimeout(timeout);
        }
        $("#miaoshaTip").html("秒杀进行中");
    }else{//秒杀已经结束
        $("#buyButton").attr("disabled", true);
        $("#miaoshaTip").html("秒杀已经结束");
    }
}

</script>
</html>

1.goods_detail controller层静态化

页面缓存

 @RequestMapping("/detail/{goodsId}")
    @ResponseBody
    Result<GoodsDetailVo> detail(Model model, MiaoshaUser user,@PathVariable("goodsId") long goodsId) {         
        model.addAttribute("user", user);
        GoodsVo goods=goodsService.getGoodsVoByGoodsId(goodsId);
        model.addAttribute("goods", goods);
        long startDate = goods.getStartDate().getTime();
        long endDate = goods.getEndDate().getTime();
        long currentTimeMillis = System.currentTimeMillis();
        int miaoshaStatus=0;
        int remainSeconds=0;
        if(currentTimeMillis<startDate) {//秒杀还没开始 进入倒计时
            miaoshaStatus=0;
            remainSeconds=(int) ((startDate-currentTimeMillis)/1000);
        } else if(currentTimeMillis>endDate) {//秒杀已经结束
            miaoshaStatus=2;
            remainSeconds=-1;
        }else {//秒杀正在进行中
            miaoshaStatus=1;
            remainSeconds=0;
        }
        model.addAttribute("miaoshaStatus", miaoshaStatus);
        model.addAttribute("remainSeconds", remainSeconds);
        GoodsDetailVo goodsDetailVo=new GoodsDetailVo();
        goodsDetailVo.setGoods(goods);
        goodsDetailVo.setMiaoshaStatus(miaoshaStatus);
        goodsDetailVo.setRemainSeconds(remainSeconds);
        goodsDetailVo.setUser(user);
         
        return Result.success(goodsDetailVo);
    }

1.miaoshaController改为静态

package com.imooc.miaosha.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.imooc.miaosha.domain.MiaoshaOrder;
import com.imooc.miaosha.domain.MiaoshaUser;
import com.imooc.miaosha.domain.OrderInfo;
import com.imooc.miaosha.redis.RedisService;
import com.imooc.miaosha.result.CodeMsg;
import com.imooc.miaosha.result.Result;
import com.imooc.miaosha.service.GoodsService;
import com.imooc.miaosha.service.MiaoshaService;
import com.imooc.miaosha.service.MiaoshaUserService;
import com.imooc.miaosha.service.OrderService;
import com.imooc.miaosha.vo.GoodsVo;

@Controller
@RequestMapping("/miaosha")
public class MiaoshaController {

    @Autowired
    MiaoshaUserService userService;
    
    @Autowired
    RedisService redisService;
    
    @Autowired
    GoodsService goodsService;
    
    @Autowired
    OrderService orderService;
    
    @Autowired
    MiaoshaService miaoshaService;
    
    /**
     * QPS:1306
     * 5000 * 10
     * */
    /**
     *  GET POST有什么区别?
     * */
    @RequestMapping(value="/do_miaosha", method=RequestMethod.POST)
    @ResponseBody
    public Result<OrderInfo> miaosha(Model model,MiaoshaUser user,
            @RequestParam("goodsId")long goodsId) {
        model.addAttribute("user", user);
        if(user == null) {
            return Result.error(CodeMsg.SESSION_ERROR);
        }
        //判断库存
        GoodsVo goods = goodsService.getGoodsVoByGoodsId(goodsId);//10个商品,req1 req2
        int stock = goods.getStockCount();
        if(stock <= 0) {
            return Result.error(CodeMsg.MIAO_SHA_OVER);
        }
        //判断是否已经秒杀到了
        MiaoshaOrder order = orderService.getMiaoshaOrderByUserIdGoodsId(user.getId(), goodsId);
        if(order != null) {
            return Result.error(CodeMsg.REPEATE_MIAOSHA);
        }
        //减库存 下订单 写入秒杀订单
        OrderInfo orderInfo = miaoshaService.miaosha(user, goods);
        return Result.success(orderInfo);
    }
}

1.订单支付详情静态化

@RequestMapping("/detail")
    @ResponseBody
    Result<OrderDetailVo> orderDetail(Model model,MiaoshaUser user,@RequestParam("orderId") long orderId) {
        if(user==null) {
            return Result.error(CodeMsg.SESSION_ERROR);
        }
        OrderInfo order=orderService.getOrderById(orderId);
        if(order==null) {
            return Result.error(CodeMsg.ORDER_IS_NOT_EXIST);
        }
        Long id = order.getGoodsId();
        GoodsVo goodsVoByGoodsId = goodsService.getGoodsVoByGoodsId(id);
        OrderDetailVo orderInfo=new OrderDetailVo();
        orderInfo.setGoodsVo(goodsVoByGoodsId);
        orderInfo.setOrderInfo(order);
        return Result.success(orderInfo);
    }

1.订单详情页面 order_detail.htm

<!DOCTYPE HTML>
<html>
<head>
    <title>订单详情</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <!-- jquery -->
    <script type="text/javascript" src="/js/jquery.min.js"></script>
    <!-- bootstrap -->
    <link rel="stylesheet" type="text/css" href="/bootstrap/css/bootstrap.min.css" />
    <script type="text/javascript" src="/bootstrap/js/bootstrap.min.js"></script>
    <!-- jquery-validator -->
    <script type="text/javascript" src="/jquery-validation/jquery.validate.min.js"></script>
    <script type="text/javascript" src="/jquery-validation/localization/messages_zh.min.js"></script>
    <!-- layer -->
    <script type="text/javascript" src="/layer/layer.js"></script>
    <!-- md5.js -->
    <script type="text/javascript" src="/js/md5.min.js"></script>
    <!-- common.js -->
    <script type="text/javascript" src="/js/common.js"></script>
</head>
<body>
<div class="panel panel-default">
  <div class="panel-heading">秒杀订单详情</div>
  <table class="table" id="goodslist">
        <tr>  
        <td>商品名称</td>  
        <td colspan="3" id="goodsName"></td> 
     </tr>  
     <tr>  
        <td>商品图片</td>  
        <td colspan="2"><img  id="goodsImg" width="200" height="200" /></td>  
     </tr>
      <tr>  
        <td>订单价格</td>  
        <td colspan="2"  id="orderPrice"></td>  
     </tr>
     <tr>
            <td>下单时间</td>  
            <td id="createDate" colspan="2"></td>  
     </tr>
     <tr>
        <td>订单状态</td>  
        <td id="orderStatus">
        </td>  
        <td>
            <button class="btn btn-primary btn-block" type="submit" id="payButton">立即支付</button>
        </td>
     </tr>
      <tr>
            <td>收货人</td>  
            <td colspan="2">XXX  18812341234</td>  
     </tr>
     <tr>
            <td>收货地址</td>  
            <td colspan="2">北京市昌平区回龙观龙博一区</td>  
     </tr>
  </table>
</div>
</body>

<script>
function render(detail){
    var order = detail.orderInfo;
    var goods = detail.goodsVo;
    $("#goodsName").text(goods.goodsName);
    $("#goodsImg").attr("src", goods.goodsImg);
    $("#orderPrice").text(order.goodsPrice);
    $("#createDate").text(new Date(order.createDate).format("yyyy-MM-dd hh:mm:ss"));
    var status = "";
    if(order.status == 0){
        status = "未支付"
    }else if(order.status == 1){
        status = "待发货";
    }
    $("#orderStatus").text(status);
}
$(function(){
    getOrderDetail();
})

function getOrderDetail(){
    var orderId = g_getQueryString("orderId");
    $.ajax({
        url:"/order/detail",
        type:"GET",
        data:{
            orderId:orderId
        },
        success:function(data){
            if(data.code == 0){
                render(data.data);
            }else{
                layer.msg(data.msg);
            }
        },
        error:function(){
            layer.msg("客户端请求有误");
        }
    });
}



</script>
</html>

相关文章

  • 2018-12-22 页面优化技术-页面静态化、ajax

    1.定义一个GoodsDetailVo类来接收从ajax页面传来的数据; 1.如何利用ajax获取页面的参数 co...

  • django页面静态化和缓存小结

    写在前面 基础的学了页面静态化和缓存,这篇文章来总结一下。 页面静态化和页面数据缓存 对网站本身性能的优化,减少数...

  • 页面静态化技术

    页面静态化概述 静态页面: 最早的时候,网站内容是通过在主机空间中放置大量的静态网页实现的。为了方便对这些分散在不...

  • 作品汇总

    这是我做的作品跟Demo汇总,希望您能够喜欢 贸易商城的页面静态页面一静态页面二静态页面三利用AJAX做的加载更多...

  • SpringBoot2 整合FreeMarker模板,完成页面静

    本文源码:GitHub·点这里 || GitEE·点这里 一、页面静态化 1、动静态页面 静态页面 即静态网页,...

  • SAAS-HRM-day7(页面静态化分析)

    1. 静态化优化方案设计1.1 为什么要使用页面静态化1.2 静态化的好处1.3 方案分析1.3.1 基本分析1....

  • 静态模板freeMarker页面静态化

    如果采用cms系统就可以实现由cms自动化对页面进行页面进行更新,自动采用静态化的技术就是生成html页面,快速上...

  • FreeMarker页面静态化

    1、页面静态化 1.1 页面静态化 模板+数据模型=输出,页面静态化需要准备数据模型和模板,先知道数据模型的结构才...

  • mysql优化

    mysql优化 mysql优化概述 前面我们讲页面静态化,memcache是通过减少对mysql 操作来提升访问速...

  • 大数据并发处理解决方案:

    1、HTML静态化 效率最高、消耗最小的就是纯静态化的html页面,所以尽可能使网站上的页面采用静态页面来实现,这...

网友评论

      本文标题:2018-12-22 页面优化技术-页面静态化、ajax

      本文链接:https://www.haomeiwen.com/subject/rsgwkqtx.html