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>

运行并查看结果:

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值