springmvc如何將對象類型的數據放入到model對象中

使用model來存放數據時,不能直接用,必須用他的實現類ModelMap

話不多說,直接整個代碼來看一下:

public class Account implements Serializable {

    private String username;
    private String password;
    private Double money;

    private User user;
	
	//getter/setter
	
	//tostring
}
public class User implements Serializable {

    private String username;
    private String password;
    
    //getter/setter
	
	//tostring
}
@Controller
@RequestMapping("/user")
public class SpringMVCHandler {

    @RequestMapping("/textPojo")
    public String testPojo(Account account){

        ModelMap modelMap = new ModelMap();

        System.out.println("pojo....");
        System.out.println(account);

        modelMap.addAttribute("account",account);
        return "success";
    }
}
 <form action="user/textPojo" method="post">
        用戶名:<input type="text" name="username"/><br/>
        密碼:<input type="text" name="password"/><br/>
        金額:<input type="text" name="money"/><br/>
        user用戶:<input type="text" name="user.username"/><br/>
        user金額:<input type="text" name="user.password"/><br/>
        <input type="submit" value="提交"/>
    </form>

注意在這裏,文件頭上必須加isELIgnored=“false”

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

    <h2>success!</h2>

    <br/>
    <%--獲取requestSope中的值--%>
    ${account}

</body>
</html>

運行並查看結果:

在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章