package com.itheima.domain;
import java.io.Serializable;
/**
* 账户的实体类
*
* @author ChonZ
*
*/
public class Account implements Serializable {
private String accountId;
private String name;
private Float money;
//如果实体类中包含pojo
private Address address;
public String getAccountId() {
return accountId;
}
public void setAccountId(String accountId) {
this.accountId = accountId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Float getMoney() {
return money;
}
public void setMoney(Float money) {
this.money = money;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
Account实体类,包含Address的pojo类
package com.itheima.domain;
import java.io.Serializable;
/*
* 地址的实体类
*/
public class Address implements Serializable {
private String provinceName;
private String city;
public String getProvinceName() {
return provinceName;
}
public void setProvinceName(String provinceName) {
this.provinceName = provinceName;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
address实体类
package com.itheima.domain;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
/**
* 一个用户实体类,它可以包含多个账户
*
* @author ChonZ
*
*/
public class User implements Serializable {
private String username;
private String passwoed;
private Integer age;
//实体类含有POJO类集合
private List<Account> accountList;
private Map<String,Account> accountMap;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPasswoed() {
return passwoed;
}
public void setPasswoed(String passwoed) {
this.passwoed = passwoed;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public List<Account> getAccountList() {
return accountList;
}
public void setAccountList(List<Account> accountList) {
this.accountList = accountList;
}
public Map<String, Account> getAccountMap() {
return accountMap;
}
public void setAccountMap(Map<String, Account> accountMap) {
this.accountMap = accountMap;
}
}
User实体类,包含Account pojo类的List集合,包含了Account pojo类的Map集合
package com.itheima.web.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.itheima.domain.Account;
import com.itheima.domain.User;
@Controller("accountController")
@RequestMapping("account")
public class AccountController {
/*
http://localhost:8080/xxxx?accountId=1
handler方法的形式参数变量名必须与url的参数名一致
*/
@RequestMapping("/findAccountById")
public String findAccount(Integer accountId) {
System.out.println("执行了查询账户" + accountId);
return "success";
}
/*
handler方法的形参是Account实体类型
通过表单input的name属性,找到Account实体类的相同的属性
通过实体类的set方法,进行数据封装
*/
@RequestMapping("/saveAccountById")
public String saveAccount(Account account) {
System.out.println("执行了查询账户" + account);
return "success";
}
/*
handler方法的形参是Account实体类型
通过表单input的name属性,找到Account实体类的相同的属性
通过实体类的set方法,进行数据封装
*/
@RequestMapping("/saveAccountById1")
public String saveAccount1(Account account) {
System.out.println("执行了保存账户" + account);
return "success";
}
/*
handler方法的形参是User实体类型,包含list集合
通过表单input的name属性accountList[0].name等,定位到User实体类所包含的集合的属性
通过Account实体类的set方法,进行数据封装
*/
@RequestMapping("/updateAccount")
public String updateAccount(User account) {
System.out.println("执行了更新账户" + account);
return "success";
}
/*
handler方法的形参是User实体类型,包含Map集合
通过表单input的name属性accountMap['map'].name等,定位到User实体类所包含的集合的属性
通过Account实体类的set方法,进行数据封装
*/
@RequestMapping("/updateAccount1")
public String updateAccount1(User account) {
System.out.println("执行了更新账户" + account);
return "success";
}
}
handler类
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>springmvc的入门案例</title>
</head>
<body>
<!-- 1.最基本的参数绑定 -->
<a href="account/findAccountById?id=1">查询用户</a>
<!-- 2.pojo的参数绑定 -->
<form action="account/saveAccount" method="post">
账户名称:<input type="text" name="name"/><br/>
账户金额:<input type="text" name="money"/><br/>
<input type="submit" value="提交"/>
</form>
<!-- 3.实体类含有pojo的参数绑定
先通过找到实体类的pojo属性address,再通过.xxxx找到pojo类中的属性
-->
<form action="account/saveAccount1" method="post">
账户名称:<input type="text" name="name"/><br/>
账户金额:<input type="text" name="money"/><br/>
账户省份:<input type="text" name="address.provinceName"/><br/>
账户城市:<input type="text" name="address.city"/><br/>
<input type="submit" value="提交"/>
</form>
<!-- 4.实体类含有pojo的List集合参数绑定
先通过accountList[0]找到实体类的list集合单个属性,再通过.xxxx找到list中的属性
-->
<form action="account/updateAccount" method="post">
账户名称:<input type="text" name="name"/><br/>
账户密码:<input type="password" name="password"/><br/>
账户年龄:<input type="text" name="age"/><br/>
账户1名称:<input type="text" name="accountList[0].name"/><br/>
账户1金额:<input type="text" name="accountList[0].money"/><br/>
账户2名称:<input type="text" name="accountList[1].name"/><br/>
账户2金额:<input type="text" name="accountList[1].money"/><br/>
<input type="submit" value="提交"/>
</form>
<!-- 5.实体类含有pojo的Map集合参数绑定
先通过accountMap['one']找到实体类的Map集合里面单个属性,再通过.xxxx找到Map中的属性
映射器会自动创建key
-->
<form action="account/updateAccount1" method="post">
账户名称:<input type="text" name="name"/><br/>
账户密码:<input type="password" name="password"/><br/>
账户年龄:<input type="text" name="age"/><br/>
账户3名称:<input type="text" name="accountMap['one'].name"/><br/>
账户3金额:<input type="text" name="accountMap['one'].money"/><br/>
账户4名称:<input type="text" name="accountMap['two'].name"/><br/>
账户5金额:<input type="text" name="accountMap['two'].money"/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
index.jsp测试页面