【重温SSM框架系列】7 - SpringMVC数据处理(数据响应与获取请求数据)

x33g5p2x  于2022-03-19 转载在 Spring  
字(3.8k)|赞(0)|评价(0)|浏览(413)

大家好,我是【1+1=王】, 热爱java的计算机(人工智能)渣硕研究生在读。
如果你也对java、人工智能等技术感兴趣,欢迎关注,抱团交流进大厂!!!
Good better best, never let it rest, until good is better, and better best.

近期会重新温习一下SSM的相关知识,相应的博客会更新至专栏【SSM框架】中,欢迎大家关注!
SSM专栏:https://blog.csdn.net/weixin_43598687/category_11652306.html

数据响应

SpringMVC中的数据响应方式主要有页面跳转和回写数据两种形式。

通过返回字符串实现页面跳转

首先,在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>

并且,我们后面所有的jsp页面都放在views路径下。

使用字符串返回的方式实现页面跳转,会将返回的字符串与视图解析器的前后缀拼接后跳转。


测试:

  1. 在views下创建success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>string</title>
</head>
<body>
通过字符串跳转成功
</body>
</html>
  1. 启动Tomcat,访问http://localhost:8080/test,跳转成功。

通过返回ModelAndView对象实现页面跳转

可以先创建一个ModelAndView对象,然后向ModelAndView对象注入jsp页面名称。

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

        return modelAndView;
    }

测试:

  1. 在views下创建success2.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>string</title>
</head>
<body>
通过modelAndView跳转成功
</body>
</html>
  1. 启动Tomcat,访问http://localhost:8080/test2,跳转成功。

除了上面在方法内容创建ModelAndView对象这种方式外,Spring还提供了另外一种ModelAndView跳转的方式。

还可以通过在方法中传输一个ModelAndView参数,然后在方法内部注入jsp页面名称。

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

        return modelAndView;
    }

回写字符串类型数据

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

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

方式2:@ResponseBody注解
通过@ResponseBody注解告知SpringMVC框架,方法返回的字符串不是跳转是直接在http响应体中返回。

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

回写对象或集合类型数据

回写对象或集合类型数据时,需要通过SpringMVC帮助我们对对象或集合进行json字符串的转换并回写,为处理器适配器配置消息转换参数,指定使用jackson进行对象或集合的转换。

  1. 引入Jackson依赖包
<!--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. 配置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测试
@RequestMapping("/test6")
    @ResponseBody
    public User test6(){
        User user = new User();
        user.setId(1000);
        user.setUsername("testName");
        user.setPassword("testPassword");

        return user;
    }

上面配置数据格式转换的时候是比较麻烦的,我们可以使用以下配置代替,可以大大简化配置。

<!--mvc的注解驱动-->
<mvc:annotation-driven/>

使用mvc:annotation-driven自动加载RequestMappingHandlerMapping(处理映射器)和
RequestMappingHandlerAdapter(处理适配器),可用在Spring-xml.xml配置文件中使用
mvc:annotation-driven替代注解处理器和适配器的配置。
同时,使用mvc:annotation-driven默认底层就会集成jackson进行对象或集合的json格式字符串的转换。

获取请求数据

SSM专栏:https://blog.csdn.net/weixin_43598687/category_11652306.html

相关文章