From 2a31c8f42d8d33f3c627d1b9dc72c96d27ee0ede Mon Sep 17 00:00:00 2001 From: Rikako Wu <496063163@qq.com> Date: Mon, 19 Dec 2022 23:51:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E5=B8=B8=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spring/Spring core/SpringMVC.md | 42 ++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/spring/Spring core/SpringMVC.md b/spring/Spring core/SpringMVC.md index 01394e5..e6260df 100644 --- a/spring/Spring core/SpringMVC.md +++ b/spring/Spring core/SpringMVC.md @@ -536,5 +536,45 @@ public class FormController { ``` ### Model Design 在web应用的上下文中,data binding涉及将http请求中的参数绑定到model object及其内层嵌套对象中。 -默认情况下,所有spring允许绑到model object中所有的公共属性(有public的getter和setter)。 +默认情况下,所有spring允许绑到model object中所有的公共属性(有public的getter和setter)。 +通常情况下,会自定义一个特定的model object类,并且该类中的public属性与表单中提交的参数相关联。 +```java +// 只会对如下两个public属性进行data binding +public class ChangeEmailForm { + + private String oldEmailAddress; + private String newEmailAddress; + + public void setOldEmailAddress(String oldEmailAddress) { + this.oldEmailAddress = oldEmailAddress; + } + + public String getOldEmailAddress() { + return this.oldEmailAddress; + } + + public void setNewEmailAddress(String newEmailAddress) { + this.newEmailAddress = newEmailAddress; + } + + public String getNewEmailAddress() { + return this.newEmailAddress; + } + +} +``` +### Exception +在@Controller和@ControllerAdvice类中,可以含有@ExceptionHandler方法,该方法用于处理controller方法中抛出的异常,使用如下所示: +```java +@Controller +public class SimpleController { + + // ... + + @ExceptionHandler + public ResponseEntity handle(IOException ex) { + // ... + } +} +```