springmvc如何将对象类型的数据放入到model对象中
程序员文章站
2022-07-15 23:38:58
...
使用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>