[Review SSM Framework Series] 7 - SpringMVC Data Processing (Data Response)

Get into the habit of writing together! This is the 7th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

data response

The data response methods in SpringMVC mainly include page jump and write-back data.

Page jump by returning a string

First, configure the prefix and suffix of the view resolver in spring-mvc.xml.

<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".jsp"/>
</bean>
复制代码

And, all the jsp pages behind us are placed under the views path.

Using the method of string return to achieve page jump, the returned string will be spliced ​​with the suffix of the view parser and then jumped. insert image description heretest:

  1. Create success.jsp under views
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>string</title>
</head>
<body>
通过字符串跳转成功
</body>
</html>

复制代码
  1. Start Tomcat, visit http://localhost:8080/test, and the jump is successful.

insert image description here

Realize page jump by returning ModelAndView object

You can create a ModelAndView object first, and then inject the jsp page name into the ModelAndView object.

    @RequestMapping("/test2")
    public ModelAndView test2(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("success2");

        return modelAndView;
    }
复制代码

test:

  1. Create success2.jsp under views
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>string</title>
</head>
<body>
通过modelAndView跳转成功
</body>
</html>

复制代码
  1. Start Tomcat, visit http://localhost:8080/test2, and the jump is successful.

insert image description hereIn addition to the above method of creating ModelAndView objects in the method content, Spring also provides another way to jump to ModelAndView.

You can also inject a jsp page name inside the method by passing a ModelAndView parameter in the method.

    @RequestMapping("/test3")
    public ModelAndView test3(ModelAndView modelAndView){
        modelAndView.addObject("msg","modelAndView中传入的message");
        modelAndView.setViewName("success3");

        return modelAndView;
    }
复制代码

Write back string type data

方式1:response.getWriter().println()

    @RequestMapping("/test4")
    public void test4(HttpServletResponse response) throws IOException {
        response.getWriter().println("re Write String data");
    }
复制代码

insert image description here Method 2: @ResponseBody annotation The SpringMVC framework is informed by the @ResponseBody annotation that the string returned by the method is not a jump but is returned directly in the http response body.

    @RequestMapping("/test5")
    @ResponseBody
    public String test5(){
        return "re Write String data";
    }
复制代码

Write back object or collection type data

When writing back object or collection type data, we need to use SpringMVC to help us convert and write back the json string of the object or collection, configure message conversion parameters for the processor adapter, and specify the use of jackson for object or collection conversion.

  1. Introduce the Jackson dependency package
<!--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>
复制代码
  1. Place 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>
复制代码
  1. controller test
    @RequestMapping("/test6")
    @ResponseBody
    public User test6(){
        User user = new User();
        user.setId(1000);
        user.setUsername("testName");
        user.setPassword("testPassword");

        return user;
    }
复制代码

insert image description hereThe above configuration data format conversion is more troublesome, we can use the following configuration instead, which can greatly simplify the configuration.

<!--mvc的注解驱动-->
<mvc:annotation-driven/>
复制代码

Use mvc:annotation-driven to automatically load RequestMappingHandlerMapping (handling mapper) and RequestMappingHandlerAdapter (handling adapter), you can use mvc:annotation-driven in the Spring-xml.xml configuration file to replace the configuration of annotation processors and adapters. At the same time, using mvc:annotation-driven default bottom layer will integrate jackson to convert the json format string of objects or collections.

Guess you like

Origin juejin.im/post/7085147520896073764