Spring Boot入门(12)实现页面访问量统计功能
在日常的网站使用中,经常会碰到页面的访问量(或者访问者人数)统计。那么,在Spring Boot中该如何实现这个功能呢?
我们的想法是比较简单的,那就是将访问量储存在某个地方,要用的时候取出来即可,储存的位置可选择数据库或者其他文件。本例所使用的例子为txt文件,我们将访问量数据记录在D盘的count.txt文件中。
下面直接开始本次的项目。整个项目的完整结构如下:
我们只需要修改划红线的三个文件,其中build.gradle的代码如下:
buildscript {
ext {
springBootVersion = '2.0.3.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf
compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf', version: '2.0.1.RELEASE'
}
视图文件(模板)index.HTML的代码如下:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>访问统计</title>
<script th:inline="javascript">
function load(){
var count = [[${count}]];
document.getElementById("visit").innerHTML = count.toString();
}
</script>
</head>
<body onload="load()">
<h1>Hello, world!</h1>
<p>  本页面已被访问<span id="visit"></span>次。</p>
</body>
</html>
控制器文件VisitController.java文件的代码如下:
package com.example.visit.Controller;
import java.io.*;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class VisitController {
@GetMapping("/index")
public String Index(Map <String, Object> map){
// 获取访问量信息
String txtFilePath = "D://count.txt";
Long count = Get_Visit_Count(txtFilePath);
System.out.println(count);
map.put("count", count); // 后台参数传递给前端
return "index";
}
/*
* 获取txt文件中的数字,即之前的访问量
* 传入参数为: 字符串: txtFilePath,文件的绝对路径
*/
public static Long Get_Visit_Count(String txtFilePath) {
try {
//读取文件(字符流)
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(txtFilePath),"UTF-8"));
//循环读取数据
String str = null;
StringBuffer content = new StringBuffer();
while ((str = in.readLine()) != null) {
content.append(str);
}
//关闭流
in.close();
//System.out.println(content);
// 解析获取的数据
Long count = Long.valueOf(content.toString());
count ++; // 访问量加1
//写入相应的文件
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(txtFilePath),"UTF-8"));
out.write(String.valueOf(count));
//清楚缓存
out.flush();
//关闭流
out.close();
return count;
}
catch (Exception e){
e.printStackTrace();
return 0L;
}
}
}
这样我们就完成了整个项目的配置,最后,我们在D盘中的count.txt中写入数字0,作为初始访问量。
运行Spring Boot项目,在浏览器中输入localhost:8080/index , 显示的页面如下:
刚载入页面时,显示页面被访问1次。当我们将这个这也载入10次后,显示如下:
这样我们就用Spring Boot实现了页面访问量的统计功能。
本次分享到此结束,欢迎大家交流~~
注意:本人现已开通两个微信公众号: 因为Python(微信号为:python_math)以及轻松学会Python爬虫(微信号为:easy_web_scrape), 欢迎大家关注哦~~
Spring Boot入门(12)实现页面访问量统计功能的更多相关文章
- Spring Boot入门(11)实现文件下载功能
在这篇博客中,我们将展示如何在Spring Boot中实现文件的下载功能. 还是遵循笔者写博客的一贯风格,简单又不失详细,实用又能让你学会. 本次建立的Spring Boot项目的主要功能 ...
- Spring boot入门(三):SpringBoot集成结合AdminLTE(Freemarker),利用generate自动生成代码,利用DataTable和PageHelper进行分页显示
关于SpringBoot和PageHelper,前篇博客已经介绍过Spring boot入门(二):Spring boot集成MySql,Mybatis和PageHelper插件,前篇博客大致讲述了S ...
- Spring Boot 入门(十二):报表导出,对比poi、jxl和esayExcel的效率
本片博客是紧接着Spring Boot 入门(十一):集成 WebSocket, 实时显示系统日志写的 关于poi.jxl和esayExcel的介绍自行百度. jxl最多支持03版excel,所以单个 ...
- spring boot入门教程——Spring Boot快速入门指南
Spring Boot已成为当今最流行的微服务开发框架,本文是如何使用Spring Boot快速开始Web微服务开发的指南,我们将使创建一个可运行的包含内嵌Web容器(默认使用的是Tomcat)的可运 ...
- Spring Boot 入门之持久层篇(三)
原文地址:Spring Boot 入门之持久层篇(三) 博客地址:http://www.extlight.com 一.前言 上一篇<Spring Boot 入门之 Web 篇(二)>介绍了 ...
- Spring Boot 入门之 Web 篇(二)
原文地址:Spring Boot 入门之 Web 篇(二) 博客地址:http://www.extlight.com 一.前言 上一篇<Spring Boot 入门之基础篇(一)>介绍了 ...
- Spring Boot 入门 - 目录
pring Boot 入门 - 进阶篇(3)- 定时任务(@Scheduled) 主要用于定时发送邮件.夜间自动维护等. (1)开启定时任务功能 @Configuration @EnableSched ...
- 161103、Spring Boot 入门
Spring Boot 入门 spring Boot是Spring社区较新的一个项目.该项目的目的是帮助开发者更容易的创建基于Spring的应用程序和服务,让更多人的人更快的对Spring进行入门体验 ...
- Spring Boot 入门(六):集成 treetable 和 zTree 实现树形图
本篇文章是接着Spring Boot 入门(五):集成 AOP 进行日志管理写的,主要集成了树形图,在部门列表或者权限列表中,树形图经常被用上.主要是根据相应的 API 凭借 html 字符串 1.t ...
随机推荐
- How to enable C development in a Windows 10 development environment VM
To enable C development in a Windows 10 development environment VM, follow these steps: Start VS in ...
- Note of Python Math
Note of Python Math math 库是Python 提供的内置数学类函数库,而其中复数类型常用于科学计算,一般计算并不常用,因此math 库不支持复数类型.math 库一共提供4个数学 ...
- 【洛谷P2584】【ZJOI2006】GameZ游戏排名系统题解
[洛谷P2584][ZJOI2006]GameZ游戏排名系统题解 题目链接 题意: GameZ为他们最新推出的游戏开通了一个网站.世界各地的玩家都可以将自己的游戏得分上传到网站上.这样就可以看到自己在 ...
- Leetcode(一)两数之和
1.两数之和 题目要求: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重 ...
- eclipse项目无故报错,markers信息为An error occurred while filtering resources
eclipse项目无故报错,markers信息为An error occurred while filtering resources 描述:eclipse项目和resource文件上有红色的叉,其m ...
- day23_雷神_crm-day2
# 俺滴第一个项目 CRM MdelForm 实现增删改查 1. ModelForm,重写 __init__ 方法,给所有字段添加 form-control 样式. 2. ModelForm,报错错误 ...
- 常用maven仓库
常用Maven仓库网址:http://mvnrepository.com/http://search.maven.org/http://repository.sonatype.org/content/ ...
- 网易彩票-我的彩票-设置-cell跳转界面
1. 点击“cell”推出对应的界面 1.1 新建group,名为:Setting 路径:MYLottery(我的彩票)->Controller 1.2 新建Cocoa Touch Class, ...
- Redis的使用及参考代码
Redis是一种完全开源免费,高性能的key-value数据库或数据结构服务器,因为value值可以是字符串,哈希(map),列表list,集合等. Jedis 是 Redis 官方首选的 Java ...
- ruby-操作mysql
ruby操作mysql数据库 以centos7.2为实验环境 Table of Contents 使用DBI访问Mysql 使用Mysql2访问Mysql DBI 安装DBI驱动 很多同学在公司是没有 ...