masterdany88
masterdany88

Reputation: 5341

Thymeleaf apply java variable to global template. Make variable avalible in each page

In my global template I have menu.

In this menu I have select input with list of customers entity.

This menu is available on each page.

How can I apply that list of customers for global template usage (list will be cached).

Please help.

----EDIT-----

Here is my whole main controller:

package com.derp.common.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.derp.common.dao.CustomerDao;
import com.derp.common.dao.UserDao;
import com.derp.common.model.Customer;
import com.derp.common.model.User;
import com.derp.generic.controller.GenericController;

@Controller
public class IndexController extends GenericController {
    @Autowired private CustomerDao customerDao;

    @Transactional
    @RequestMapping(value="/", method=RequestMethod.GET)
    public ModelAndView mainPage(HttpSession session) {
        ModelAndView result = new ModelAndView("home");
        result.addObject("title", "Strona główna");
        System.out.println(session.getAttribute("customersList"));
        session.setAttribute("customersList", customerDao.get());
        return result;
    }
    @Transactional
    @RequestMapping(value="/index", method=RequestMethod.GET)
    public String mainPage2(HttpSession session) {
        return "redirect:/";
    }
    @RequestMapping(value="/o_systemie", method=RequestMethod.GET)
    public ModelAndView aboutSystem(HttpSession session) {
        ModelAndView result = new ModelAndView("aboutSystem");
        result.addObject("title", "O programie");
        return result;
    }
    @RequestMapping(value="/test", method=RequestMethod.GET)
    public ModelAndView testSystem(HttpSession session) {
        ModelAndView result = new ModelAndView("test");
        result.addObject("title", "Test");
        return result;
    }
    @ModelAttribute("customers")
    public List<Customers> populateCustomers() {
        return customerDao.get();
    }
}

Each of this controllers method generate custom view (f.e. test.html) and inject it in main view template (which is template.html)

In this line: session.setAttribute("customersList", customerDao.get()); is data that I would like to be populated always in template.html

Upvotes: 0

Views: 1592

Answers (2)

sven.kwiotek
sven.kwiotek

Reputation: 1479

You have also the possibility to make it more general with ControllerAdvice and Variable of your package name containing your controller (classes):

@ControllerAdvice("your.package.controllers")
class Advice {
    @Autowired
    CustomerService customerService;

    @ModelAttribute("customers")
    public List<Customers> populateCustomers() {
        return customerService.findAll();
    }
}

Upvotes: 1

sven.kwiotek
sven.kwiotek

Reputation: 1479

You can make use of @ModelAttribute on a method for example like this:

@ModelAttribute("customers")
public List<Customers> populateCustomers() {
    return this.customerService.findAll();
}

This method is used to populate the model with your customers. You have then access to e.g. customers.size etc... in your template.

Upvotes: 0

Related Questions