Ininiv
Ininiv

Reputation: 1325

Use of Model object as a param in the request handling methods

I just want to know

Why do we use Model object as a Parameter to the request handling method in the Spring MVC application?

Basic Explanation helps me a lot.

Upvotes: 1

Views: 66

Answers (2)

Arun
Arun

Reputation: 546

ModelAndView (or Model) is a specialized spring object to store name value pairs (kind of java Map). It is optional to have the Model Object as a parameter to the request method. However in case if your request method has anything that needs to be passed on to the View; then you need a Model.

So Model is basically a data structure that carries information from the service layer to the view layer.

You can also initialize a Model inside your request method as:

public ModelAndView listCarrier() {
    HashMap<String, Object> model = new HashMap<String, Object>();
    model.put("isView", request.getParameter("isView"));
    return model;
}

Upvotes: 1

dReAmEr
dReAmEr

Reputation: 7196

You can add attribute to Model object and use that attribute inside your JSP like below.

@RequestMapping(method = RequestMethod.GET)
public String login(String name, Model model) {
        model.addAttribute("name", name);
        return "xyz";
    }

and later on you can access this property in your xyz.jsp like below.

Name: ${name}

For more info refer :-Model API docs

Upvotes: 0

Related Questions