S
S
SankaSanka2020-09-25 09:43:11
Java
SankaSanka, 2020-09-25 09:43:11

Spring how to pass value between controller methods?

recently with spring. don’t hit hard if it’s a very stupid question) I

add 2 variables to the model.

@GetMapping("/begin")
    public String starter(Model model) {
        ........
        
        model.addAttribute("citySet", setWords);
        model.addAttribute("citySuggestedSystem", citySuggestedSystem);
....
        return "begin";
    }

then I show a view where the user enters one word. and I need to process the result using these variables and a new word,

I'm trying to get these values ​​in a new controller like this:
@GetMapping("/next")
    public String customerInputNewWorld(@RequestParam String word, Model model) {
        @SuppressWarnings("unchecked")
        HashSet<String> setWords= (HashSet<String>) model.getAttribute("citySet");
        String citySuggestedSystem= (String) model.getAttribute("citySuggestedSystem");
.......
        System.out.println(citySuggestedSystem);
.....
        return "begin";
 
    }

and get null.

Am I doing something fundamentally wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Orkhan, 2020-09-25
@SankaSanka

Hello!
Use SessionAttribute for this
https://stackoverflow.com/questions/37433863/how-t...

S
SankaSanka, 2020-09-26
@SankaSanka

Yes. thank. line
@SessionAttributes({ "citySuggestedSystem", "citySet"}) solved my problem)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question