SpringMVC -SpringMVC数据响应详解与案例步骤开发

SpringMVC的数据响应方式

请添加图片描述

页面跳转

返回字符串形式

请添加图片描述

返回ModelAndView对象

编写UserController.java
package com.taotao.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 * create by 刘鸿涛
 * 2022/4/16 18:19
 */
@SuppressWarnings({"all"})
@RequestMapping("/user")
@Controller
public class UserController {

    @RequestMapping(value = "/quick2")
    public ModelAndView save2(){
        /*
            model:模型 作用封装数据
            View:视图 作用展示数据
         */
        ModelAndView modelAndView = new ModelAndView();
        //设置视图名称
        modelAndView.setViewName("success");
        return modelAndView;
    }


    @RequestMapping(value = "/quick",method = RequestMethod.GET,params = {"username"})
    public String save(){
        System.out.println("Controller save running...");
        //跳转视图
        return "success";
    }
}

成功测试

请添加图片描述

设置模型数据
package com.taotao.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 * create by 刘鸿涛
 * 2022/4/16 18:19
 */
@SuppressWarnings({"all"})
@RequestMapping("/user")
@Controller
public class UserController {

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


    @RequestMapping(value = "/quick",method = RequestMethod.GET,params = {"username"})
    public String save(){
        System.out.println("Controller save running...");
        //跳转视图
        return "success";
    }
}
编写success.jsp
<%--
  Created by IntelliJ IDEA.
  User: guigui
  Date: 2022/4/16
  Time: 18:21
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page isELIgnored="false" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>success!! ${username}</h1>
</body>
</html>

注意jsp识别代码
<%@page isELIgnored="false" %>
成功测试

请添加图片描述

返回ModelAndView对象2

编写UserControlle
package com.taotao.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 * create by 刘鸿涛
 * 2022/4/16 18:19
 */
@SuppressWarnings({"all"})
@RequestMapping("/user")
@Controller
public class UserController {

    @RequestMapping(value = "/quick3")
    public ModelAndView save3(ModelAndView modelAndView){
        modelAndView.addObject("username","liuhongtao");
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping(value = "/quick2")
    public ModelAndView save2(){
        /*
            model:模型 作用封装数据
            View:视图 作用展示数据
         */
        ModelAndView modelAndView = new ModelAndView();
        //设置模型数据
        modelAndView.addObject("username","taotao");
        //设置视图名称
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping(value = "/quick",method = RequestMethod.GET,params = {"username"})
    public String save(){
        System.out.println("Controller save running...");
        //跳转视图
        return "success";
    }
}

成功测试

请添加图片描述

返回ModelAndView对象3

编写UserController
package com.taotao.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 * create by 刘鸿涛
 * 2022/4/16 18:19
 */
@SuppressWarnings({"all"})
@RequestMapping("/user")
@Controller
public class UserController {
    @RequestMapping(value = "/quick4")
    public String save4( Model model){
        model.addAttribute("username","刘鸿涛");
        return "success";
    }

    @RequestMapping(value = "/quick3")
    public ModelAndView save3(ModelAndView modelAndView){
        modelAndView.addObject("username","liuhongtao");
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping(value = "/quick2")
    public ModelAndView save2(){
        /*
            model:模型 作用封装数据
            View:视图 作用展示数据
         */
        ModelAndView modelAndView = new ModelAndView();
        //设置模型数据
        modelAndView.addObject("username","taotao");
        //设置视图名称
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping(value = "/quick",method = RequestMethod.GET,params = {"username"})
    public String save(){
        System.out.println("Controller save running...");
        //跳转视图
        return "success";
    }
}

成功测试

请添加图片描述

返回ModelAndView对象4

编写UserController
package com.taotao.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

/**
 * create by 刘鸿涛
 * 2022/4/16 18:19
 */
@SuppressWarnings({"all"})
@RequestMapping("/user")
@Controller
public class UserController {
    @RequestMapping(value = "/quick5")
    public String save5(HttpServletRequest request){
        request.setAttribute("username","刘鸿涛");
        return "success";
    }

    @RequestMapping(value = "/quick4")
    public String save4( Model model){
        model.addAttribute("username","刘鸿涛");
        return "success";
    }

    @RequestMapping(value = "/quick3")
    public ModelAndView save3(ModelAndView modelAndView){
        modelAndView.addObject("username","liuhongtao");
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping(value = "/quick2")
    public ModelAndView save2(){
        /*
            model:模型 作用封装数据
            View:视图 作用展示数据
         */
        ModelAndView modelAndView = new ModelAndView();
        //设置模型数据
        modelAndView.addObject("username","taotao");
        //设置视图名称
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping(value = "/quick",method = RequestMethod.GET,params = {"username"})
    public String save(){
        System.out.println("Controller save running...");
        //跳转视图
        return "success";
    }
}

成功测试

请添加图片描述

回写数据

直接返回字符串

请添加图片描述

编写UserController
package com.taotao.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * create by 刘鸿涛
 * 2022/4/16 18:19
 */
@SuppressWarnings({"all"})
@RequestMapping("/user")
@Controller
public class UserController {
    @RequestMapping(value = "/quick6")
    public void save6(HttpServletResponse response) throws IOException {
        response.getWriter().print("hell taotao");
    }
}

成功测试

请添加图片描述

直接返回字符串2

请添加图片描述

编写UserController
package com.taotao.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * create by 刘鸿涛
 * 2022/4/16 18:19
 */
@SuppressWarnings({"all"})
@RequestMapping("/user")
@Controller
public class UserController {
    @RequestMapping(value = "/quick7")
    @ResponseBody       //告知SpringMVC框架 不进行视图跳转 直接进行数据响应
    public String save7(HttpServletResponse response) throws IOException {
        return "hello taotao";
    }

    @RequestMapping(value = "/quick6")
    public void save6(HttpServletResponse response) throws IOException {
        response.getWriter().print("hell taotao");
    }
}

成功测试

请添加图片描述

直接返回json格式字符串

编写UserController
package com.taotao.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * create by 刘鸿涛
 * 2022/4/16 18:19
 */
@SuppressWarnings({"all"})
@RequestMapping("/user")
@Controller
public class UserController {
    @RequestMapping(value = "/quick8")
    @ResponseBody       //告知SpringMVC框架 不进行视图跳转 直接进行数据响应
    public String save8(HttpServletResponse response) throws IOException {
        return "{\"username\":\"taotao\",\"age\":18}";
    }

    @RequestMapping(value = "/quick7")
    @ResponseBody       //告知SpringMVC框架 不进行视图跳转 直接进行数据响应
    public String save7(HttpServletResponse response) throws IOException {
        return "hello taotao";
    }

    @RequestMapping(value = "/quick6")
    public void save6(HttpServletResponse response) throws IOException {
        response.getWriter().print("hell taotao");
    }
}

成功测试

请添加图片描述

直接返回json格式字符串(解耦)

编写user类
package com.taotao.domain;

/**
 * create by 刘鸿涛
 * 2022/4/17 21:38
 */
@SuppressWarnings({"all"})
public class User {
    private String username;
    private int age;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", age=" + age +
                '}';
    }
}

导入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.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.9.0</version>
    </dependency>
编写UserController
package com.taotao.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.taotao.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * create by 刘鸿涛
 * 2022/4/16 18:19
 */
@SuppressWarnings({"all"})
@RequestMapping("/user")
@Controller
public class UserController {
    @RequestMapping(value = "/quick9")
    @ResponseBody       //告知SpringMVC框架 不进行视图跳转 直接进行数据响应
    public String save9() throws IOException {
        User user = new User();
        user.setUsername("lisa");
        user.setAge(20);
        //使用json的转换工具将对象转换成json格式字符串在返回
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(user);
        return json;
    }

    @RequestMapping(value = "/quick8")
    @ResponseBody       //告知SpringMVC框架 不进行视图跳转 直接进行数据响应
    public String save8(HttpServletResponse response) throws IOException {

    }

    @RequestMapping(value = "/quick7")
    @ResponseBody       //告知SpringMVC框架 不进行视图跳转 直接进行数据响应
    public String save7(HttpServletResponse response) throws IOException {
        return "hello taotao";
    }

    @RequestMapping(value = "/quick6")
    public void save6(HttpServletResponse response) throws IOException {
        response.getWriter().print("hell taotao");
    }
}

成功测试

请添加图片描述

返回对象或集合

编写UserController
package com.taotao.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.taotao.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * create by 刘鸿涛
 * 2022/4/16 18:19
 */
@SuppressWarnings({"all"})
@RequestMapping("/user")
@Controller
public class UserController {
    @RequestMapping(value = "/quick10")
    @ResponseBody       //告知SpringMVC框架 不进行视图跳转 直接进行数据响应
    //期望SpringMVC自动将User转换成json格式的字符串
    public User save10() throws IOException {
        User user = new User();
        user.setUsername("lisa");
        user.setAge(20);
        return user;
    }

    @RequestMapping(value = "/quick9")
    @ResponseBody       //告知SpringMVC框架 不进行视图跳转 直接进行数据响应
    public String save9() throws IOException {
        User user = new User();
        user.setUsername("lisa");
        user.setAge(20);
        //使用json的转换工具将对象转换成json格式字符串在返回
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(user);
        return json;
    }

    @RequestMapping(value = "/quick8")
    @ResponseBody       //告知SpringMVC框架 不进行视图跳转 直接进行数据响应
    public String save8(HttpServletResponse response) throws IOException {
        return "{\"username\":\"taotao\",\"age\":18}";
    }

    @RequestMapping(value = "/quick7")
    @ResponseBody       //告知SpringMVC框架 不进行视图跳转 直接进行数据响应
    public String save7(HttpServletResponse response) throws IOException {
        return "hello taotao";
    }

    @RequestMapping(value = "/quick6")
    public void save6(HttpServletResponse response) throws IOException {
        response.getWriter().print("hell taotao");
    }
}

编写Spring-mvc.xml

配置处理器映射器

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">

<!--Controller的组件扫描-->
    <context:component-scan base-package="com.taotao">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

<!--    配置内部资源视图解释器-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--        /jsp/success.jsp-->
        <property name="prefix" value="/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

<!--    配置处理器映射器-->
    <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>
</beans>
成功测试

请添加图片描述

返回对象或集合(解耦)

请添加图片描述

编写Spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd">

<!--Controller的组件扫描-->
    <context:component-scan base-package="com.taotao">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

<!--    配置内部资源视图解释器-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--        /jsp/success.jsp-->
        <property name="prefix" value="/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

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

<!--    mvc的注解驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
成功测试

请添加图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鬼鬼骑士

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值