[SSM hits the big factory directly] Chapter 5: SpringMVC data response

content

SpringMVC's data response method

1) page jump  

2) Write back data

3) Configure annotation driver

4) Knowledge points


SpringMVC's data response method

1) page jump  

  • Return the string directly: In this method, the returned string is spliced ​​with the prefix and suffix of the view parser and then jumps. 

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5rGC5LiN6ISx5Y-R,size_20,color_FFFFFF,t_70,g_se,x_16

Return a string with prefix:
Forward: forward:/WEB-INF/views/index.jsp
Redirect: redirect:/index.jsp
  • Returned by ModelAndView object
@RequestMapping("/quick2")
public ModelAndView quickMethod2(){
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("redirect:index.jsp");
    return modelAndView;
}
@RequestMapping("/quick3")
public ModelAndView quickMethod3(){
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("forward:/WEB-INF/views/index.jsp");
    return modelAndView;
}

 When forwarding, it is often necessary to store data in the request field and display it in the jsp page, so how does the Controller store data in the request field?

① Set by the request object setAttribute() method injected by the SpringMVC framework.

@RequestMapping("/quick")
public String quickMethod(HttpServletRequest request){
    request.setAttribute("name","zhangsan");
    return "index";
}

② Set by the addObject() method of ModelAndView.

@RequestMapping("/quick3")
public ModelAndView quickMethod3(){
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("forward:/WEB-INF/views/index.jsp");
    modelAndView.addObject("name","lisi");
    return modelAndView;
}

2) Write back data

  • Return the string directly: In the basic stage of the Web, the client accesses the server. If you want to directly write back the string as the response body, you only need to use response.getWriter().print("hello world"), then in the Controller What if I want to write back a string directly?

① The response object injected by the SpringMVC framework uses response.getWriter().print("hello world") to write back the data. At this time, no view jump is required, and the return value of the business method is void.

@RequestMapping("/quick4")
public void quickMethod4(HttpServletResponse response) throws IOException {
    response.getWriter().print("hello world");
}
② Return the string that needs to be written back directly, but at this time, you need to inform the SpringMVC framework through the @ResponseBody annotation. The string returned by the method is not a jump, but is returned directly in the http response body.
@RequestMapping("/quick5")
@ResponseBody
public String quickMethod5() throws IOException {
    return "hello springMVC!!!"; 
}

In development, complex java objects are often converted into strings in json format . We can use the json conversion tool jackson learned in the web stage to convert,

        1. Import jackson coordinates in pom.xml.

<!--jackson-->
<dependency>
 <groupId>com.fasterxml.jackson.core</groupId>
 <artifactId>jackson-core</artifactId>
 <version>2.9.0</version>
</dependency>
<dependency>
 <groupId>com.fasterxml.jackson.core</groupId>
 <artifactId>jackson-databind</artifactId>
 <version>2.9.0</version>
</dependency> 
<dependency>
 <groupId>com.fasterxml.jackson.core</groupId>
 <artifactId>jackson-annotations</artifactId>
 <version>2.9.0</version>
</dependency>

        2. Convert the json format string through jackson and write the string back.  

@RequestMapping("/quick7")
@ResponseBody
public String quickMethod7() throws IOException {
    User user = new User();
    user.setUsername("zhangsan");
    user.setAge(18);
    ObjectMapper objectMapper = new ObjectMapper();
    String s = objectMapper.writeValueAsString(user);
    return s;
}
  • return object or collection

 Use SpringMVC to help us convert objects or collections to json strings and write them back, configure message conversion parameters for the processor adapter, and specify the use of jackson to convert objects or collections. Therefore, the following configuration needs to be done 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"></bean>
     </list>
 </property>
</bean>

Return the object or collection directly in the method

@RequestMapping("/quick8")
@ResponseBody
public User quickMethod8() throws IOException {
    User user = new User();
    user.setUsername("zhangsan");
    user.setAge(18);
    return user;
}

3) Configure annotation driver

Adding @ResponseBody to the method can return a string in json format, but this configuration is more troublesome and the configuration code is more. Therefore, we can use the annotation driver of mvc to replace the above configuration.
Among the various components of SpringMVC, the processor mapper , the processor adapter , and the view resolver are called the three components of SpringMVC.
Using <mvc:annotation-driven> to automatically load RequestMappingHandlerMapping (handling mapper) and RequestMappingHandlerAdapter (handling adapter) can be used in Spring-xml.xml configuration file
<mvc:annotation-driven> overrides the configuration of annotation processors and adapters.
At the same time, the default bottom layer of <mvc:annotation-driven> will integrate jackson to convert the json format string of the object or collection.
<!--在spring-mvc.xml中配置mvc的注解驱动--> 
<mvc:annotation-driven/>

4) Knowledge points

SpringMVC's data response method
1) page jump
        return string directly
        Returned by ModelAndView object
2) Write back data
        return string directly
        return object or collection

Guess you like

Origin blog.csdn.net/qq_52360069/article/details/123777280