日常提交
This commit is contained in:
@@ -536,5 +536,45 @@ public class FormController {
|
|||||||
```
|
```
|
||||||
### Model Design
|
### Model Design
|
||||||
在web应用的上下文中,data binding涉及将http请求中的参数绑定到model object及其内层嵌套对象中。
|
在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<String> handle(IOException ex) {
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user