SpringMVC_响应数据和结果视图

5. 响应数据和结果视图

前面演示的方法都是返回的字符串,用于视图解析器拼接响应视图的路径,

controller中方法的返回类型如下:

  1. String
  2. void(不常用)
  3. ModelAndView
  4. json

5.1. 字符串

controller 方法返回字符串也称为逻辑视图名,通过视图解析器解析为物理视图地址。

物理视图地址 = 视图解析器前缀 + 逻辑视图名 + 视图解析器后缀

<!--视图解析器-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <!--前缀-->
    <property name="prefix" value="/WEB-INF/pages/"/>
    <!--后缀-->
    <property name="suffix" value=".jsp"/>
    <property name="contentType" value="text/html;charset=UTF-8"/>
</bean>
@RequestMapping("/saveAccount")
public String saveAccount(User user){
    System.out.println(user);
    return "success";
}

最终返回的物理视图地址:/WEB-INF/pages/ + success + .jsp = /WEB-INF/pages/success.jsp

5.2. void(了解)

很少用,默认会查找请求路径结尾的jsp资料

  • 请求路径:http://localhost:8080/user/void.do
  • controller
@RequestMapping("/void")
public void returnVoid(){
    System.out.println("返回值void...");
}

在这里插入图片描述

5.3. ModelAndView

ModelAndView 是 SpringMVC 为我们提供的一个对象,该对象也可以用作控制器方法的返回值。

该对象中有两个方法:

  • (String attributeName, @Nullable Object attributeValue)

    保存键值对数据到ModelMap中,可以传递给前端页面展示。

  • setViewName(@Nullable String viewName)

    设置逻辑视图名称,等同于return “success”;

/**
* Add an attribute to the model.
* @param attributeName name of the object to add to the model (never {@code null})
* @param attributeValue object to add to the model (can be {@code null})
* @see ModelMap#addAttribute(String, Object)
* @see #getModelMap()
*/
public ModelAndView addObject(String attributeName, @Nullable Object attributeValue) {
    getModelMap().addAttribute(attributeName, attributeValue);
    return this;
}

/**
* Set a view name for this ModelAndView, to be resolved by the
* DispatcherServlet via a ViewResolver. Will override any
* pre-existing view name or View.
*/
public void setViewName(@Nullable String viewName) {
    this.view = viewName;
}
  • 示例
  • controller
@RequestMapping(value = "/model/view",method = RequestMethod.GET)
public ModelAndView modelAndView(ModelAndView modelAndView){
    System.out.println("测试 modelAndView...");
    User user = new User();
    user.setName("tom");
    user.setAge(22);
    //保存数据
    modelAndView.addObject("clazz","大数据");
    modelAndView.addObject("user",user);
    //设置视图名称
    modelAndView.setViewName("success");
    //返回ModelAndView对象
    return modelAndView;
}
  • jsp
<body>
    <h2>sccess</h2>

    class: ${clazz}
    username: ${user.name}
    age: ${user.age}

</body>
  • 页面效果:
    在这里插入图片描述

5.4. json

使用@ResponseBody注解

5.4.1. @ResponseBody作用

该注解用于将 Controller 的方法返回的对象,通过 HttpMessageConverter 接口转换为指定格式的

数据如:json、xml 等,默认转为json格式,再通过 Response 响应给客户端 。

使用json格式的数据进行交互,也是目前前后端分离开发最主要的方式。

  • Springmvc 默认用 MappingJacksonHttpMessageConverter 对 json 数据进行转换,需要加入

    jackson 的包(如果已经添加过,不要重复添加)

<!-- 版本管理 -->
<jackson-version>2.11.0</jackson-version>


<!-- 依赖jar -->
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>${jackson-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>${jackson-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>${jackson-version}</version>
</dependency>
5.4.2. 示例

将pojo对象转为json格式响应给客户端

  • controller
@RequestMapping(value = "/testResponseBody",method = RequestMethod.POST)
public @ResponseBody User testResponseBody(){
    System.out.println("使用 @ResponseBody响应json数据 ...");

    User user = new User();
    user.setId(1001);
    user.setMoney(100.1F);
    user.setName("tom");
    user.setAge(22);

    return user;
}
  • postman测试
    在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_44509920/article/details/107700360