Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

java - Explanation of the model object in Spring

@Controller
public class GreetingController {

    @GetMapping("/greeting")
    public String greeting(HttpServletRequest request, Model model) {

        String name = request.getParameter("name");
        model.addAttribute("name", name);

        return "greeting";
    }
}

What does the Model do? Is this essentially the context variables that are passed to the "greeting.html" file? Is the only use of it addAttributes that will then be passed to the HTML template? Where could I read more about what Model does and how it should be used?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Did you see the documentation of Model?

Java-5-specific interface that defines a holder for model attributes. Primarily designed for adding attributes to the model. Allows for accessing the overall model as a java.util.Map.

Model is an essential part of MVC pattern which is widely used in Spring. As you have said, a Model is a holder of the context data passed by a Controller to be displayed on a View.

You can use only one Model which contains more data distinct with a unique key because the Model is based on the java.util.Map - as the documentation says..


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...