Raj
Raj

Reputation: 1724

How to pass single parameter to contoller in Spring MVC?

I have some questions from a design point of view in Spring Web MVC.

Is it good practice to use Request Object in controller? If not, then what is alternative way to pass pass one text fields value to controller? Do I need to create one new from bean for this single fields?

Upvotes: 0

Views: 280

Answers (1)

Gerard Ribas
Gerard Ribas

Reputation: 727

It depends of the situation, in a few cases I used the HttpServletRequest; for example for writing a file to the output stream.

If you want to get the Request Parameters you can use the annotation @RequestParam, that it´s more easy to get the parameters from the request.

Depends that you want to handle, for example for a form you can use @ModelAttribute and this attribute can be in a session or in the request.

For example:

@Controller
public class YourController {

 @RequestMapping(value = "someUrl", method = RequestMethod.GET)
    public String someMethod(@RequestParam("someProperty") String myProperty)  
    {
        // ... do some stuff
    }
}

Check the documentation here:

@RequestParam
@ModelAttribute
@PathVariable

Upvotes: 1

Related Questions