Domanda

I have got a really easy question about sending parameters from Controller to View with thymeleaf.

I have got HTML page with line:

<p th:text="${xyz}"></p>

When I acces page http://localhost:8080/myproject-web/mypage this paragraph is empty.

When I click submit button it goes to controller:

@RequestMapping(value = "/subscribeth", params = { "save" })
public String save(final MyMainObject subscription, final BindingResult bindingResult, final ModelMap model) {
            System.out.println("I am here"); // it is displayed
    model.addAttribute("xyz", "Hello");
    return "redirect:/mypage";
}

So, it goes once again to mypage but the paragraph is still empty.

About what I forgot...? Thank you in advance

È stato utile?

Soluzione

You are actually redirecting the page. i.e. this will ask the client to resend the request to the given page. But, what you need it just a forward / a normal view rendering of mypage. So, you will have to change this

return "redirect:/mypage";

to

return "forward:/mypage";

Note : You can also set the values across a redirect request using RedirectAttributes

Altri suggerimenti

Returning "redirect:/mypage" sends makes Spring return an HTTP 302 to the user's browser for the link /mypage. Therefor when the user's browser requests that link, there is not model populated.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top