博客
关于我
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/

    你可能感兴趣的文章
    org.apache.dubbo.common.serialize.SerializationException: com.alibaba.fastjson2.JSONException: not s
    查看>>
    sqlserver学习笔记(三)—— 为数据库添加新的用户
    查看>>
    org.apache.http.conn.HttpHostConnectException: Connection to refused
    查看>>
    org.apache.ibatis.binding.BindingException: Invalid bound statement错误一例
    查看>>
    org.apache.ibatis.exceptions.PersistenceException:
    查看>>
    org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned
    查看>>
    org.apache.ibatis.type.TypeException: Could not resolve type alias 'xxxx'异常
    查看>>
    org.apache.poi.hssf.util.Region
    查看>>
    org.apache.xmlbeans.XmlOptions.setEntityExpansionLimit(I)Lorg/apache/xmlbeans/XmlOptions;
    查看>>
    org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /
    查看>>
    org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:processDebugManifest'
    查看>>
    org.hibernate.HibernateException: Unable to get the default Bean Validation factory
    查看>>
    org.hibernate.ObjectNotFoundException: No row with the given identifier exists:
    查看>>
    org.springframework.amqp.AmqpConnectException:java.net.ConnectException:Connection timed out:connect
    查看>>
    org.springframework.beans.factory.BeanDefinitionStoreException
    查看>>
    org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata
    查看>>
    org.springframework.boot:spring boot maven plugin丢失---SpringCloud Alibaba_若依微服务框架改造_--工作笔记012
    查看>>
    SQL-CLR 类型映射 (LINQ to SQL)
    查看>>
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    查看>>
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    查看>>