I have one controller as:
@Controller
public class UserController {
@Autowired
private UserService userService;
protected int userId; // I need to use it across many other controllers. HOW?
@GetMapping("/")
private String viewHomePage(Model model) {
return "eAccounts";
}
@GetMapping("/dashboard")
private String getDashboardPage() {
return "dashboard";
}
@GetMapping("/signup")
private String getSignupPage(Model model) {
User user = new User();
model.addAttribute("user", user);
return "signup";
}
@PostMapping("/verifySignup")
private String verifySignup(@ModelAttribute("user") User user, Model model, HttpServletRequest request) {
if (userService.verifySignup(user) == 1) {
userId = user.getId();// save user id for later use
return "redirect:/dashboard";
}
return "signup";
}
}
And there are multiple controllers (e.g., assetController, liabilityController, etc):
@Controller
public class AssetController {
@Autowired
private AssetService assetService;
@GetMapping("/assets")
public String getAssetsPage(Model model) {
model.addAttribute("listAssets", assetService.getAllAssets(userId)); // here i need to use userId, and at many more places
return "assets";
}
}
I don't want to merge all my controllers code in one single controller. I just need to save userId whenever user signs up or login in the website, and then use it across many other controllers. Any clean solution will be appreciated.