Flavio
Flavio

Reputation: 915

How to pass ID to controller?

I have a form

<form method="POST" action="/user/${id}">
    <input type="text" name="id" value="${id}" placeholder="Input Id">
    <button>Get User</button>
</form>

How to pass id to controller?

    @RequestMapping (value = "/user/{id}", method = RequestMethod.POST)
    public String getStudent(@PathVariable ("id") Integer id, Model model){
        User savedUser = userRepository.get(id);
        model.addAttribute("user", savedUser);
        return "user";
    }

Upvotes: 0

Views: 5188

Answers (1)

Santhosh
Santhosh

Reputation: 8197

You could do this way , consider i am passing the ${id} value through the query string

<a href="user?id=${id}">Get User</a>

And in your controller,

@RequestMapping ("user")
    public String getStudent(@RequestParam  Integer id, Model model){
        User savedUser = userRepository.get(id);
        model.addAttribute("user", savedUser);
        return "user";
    }

Hope this helps !

Upvotes: 1

Related Questions