Springmvc如何实现向前台传递数据

所属分类: 软件编程 / java 阅读数: 85
收藏 0 赞 0 分享

1) 在springmvc方法的形参中定义Map,Model,ModelMap,并在方法中通过这三个对象进行值的传递;

①其中Map和ModelMap使用方式是一致的;

@RequestMapping("/detail")
  public String detail(Integer id,
             //ModelMap modelMap
             Map modelMap
               ){
    HashMap<String,String> conditions=new HashMap<>();
    conditions.put("sal","88888888");
    conditions.put("age","35");
    //todo 去数据库查询用户信息
    System.out.println("查询id为"+id+"的用户记录");
    User user=new User(id,"詹姆斯",18,"男","美国克利夫兰",
              new Role("小前锋",23),
              conditions,
              Arrays.asList("打篮球","打游戏"));
    //通过modelMap或map向前台传值==>request.setAttribute(key,value)
    modelMap.put("user",user);
    return "detail.jsp";
  }

②Model只是通过addAttribute进行传值;

@RequestMapping("/detail")
  public String detail(Integer id,
             Model model){
    HashMap<String,String> conditions=new HashMap<>();
    conditions.put("sal","88888888");
    conditions.put("age","35");
    //todo 去数据库查询用户信息
    System.out.println("查询id为"+id+"的用户记录");
    User user=new User(id,"詹姆斯",18,"男","美国克利夫兰",
              new Role("小前锋",23),
              conditions,
              Arrays.asList("打篮球","打游戏"));
    //通过Model对象传递数据
    model.addAttribute("user",user);
    return "detail.jsp";
  }

2) 定义方法的返回值类型为ModelAndView,在方法中创建ModelAndView 并指定跳转的页面和传递的数据,最后返回ModelAndView对象;

3) 通过注解的方式 @ModelAttribute;

4) 在方法参数中定义Request或session对象,通过其对应的API;

下面2),3),4)的情况都在下面的代码内;

//演示通过ModelAndView向页面传值
//@ModelAttribute:注解将对象传递到request域中 1)加在方法参数上,将对象传递到request域中,或向request域中取值
//                     2)加在方法上,将方法的返回值放入request域中
  @RequestMapping("/detail2")
  public ModelAndView detail2(Integer id, @ModelAttribute("username") String username,
                HttpServletRequest request,
                HttpSession session,
                HttpServletResponse response
  ){

    request.setAttribute("requestTest","请求域数据");
    session.setAttribute("sessionTest","session域数据");

    HashMap<String,String> conditions=new HashMap<>();
    conditions.put("sal","88888888");
    conditions.put("age","35");
    //todo 去数据库查询用户信息
    System.out.println("查询id为"+id+"的用户记录");
    User user=new User(id,"詹姆斯",18,"男","美国克利夫兰",
        new Role("小前锋",23),
        conditions,
        Arrays.asList("打篮球","打游戏"));
    //通过ModelAndView设置跳转的页面和值
    ModelAndView modelAndView=new ModelAndView();
    //向页面传值
    modelAndView.addObject("user",user);
    //指定跳转的页面 以/开头,则直接到资源根目录下找(即webapp下)
    //       不以/开头,跟在RequestMapping最后一个/后面
    modelAndView.setViewName("detail.jsp");
    return modelAndView;
  }
  //将方法返回值放入request域中
  @ModelAttribute(name = "modelAttributeTest")
  public String test(){
    return "我是@ModelAttribute的测试";
  }

detail.jsp中代码如下:

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
  <title>用户详情页面</title>
</head>
<body>
<h1>用户详细信息</h1>
<table>
  <tr>
    <td>用户名</td>
    <td>${user.name}</td>
    <td>年龄</td>
    <td>${user.age}</td>
  </tr>
  <tr>
    <td>性别</td>
    <td>${user.sex}</td>
    <td>地址</td>
    <td>${user.addr}</td>
  </tr>
  <tr>
    <td>角色ID</td>
    <td>${user.role.id}</td>
    <td>角色名</td>
    <td>${user.role.name}</td>
  </tr>
  <tr>
    <td>条件1</td>
    <td>${user.conditions.sal}</td>
    <td>条件2</td>
    <td>${user.conditions.age}</td>
  </tr>
  <tr>
    <td>爱好</td>
    <td>${user.hobbies}</td>
  </tr>
</table>
获取@ModelAttribute设置的值:${username}<br/>
获取@ModelAttribute设置的值2:${modelAttributeTest}<br/>
获取request设置的值3:${requestTest}<br/>
获取session设置的值4:${sessionTest}
</body>
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

JavaWeb项目部署到服务器详细步骤详解

这篇文章主要介绍了JavaWeb项目如何部署到服务器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA基于支付宝小程序搭建springboot项目的详细步骤

这篇文章主要介绍了IDEA基于支付宝小程序搭建springboot项目的详细步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解SpringBoot应用服务启动与安全终止

这篇文章主要介绍了SpringBoot应用服务启动与安全终止,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Boot启动及退出加载项的方法

这篇文章主要介绍了Spring Boot启动及退出加载项的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Data Jpa 自动生成表结构的方法示例

这篇文章主要介绍了Spring Data Jpa 自动生成表结构的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA中osgi的开发应用指南详解

这篇文章主要介绍了IDEA中osgi的开发应用指南详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解用maven将dubbo工程打成jar包运行

这篇文章主要介绍了详解用maven将dubbo工程打成jar包运行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解Java合并数组的两种实现方式

这篇文章主要介绍了Java合并数组的两种实现方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

使用Jenkins Pipeline自动化构建发布Java项目的方法

这篇文章主要介绍了使用Jenkins Pipeline自动化构建发布Java项目的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

使用Maven配置Spring的方法步骤

这篇文章主要介绍了使用Maven配置Spring的方法步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多