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>
<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>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM