SpringMVC-数据响应

 

目录

页面跳转

1.返回字符串形式

2.ModeAndVIew形式

回写数据

1.直接返回字符串

2.json返回(返回对象)


页面跳转

1.返回字符串形式

直接返回字符串:此种方式会将返回的字符串与视图解析器的前后缀拼接后跳转。

 @RequestMapping(value="/quick",method = RequestMethod.GET)
    public String save(){
        System.out.println("Controller save running....");
        return "success";
    }
  <bean id="InternalResourceView class=" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

转发: forward: /wEB-INE/views/ index.jsp

重定向: redirect : / index.jsp

2.ModeAndVIew形式

方式1:

@RequestMapping(value="/quick2")
    public ModelAndView save2(){
        ModelAndView modelAndView = new ModelAndView();
        //设置模型数据
        modelAndView.addObject("username","itPig");
        //设置视图名称
        modelAndView.setViewName("success");
        return modelAndView;
    }

方式2:

@RequestMapping(value="/quick3")
    public ModelAndView save3(ModelAndView modelAndView){
        //设置模型数据
        modelAndView.addObject("username","itPig1");
        //设置视图名称
        modelAndView.setViewName("success");
        return modelAndView;
    }

方式3:

  @RequestMapping(value="/quick4")
    public String save4(Model model){
        //设置模型数据
        model.addAttribute("username","真好玩");
        return "success";
    }

回写数据

1.直接返回字符串

web阶段需要用到response.getWriter0.print,而在mvc会更简单

通过SpringMVC框架注入的response对象,使用response.getWriter(.print(“hello world”)回写数据,此时不需要视图跳转,业务方法返回值为void。

@RequestMapping(value="/quick5")
    public void save5(HttpServletResponse response) throws IOException {
        response.getWriter().println("你真牛");
    }
 @RequestMapping(value="/quick6")
    @ResponseBody //告诉方法不进行跳转,直接进行数据响应
    public String save6() throws IOException {
        return "hello hhh";
    }

2.json返回(返回对象)

方式一:使用js转换工具将对象转换为json格式字符串

pom.xml导入

  <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>
  @RequestMapping(value="/quick7")
    @ResponseBody //告诉方法不进行跳转,直接进行数据响应
    public String save7() throws IOException {
        UserName userName=new UserName(19,"zwq");
        //使用js转换工具将对象转换为json格式字符串
        ObjectMapper objectMapper=new ObjectMapper();
        String json = objectMapper.writeValueAsString(userName);
        return json;
    }

方式2:

使用mvc配置处理器映射器来进行返回

配置

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
            </list>
        </property>
    </bean>
 @RequestMapping(value="/quick8")
    @ResponseBody //告诉方法不进行跳转,直接进行数据响应
    public UserName save8() throws IOException {
        UserName userName=new UserName();
        userName.setAge(19);
        userName.setUsername("aaa");
        //使用js转换工具将对象转换为json格式字符串
        return userName;
    }

方式3:

通过mvc的注解将配置给简略化

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                            http://www.springframework.org/schema/mvc  http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven></mvc:annotation-driven>
 @RequestMapping(value="/quick8")
    @ResponseBody //告诉方法不进行跳转,直接进行数据响应
    public UserName save8() throws IOException {
        UserName userName=new UserName();
        userName.setAge(19);
        userName.setUsername("aaa");
        //使用js转换工具将对象转换为json格式字符串
        return userName;
    }

若有收获,就点个赞吧

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值