Data response mode
Spring MVC's response to front-end requests can be divided into two categories and four types
Page Jump
For front-end requests, page Jump is realized by forwarding or redirection, and data return is supported
Return string
@RequestMapping("/say1") public String say1() { System.out.println("Meow"); return "success"; }
In this way, the returned string will be spliced with the prefix and suffix of the view parser and jump
Return ModelAndView object
(1) Create a ModelAndView object through the new operator and fill in the data returned
@RequestMapping("/say2") public ModelAndView say2() { ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("userName", "Owl Nighthawk"); modelAndView.setViewName("success"); return modelAndView; }
In the front-end page success.jsp, you can obtain data through EL expressions
<h1>${userName}</h1>
(2) Injection of ModelAndView parameters through MVC framework
@RequestMapping("/say3") public ModelAndView say3(ModelAndView modelAndView) { modelAndView.addObject("userName", "Owl Nighthawk"); modelAndView.setViewName("success"); return modelAndView; }
The MVC framework will automatically The ModelAndView parameter is injected so that it can be used directly in the method
(3) Model parameters are injected through MVC framework
@RequestMapping("/say4") public String say4(Model model) { model.addAttribute("userName", "Owl Nighthawk"); return "success"; }
Similar to the above example, the data is encapsulated in the Model object and returned as a string to represent the specific View
(4) HttpServletRequest parameter is injected through MVC framework
@RequestMapping("/say5") public String say5(HttpServletRequest request) { request.setAttribute("userName", "Owl Nighthawk"); return "success"; }
The request here is equivalent to the request object received in the native servlet, which contains the corresponding request information. This method is not commonly used. Use the object provided by the framework as much as possible
Write back data
Return string
(1) Through MVC framework HttpServletResponse parameter for injection
@RequestMapping("/say6") public void say6(HttpServletResponse response) throws IOException { response.setCharacterEncoding("unicode"); response.getWriter().print("Owl Nighthawk"); }
Return through response.getWriter().print() in the method, but it is not commonly used. Use the objects provided by the framework as much as possible
(2) Return data identification through @ ResponseBody annotation
@RequestMapping("/say7") @ResponseBody public String say7() { return "Owl Nighthawk"; }
@The ResponseBody annotation tells the MVC framework that the method is to return data instead of page Jump
Returns an object or collection
(1) Wrap json format string through jackson tool
Import dependent
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.13.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.13.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.13.0</version> </dependency>
Convert the object to be returned to json format string through the writeValueAsString() method of ObjectMapper object
@RequestMapping("/say8") @ResponseBody public String say8() throws JsonProcessingException { User user = new User(); user.setUserName("Owl Nighthawk"); user.setAge(3); ObjectMapper objectMapper = new ObjectMapper(); return objectMapper.writeValueAsString(user); }
(2) Configure the message converter to return custom objects directly
The required dependencies are the same as above
Configure the following code in spring-mvc.xml
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/> </list> </property> </bean>
Just return the custom object directly
@RequestMapping("/say9") @ResponseBody public User say9() { User user = new User(); user.setUserName("Owl Nighthawk"); user.setAge(3); return user; }
(3) The above configuration is replaced by annotation driven provided by MVC framework
Add a namespace in spring-mvc.xml
xmlns:mvc="http://www.springframework.org/schema/mvc"
Add after xsi:schemaLocation
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
Annotation driven as follows
<mvc:annotation-driven/>
Through annotation driven, the bottom layer automatically integrates jackson. At this time, the user-defined object can be returned directly without configuring the message converter