溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Springmvc實現向前臺傳遞數據的方法

發布時間:2020-07-02 09:21:52 來源:億速云 閱讀:211 作者:清晨 欄目:開發技術

不懂Springmvc實現向前臺傳遞數據的方法?其實想解決這個問題也不難,下面讓小編帶著大家一起學習怎么去解決,希望大家閱讀完這篇文章后大所收獲。

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>
<h2>用戶詳細信息</h2>
<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>

感謝你能夠認真閱讀完這篇文章,希望小編分享Springmvc實現向前臺傳遞數據的方法內容對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,遇到問題就找億速云,詳細的解決方法等著你來學習!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女