當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringMVC(十七-二十) ModelAttribute 注解
生活随笔
收集整理的這篇文章主要介紹了
SpringMVC(十七-二十) ModelAttribute 注解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
有點難理解。
修飾方法是表示在該控制器的所有目標方法執行前都執行該modelattribute注解的方法。
修飾參數是表示什么?修飾參數時@modelattributes(value="xxxx") User user ?中的value值需要和@modelattribute修改的方法加入的鍵的值一致。
?
Controller Code:
package com.tiekui.springmvc.handlers;import java.util.Map;import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam;import com.tiekui.springmvc.pojo.Address; import com.tiekui.springmvc.pojo.User; import com.tiekui.springmvc.pojo.UserNew;@Controller public class ModelAttributes {@RequestMapping("testModelAttribute")public String targetMethod(UserNew user){System.out.println("Target method" + user);return "afterModelAttributes";}@ModelAttributepublic void getuser(@RequestParam(value="id") String id,Map<String, UserNew> map) {if ((id != null)) {UserNew user = new UserNew("tiekui","1234","zhoutiekui@huawei.com",30,1, new Address("guangzhou", "zhenzhen"));System.out.println("Get an object" + user);map.put("userNew", user);}} }?
Response View:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%> <!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=ISO-8859-1"> <title>Insert title here</title> </head> <body>userAge: ${requestScope.userNew.age}</body> </html>?
View:
<form action="testModelAttribute"><br>password : <input type="text" name="username" value="tiekui"><br>password : <input type="password" name="password" value="1234"><br>email : <input type="text" name="email" value="zhoutiekui@huawei.com"><br>age : <input type="text" name="age" value="18"><br><input type="hidden" name="id" value="1"><br>Address Province: <input type="text" name="address.province" value="guangzhou"><br>Address City: <input type="text" name="address.city" value="shenzhen"><br><input type="submit" value="Test ModelAttributes"></form>?
POJO userNew:
package com.tiekui.springmvc.pojo;public class UserNew {private String username;private String password;private String email;private int age;private Integer id;public UserNew(String username, String password, String email, int age, Integer id, Address address) {super();this.username = username;this.password = password;this.email = email;this.age = age;this.id = id;this.address = address;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}private Address address;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public Address getAddress() {return address;}public void setAddress(Address address) {this.address = address;}@Overridepublic String toString() {return "UserNew [username=" + username + ", password=" + password + ", email=" + email + ", age=" + age+ ", id=" + id + ", address=" + address + "]";}} View Code?POJO Address:
package com.tiekui.springmvc.pojo;public class Address {private String province;private String city;public Address() {super();}public Address(String province, String city) {super();this.province = province;this.city = city;}@Overridepublic String toString() {return "Address [province=" + province + ", city=" + city + "]";}public String getProvince() {return province;}public void setProvince(String province) {this.province = province;}public String getCity() {return city;}public void setCity(String city) {this.city = city;} }?
轉載于:https://www.cnblogs.com/zhoutiekui/p/6351254.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的SpringMVC(十七-二十) ModelAttribute 注解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 改造一下jeecg中的部门树
- 下一篇: gradle idea java ssm