Spring(四): SpringMVC-数据响应,ModelAndView,Json的使用

一 页面跳转

1.直接返回字符串:此种方法会和视图解析器的前后缀拼接跳转

 @RequestMapping("/quick")
    public String save(){
        System.out.println("Controller save running...");
        return "redirect:/success.jsp";
    }

2.通过ModelAndView

(1)直接返回ModelAndView

手动新建ModelAndView,然后对model和view分别设置

    @RequestMapping("/quick2")
    public ModelAndView save2(){
//       model:模型 封装数据
//       view:视图 展示数据
        ModelAndView modelAndView = new ModelAndView();
//       设置模型数据
        modelAndView.addObject("username","itcast");
//       设置视图名称
        modelAndView.setViewName("index.jsp");
        return modelAndView;
    }

(2)用SpringMVC创建ModelAndView,后对model和view分别设置

    @RequestMapping("/quick3")
    public ModelAndView save3(ModelAndView modelAndView){

//       设置模型数据
        modelAndView.addObject("username","itheima");
//       设置视图名称
        modelAndView.setViewName("index.jsp");
        return modelAndView;
    }

(3)用SpringMVC创建model,后用返回viewname

    @RequestMapping("/quick4")
    public String save4(Model model){

        model.addAttribute("username","itheima");
        return "index.jsp";
    }

(4)用request,将model放入域中,也可以实现页面跳转。传入形参,SpringMVC直接返回实参

    @RequestMapping("/quick5")
    public String save5(HttpServletRequest request){

        request.setAttribute("username","it");
        return "index.jsp";
    }

 (5)对应的index.jsp,如下

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h1>hello world!</h1>
    <h1>${username}</h1>
</body>
</html>

二 回写数据

1.直接返回字符串 

(1)通过SpringMVC注入response对象,使用response.getWriter().print();回写数据,注意返回值为void

@RequestMapping("/quick6")
    public void save6(HttpServletResponse response) throws IOException {

        response.getWriter().print("hello itcast");
    }

(2)将需要回写的字符串直接返回,但此时需要通过@ResponseBody注解告知SpringMVC框架,方法返回的字符串不是跳转而是直接在http响应体中返回(直接返回视图重点

    @RequestMapping("/quick7")
    @ResponseBody//告知SpringMVC不进行视图跳转,直接进行数据响应
    public String save7()  {
        return "hello world";
    }

返回结果为: 

 (3)可返回Json字符串

可以直接return json字符串,也可以使用接送的转换工具将对象转换成json格式字符串再返回

下面我们只演示后一种情况

首先导包,注意版本要求(2.9以上)

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

而后,创建对象,进行赋值,再将其转换成json格式字符串

 

2.返回对象或集合

(1)我们可以通过SpringMVC帮助我们对对象集合进行json字符串的转换并回写

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

而后,直接返回对象就可以,即可返回json格式字符串

    @RequestMapping("/quick10")
    @ResponseBody
    public User save10()  {

        User user = new User();
        user.setAge(18);
        user.setName("xiaoming");
        return user;
    }

(2)我们可以利用MVC的注解驱动代码代替上述配置

 首先,我们加载mvc驱动

 而后,在spring-mvc.xml下进行MVC注解驱动

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

最后,运行程序,就可得到和(1)同样的效果。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值