[英]How do I display the total number of users on my Web App?
I am a beginner in Java and I'm trying to display the total number of clients on my html file.我是 Java 的初学者,我正在尝试在我的 html 文件上显示客户端总数。 I tried setting it as a variable directly from my app controller but it returns a blank display.
我尝试直接从我的应用程序 controller 将其设置为变量,但它返回空白显示。 Any help on how I could display the total number of client/users is greatly appreciated.
非常感谢有关如何显示客户/用户总数的任何帮助。
Here's what I did这就是我所做的
MY CONTROLLER我的 CONTROLLER
@RequestMapping("/backoffice-admin/admins/editar-admin/{id}")
public String dash6a(@PathVariable("id") int id, Model model) {
model.addAttribute("num", clientRepository.countById(id));
return "/backoffice-admin/admins/editar-admin";
}
REPOSITORY存储库
package com.cometa.app.repository;
import javax.validation.Valid;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.transaction.annotation.Transactional;
import com.cometa.app.model.Client;
public interface ClientRepository extends CrudRepository<Client, Integer> {
@Query(value = "SELECT COUNT(id) FROM client;", nativeQuery = true)
int countById(int id);
}
HTML file HTML 文件
<div class="stats-holder-2-2-2" th:each="client, custStat: ${num}">
<a href="/backoffice-admin/clientes/todos-clientes" class="wrapper_stats-2 w-inline-block">
<div class="div-block-99">
<div class="div-block-137 azul">
<div class="text-block-113">1</div>
</div>
<div class="div-block-101" >
<div class="text-block-114 _303" th:text="${custStat.count}">[[${num}]]</div>
<div class="text-block-115">Clientes</div>
</div>
The answer will depend on what you consider to be a user that you want to count.答案将取决于您认为什么是您想要计算的用户。
It is straightforward to cound the requests.计算请求很简单。 Every time your application assembles the data to be returned to the client you can increase a count variable.
每次您的应用程序组装要返回给客户端的数据时,您都可以增加一个计数变量。 Many frameworks can provide such counts on their own as well.
许多框架也可以自行提供此类计数。
You could also check user sessions (given that your application works session oriented).您还可以检查用户会话(假设您的应用程序面向 session 工作)。 Many frameworks also offer such counters.
许多框架也提供这样的计数器。 If need be, create and register a HttpSessionListener .
如果需要,创建并注册一个HttpSessionListener 。 With that you can count current and total amount of sessions created.
有了它,您可以计算当前和创建的会话总数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.