這篇文章將為大家詳細(xì)講解有關(guān)Spring MVC如何實(shí)現(xiàn)接受表單自動(dòng)封裝特性,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
為海寧等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及海寧網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、海寧網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!
Spring MVC中的Controller可以以實(shí)體類接受來(lái)自客戶端的form表單,表單的字段自動(dòng)構(gòu)成實(shí)體類對(duì)象
客戶端的表單
實(shí)體類
public class User { private String name; private Integer age; private String hobby; public User() { this.name = "未初始化"; this.age = 10; this.hobby = "coding"; } public User(String name) { this.name = name; this.age = 10; this.hobby = "coding"; } public User(String name, Integer age) { this.name = name; this.age = age; this.hobby = "coding"; } public User(String name, Integer age, String hobby) { this.name = name; this.age = age; this.hobby = hobby; } public Integer getAge() { return age; } public String getHobby() { return hobby; } public String getName() { return name; } public void setAge(Integer age) { this.age = age; } public void setHobby(String hobby) { this.hobby = hobby; } public void setName(String name) { this.name = name; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + ", hobby='" + hobby + '\'' + '}'; } }
服務(wù)端接收
@Controller @RequestMapping("/test") public class TestController { @RequestMapping(value = "/user", method = RequestMethod.POST) // 控制器會(huì)自動(dòng)實(shí)例化參數(shù) public String user(User user) { System.out.println(user); return "redirect:/test/user"; } @RequestMapping(value = "/user", method = RequestMethod.GET) public String user() { return "form"; } }
關(guān)于“Spring MVC如何實(shí)現(xiàn)接受表單自動(dòng)封裝特性”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。