博客
关于我
SpringMVC系列--数据返回及页面跳转
阅读量:515 次
发布时间:2019-03-07

本文共 1796 字,大约阅读时间需要 5 分钟。

Spring MVC 中的返回类型与数据接收

一、返回String类型

在Spring MVC中,返回String类型的方法通常用于页面跳转或静态资源的返回。以下是常见的操作方式:

  • 直接返回页面字符串

    在控制器方法中,直接返回“success”字符串会自动定向到对应的页面。

    @RequestMapping("/testString")public String testString(Model model) {    System.out.println("testString方法执行了...");    User user = new User();    user.setUsername("jack");    user.setPassword("123456");    user.setAge(30);    model.addAttribute("user", user);    return "success";}
  • 页面跳转(Forward)

    使用 return "forward:/WEB-INF/pages/success.jsp" 可以实现页面的转发跳转。

  • 重定向(Redirect)

    使用 return "redirect:/index.jsp" 实现页面的重定向跳转。

  • 二、无返回值的接收(Void类型)

    在Spring MVC中,Void类型的方法通常用于无返回值的接收,常见的有请求转发和重定向。

  • 请求转发(Forward)

    使用 request.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(request, response); 进行请求转发。

  • 重定向(Redirect)

    使用 response.sendRedirect(request.getContextPath() + "/index.jsp"); 进行重定向。

  • 直接响应数据

    在Void类型的方法中,可以直接通过 response.getWriter().print("你好"); 输出响应数据。

  • 三、返回ModelAndView对象

    返回ModelAndView对象是一种常见的数据传输方式,适用于需要传递模型数据和视图名称的场景。

    @RequestMapping("/testModelAndView")public ModelAndView testModelAndView() {    ModelAndView mv = new ModelAndView();    System.out.println("testModelAndView方法执行了...");    User user = new User();    user.setUsername("jack");    user.setPassword("123456");    user.setAge(30);    mv.addObject("user", user);    mv.setViewName("success");    return mv;}

    四、接收异步请求数据

    在Spring MVC中,通过 @RequestBody 注解可以接收客户端发送的JSON数据,适用于异步请求的处理。

    @ResponseBody@RequestMapping("/testAjax")public User testAjax(@RequestBody User user) {    System.out.println("testAjax方法执行了...");    System.out.println(user);    user.setUsername("rose");    user.setAge(40);    return user;}

    注意事项

  • 静态资源过滤

    springmvc.xml中配置静态资源过滤,确保前端控制器DispatcherServlet正确拦截静态资源。

  • 字符编码设置

    在 Void类型的方法中,确保设置正确的字符编码,避免乱码问题。

  • 数据传输优化

    在接收异步请求时,确保数据格式的正确性,避免JSON解析错误。

  • 通过以上方法,可以灵活地在Spring MVC应用中处理不同类型的返回值和数据接收需求。

    转载地址:http://lhvjz.baihongyu.com/

    你可能感兴趣的文章
    NIFI大数据进阶_连接与关系_设置数据流负载均衡_设置背压_设置展现弯曲_介绍以及实际操作---大数据之Nifi工作笔记0027
    查看>>
    Nio ByteBuffer组件读写指针切换原理与常用方法
    查看>>
    NIO Selector实现原理
    查看>>
    nio 中channel和buffer的基本使用
    查看>>
    NISP一级,NISP二级报考说明,零基础入门到精通,收藏这篇就够了
    查看>>
    NI笔试——大数加法
    查看>>
    NLP 基于kashgari和BERT实现中文命名实体识别(NER)
    查看>>
    Nmap扫描教程之Nmap基础知识
    查看>>
    Nmap端口扫描工具Windows安装和命令大全(非常详细)零基础入门到精通,收藏这篇就够了
    查看>>
    NMAP网络扫描工具的安装与使用
    查看>>
    NN&DL4.1 Deep L-layer neural network简介
    查看>>
    NN&DL4.3 Getting your matrix dimensions right
    查看>>
    NN&DL4.8 What does this have to do with the brain?
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>