Home  >  Article  >  Java  >  Java SpringMVC data response example analysis

Java SpringMVC data response example analysis

WBOY
WBOYforward
2023-05-21 12:16:06721browse

1) Page jump

Return the string directly: This method will concatenate the returned string with the prefix and suffix of the view parser and then jump.

Java SpringMVC data response example analysis

Return the string with the prefix:

Forward: forward:/WEB-INF/views/index.jsp

Heavy Orientation: redirect:/index.jsp

Return through 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 to send it to the Controller? What about storing data in the request domain?

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

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

② Set through 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 string directly: In the basic stage of Web, the client accesses the server. If you want to write back the string directly and return it as the response body, you only need to use response .getWriter().print("hello world") is enough. So what if you want to write back a string directly in the Controller?

① Use response.getWriter().print("hello world") to write back data through the response object injected by the SpringMVC framework. 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, it is often necessary to convert complex java objects into json format strings. We can use the json conversion tool jackson that we have learned in the web stage to convert,

1. In pom. Import jackson coordinates into 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 json format string through jackson and write back the string.

@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 objects or collections

Use SpringMVC to help us convert json strings and write back objects or collections, configure message conversion parameters for the processor adapter, and specify the use of jackson for objects or collections Collection conversion, so 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 objects or collections 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 requires a lot of configuration code. Therefore, we can use the mvc annotation driver instead of the above configuration.

Among the various components of SpringMVC, the processor mapper, processor adapter, and view resolver are called the three major components of SpringMVC.

Use to automatically load RequestMappingHandlerMapping (processing mapper) and RequestMappingHandlerAdapter (processing adapter), which can be used in the Spring-xml.xml configuration file alternative annotations Processor and adapter configuration.

Also use By default, the underlying layer will integrate jackson to convert json format strings of objects or collections.

<!--在spring-mvc.xml中配置mvc的注解驱动--> 
<mvc:annotation-driven/>

4) Knowledge points

Data response method of SpringMVC

1) Page jump

  • ##Return string directly

  • Return through the ModelAndView object

2) Write back data                                                                                                                 

    #Return an object or collection

The above is the detailed content of Java SpringMVC data response example analysis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete