zoukankan      html  css  js  c++  java
  • springmvc+ajax——第二讲(页面缓存)

    springmvc+ajax+页面缓存(参考:https://www.cnblogs.com/liuling/archive/2013/07/25/2013-7-25-01.html)

    必须设置响应头才能实现页面缓存效果,一般对css等静态文件可以使用缓存,但是对于动态则不能使用缓存,springmvc默认是无缓存的。如果要想设置成有缓存,修改controller类:


    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Date;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    @Controller
    public class TestController {
    public TestController() {
    System.out.println("------TestController------");
    }
    @RequestMapping(value="/testAjax",method=RequestMethod.GET)//m                     //如果在前端alert("xxx")能执行通,但是后台没反应可能是                       //method错了:ethod=RequestMethod.GET
    public void testAjax(HttpServletRequest req,HttpServletResponse resp){
    // System.out.println("testAjax");
    String param = null;
    param = req.getParameter("param");
    // System.out.println(param);
    Date date = new Date();
    resp.setDateHeader("Last-Modified", date.getTime());

    //为了更好地测试缓存效果,可以设置false:如xmlHttpRequest.open("GET", "./testAjax?param="+param, false);点击按钮就会卡住,知道几秒

    //之后才可以

    resp.setHeader("Pragma", "Pragma");
    resp.setHeader("cache-control", "public");

    int mills = 0;
    while(mills < 5){
    try {
    Thread.sleep(1000);
    System.out.println("睡眠"+mills+"秒后");
    mills ++ ;
    } catch (InterruptedException e1) {
    e1.printStackTrace();
    }
    }

    PrintWriter pw = null;
    try {
    pw = resp.getWriter();
    pw.print(param);
    } catch (IOException e) {
    e.printStackTrace();
    }finally{
    pw.flush();
    pw.close();
    System.out.println("finally");
    }
    }


    }

  • 相关阅读:
    大数据学习总结(7)we should...
    大数据学习总结(6)what is our foucus
    洛谷P4632 [APIO2018] New Home 新家(动态开节点线段树 二分答案 扫描线 set)
    BZOJ5249: [2018多省省队联测]IIIDX(线段树 贪心)
    BZOJ2438: [中山市选2011]杀人游戏(tarjan)
    cf1072D. Minimum path(BFS)
    cf1072B. Curiosity Has No Limits(枚举)
    cf567E. President and Roads(最短路计数)
    六校联考考试反思
    BZOJ4010: [HNOI2015]菜肴制作(拓扑排序 贪心)
  • 原文地址:https://www.cnblogs.com/lirenhe/p/9774459.html
Copyright © 2011-2022 走看看