Elsha Destino
Elsha Destino

Reputation: 81

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. I tried setting it as a variable directly from my app controller but it returns a blank display. Any help on how I could display the total number of client/users is greatly appreciated.

Here's what I did

MY 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

<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>

Upvotes: 2

Views: 606

Answers (1)

queeg
queeg

Reputation: 9394

The answer will depend on what you consider to be a user that you want to count.

It is straightforward to count 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). Many frameworks also offer such counters. If need be, create and register a HttpSessionListener. With that you can count current and total amount of sessions created.

Upvotes: 2

Related Questions