SpringMVC——数据响应

简介

  SpringMVC数据响应分为:页面跳转和回写数据

 

页面跳转

  1.直接在方法映射上返回字符串,进行页面跳转

    @RequestMapping(value = "save")
    public String save(){
        System.out.println("find!");
        return "/jsp/success.jsp";
    }

  

  2, 通过ModelAndView对象,进行页面跳转

    形参ModelAndView,SpringMVC会注入这个参数。

                modelAndView.setViewName(String viewName):设置跳转视图相对工程位置

        modelAndView.setObject(String key ,Object obj):设置域数据

      返回ModelAndView即可使用

    @RequestMapping(value = "save1")
    public ModelAndView save(ModelAndView modelAndView){
        modelAndView.setViewName("/jsp/success.jsp");
        modelAndView.addObject("userName","zs,zs");
        return modelAndView;
    }

 

回写数据

  1.@ResponseBody:

    作用:让字符串作为响应返回。(不进行页面跳转,直接返回数据)

@RequestMapping("save3")
    @ResponseBody
    public String save3(){
        return "Hello!";
    }

    应用场景:直接回复json字符串

  

  2.jackson,对象转json库

  坐标:

<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.jwebmp.jpms.jackson.core</groupId>
  <artifactId>jackson-annotations</artifactId>
  <version>0.67.0.16</version>
</dependency>

  ObjectMapping 

    方法 

     String writeValueAsString(Object obj)

      参数:

        Object obj:对象

      返回值 String

      返回值意义:将对象转为JSON格式字符粗

      作用:将对象转为JSON格式字符粗

  使用

    @RequestMapping("save4")
    @ResponseBody
    public String save4() throws JsonProcessingException {
        User user = new User();
        user.setUserName("cw");
        user.setAge(21);
        //json转换工具 jackson
        ObjectMapper objectMapper = new ObjectMapper();
        String str = objectMapper.writeValueAsString(user);
        return str;
    }

 

其他

  使用springMVC内置对象转json的工具

    RequestMappingHandlerAdapter:处理器适配器

      SpringMVC.xml配置这个对象,对这个对象的方法进行设置。然后返回的对象都是json格式

 <!-- 配置处理器映射器    -->
    <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>

或者使用:

  使用mvc命名空间上的标签,它会自动结合jackson

  

xmlns:mvc="http://www.springframework.org/schema/mvc"
http://www.springframework.org/schema/mvc  http://www.springframework.org/schema/mvc/spring-mvc.xsd

    <mvc:annotation-driven/>

 

 使用

@RequestMapping("save4")
    @ResponseBody
    public String save4() throws JsonProcessingException {
        User user = new User();
        user.setUserName("cw");
        user.setAge(21);
        //json转换工具 jackson
        ObjectMapper objectMapper = new ObjectMapper();
        String str = objectMapper.writeValueAsString(user);
        return str;
    }

 

   也可以在参数内传HttpServletRequest request。SpringMVC一样会注入这个对象。

@RequestMapping("save2")
    public String save2(HttpServletRequest request){
        request.setAttribute("userName","cw,cw");
        return "/jsp/success.jsp";
    }

 

posted @ 2021-11-20 18:21  remix_alone  阅读(63)  评论(0)    收藏  举报