溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Spring MVC 使用POJO對象綁定請求參數(shù)值

發(fā)布時間:2020-08-06 15:08:28 來源:網(wǎng)絡 閱讀:459 作者:沙漏半杯 欄目:編程語言

index.jsp前臺頁面加上這些,把值輸入之后,通過post提交到后臺。


<form action="springmvc/testPOJO" method="post">

? ? ? ? username: <input type="text" name="username"/>

? ? ? ? <br/>

? ? ? ? password: <input type="password" name="password"/>

? ? ? ? <br/>

? ? ? ? email: <input type="text" name="email"/>

? ? ? ? <br/>

? ? ? ? age: <input type="text" name="age"/>

? ? ? ? <br/>

? ? ? ? city: <input type="text" name="address.city"/>

? ? ? ? <br/>

? ? ? ? province: <input type="text" name="address.province"/>

? ? ? ? <br/>

? ? ? ? <input type="submit" value="Submit"/>

? ? </form>


user.java


package com.hust.springmvc.entities;


public class User {

? ? private String username;

? ? private String password;


? ? private String email;

? ? private int age;


? ? private Address address;


? ? public Address getAddress() {

? ? ? ? return address;

? ? }


? ? public void setAddress(Address address) {

? ? ? ? this.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;

? ? }


? ? @Override

? ? public String toString() {

? ? ? ? return "User [username=" + username + ",password=" + password + ",email=" + email + ",age=" + age + ",adress"

? ? ? ? ? ? ? ? + address + "]";

? ? }

}


address.java


package com.hust.springmvc.entities;


public class Address {

? ? private String province;

? ? private String 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;

? ? }


? ? @Override

? ? public String toString() {

? ? ? ? return "Address [province=" + province + ", city=" + city + "]";

? ? }


}


SpringMVCTest.java


package com.hust.springmvc1;


import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.SessionAttributes;


import com.hust.springmvc.entities.User;


@Controller

@RequestMapping("/springmvc")

public class SpringMVCTest {?


? ? private static final String SUCCESS = "success";


? ? /**

? ? ?* SpringMVC 會按請求參數(shù)名和 POJO 屬性名進行自動匹配, 自動為該對象填充屬性值。

? ? ?* 支持級聯(lián)屬性。如 address.city、address.province 等

? ? ?*/

? ? @RequestMapping("/testPOJO")

? ? public String testPOJO(User user) {

? ? ? ? System.out.println("testPOJO User: " + user);

? ? ? ? return SUCCESS;

? ? }


}


這個時候控制臺就會收到前臺傳過來的所有的值,包括adress里面的city和province。

歡迎工作一到五年的Java工程師朋友們加入Java技術交流群:659270626
群內(nèi)提供免費的Java架構學習資料(里面有高可用、高并發(fā)、高性能及分布式、Jvm性能調(diào)優(yōu)、Spring源碼,MyBatis,Netty,Redis,Kafka,Mysql,Zookeeper,Tomcat,Docker,Dubbo,Nginx等多個知識點的架構資料)合理利用自己每一分每一秒的時間來學習提升自己,不要再用"沒有時間“來掩飾自己思想上的懶惰!趁年輕,使勁拼,給未來的自己一個交代!


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI