SpringMVC 处理模型数据:

  1 controller接收pojo:

    <form action="save" method="get">
<label for="">用户名:<input type="text" name="username" /></label>
     <label for="">密码:<input type="password" name="password" /></label>
<label for="">年龄:<input type="text" name="age" /></label>
<label for="">邮箱:<input type="text" name="email" /></label>
     <label for="">省份:<input type="text" name="address.province" />
     </label> <label for="">城市:<input type="text" name="address.city" /></label>
<button>保存</button>
</form>
    @RequestMapping("/save")
public String save(User user) { System.out.println(user); return "success";
}

  2 controller接收model:

   @RequestMapping("/edit2")
public String edit1(Model model) { User user = new User();
user.setUsername("harry");
user.setPassword("123");
user.setAge(17);
user.setEmail("[email protected]"); Address address = new Address();
address.setProvince("horwards");
address.setCity("potter");
user.setAddress(address);
model.addAttribute("user", user);
return "user/form";
}

  3 controller返回modleAndView:

<form action="save" method="get">
<label for="">用户名:<input type="text" name="username" value="${user.username}"/></label>
<label for="">密码:<input type="password" name="password" value="${user.password}"/></label>
<label for="">年龄:<input type="text" name="age" value="${user.age}"/></label>
<label for="">邮箱:<input type="text" name="email" value="${user.email}"/></label>
<label for="">省份:<input type="text" name="address.province" value="${user.address.province}"/></label>
<label for="">城市:<input type="text" name="address.city" value="${user.address.city}"/></label>
<button>保存</button>
</form>
    @RequestMapping("/edit")
public ModelAndView edit() { User user = new User();
user.setUsername("draco");
user.setPassword("123");
user.setAge(17);
user.setEmail("[email protected]"); Address address = new Address();
address.setProvince("horwards");
address.setCity("malfoy"); user.setAddress(address); ModelAndView mv = new ModelAndView();
mv.setViewName("user/form");
mv.addObject("user", user); return mv;
}
05-11 00:40