0

[Solved] I was wrong at so many levels, key concept was session good answer, i follow this way

 String index(HttpSession session) {
            session.setAttribute("mySessionAttribute", "someValue");
            return "index";
        }
<p th:text="${session.ShoppingCart.prop}" th:unless="${session == null}"> . </p>

I have a simple Spring boot app with Thymeleaf, the way I pass data to html from Controller is by using org.springframework.ui.Model;, it works fine but this data is accesible by everyone. The data is fetched from a public weather api, is not sensible but it is unique by user, the app is public and has not login so I can't store user state in a database.

I'm looking for something like a LocalStorage but for Spring a way to identify each user and store temporaly that data and then pass it to thymeleaf.

I thought about creating a random id on client browser, send it to server and persist the user info there, i will identify each user by cookies (if no cookie -> new id) but then.. how could I pass that info to thymeleaf? "privately".

The way i am used to handle that on previous apps was consuming the view directly on Js I am pretty new, and i feel that maybe I am missing some key concept here.

thank you guys.

Here is the flow: Controller.java

@GetMapping("/something")
public String something(Model model) {
    model.addAtributte("something",something);
    return "index";
    }

index.html

<p class="card" th:text="${something}"></p>

And here is what im looking

@GetMapping("/something")
public String something(MyPrivateModel notForEveryoneModel) {
    
    notForEveryoneModel.addAtributte("something",something);
    return "index";
    }
<p class="card" th:text="${ [notForEveryoneModel].something}"></p>
addUsername
  • 183
  • 1
  • 12
  • How do you know that the data is unique to user if it's public? Why don't you just put a login form? And finally how will you distinguish the same person connecting with different computers or browsers etc.? – Onur Baştürk Nov 02 '20 at 14:10
  • 1
    A `Model` is only living for a single request, so imho you are overthinking things. – M. Deinum Nov 02 '20 at 14:45

0 Answers0