日常提交
This commit is contained in:
@@ -47,14 +47,14 @@
|
||||
- execution:匹配目标方法的执行,可以在括号中接收一个函数签名,包含返回类型、函数名和函数参数类型
|
||||
```java
|
||||
// 被@JointPoint注解标注的方法必须具有void的返回类型
|
||||
@JoinPoint("execution(* Point.*(..))")
|
||||
@Pointcut("execution(* Point.*(..))")
|
||||
void methodInjected() {
|
||||
|
||||
}
|
||||
```
|
||||
- within:匹配声明在某一特定类中的方法
|
||||
```java
|
||||
@JoinPoint("within(Point)")
|
||||
@Pointcut("within(Point)")
|
||||
```
|
||||
- this:匹配生成的代理对象为该类型的一个实例
|
||||
- target:匹配目标对象为该类型的一个实例
|
||||
@@ -65,7 +65,7 @@
|
||||
- @annotation:执行的方法具有指定注解
|
||||
- Spring AOP同样支持将JoinPoint匹配为具有特定name的Spring bean对象
|
||||
```java
|
||||
@JoinPoint("bean(nameA) || bean(nameB))")
|
||||
@Pointcut("bean(nameA) || bean(nameB))")
|
||||
```
|
||||
- ## Spring AOP中的Advice
|
||||
- Advice和Pointcut Expresion相关联,主要可以分为before、after、around等种类
|
||||
|
||||
@@ -462,3 +462,40 @@ public class User {
|
||||
}
|
||||
```
|
||||
### Model
|
||||
#### @ModelAttribute注解用法
|
||||
- 将@ModelAttribute注解标记在handler method的参数上,用于创建或访问一个对象,该对象从model中获取,并且该对象通过WebDataBinder与http request绑定在一起。
|
||||
- 将@ModelAttribute注解绑定在位于@Controller或@ControllerAdvice类中的方法上,用于在任何handler method调用之前初始化model
|
||||
- 将@ModelAttribute注解绑定在@RequestMapping方法上,用于标注该方法的返回值是一个model attribute
|
||||
|
||||
#### @ModelAttribute作用于Controller类中普通方法上
|
||||
对于上述的第二种使用,一个controller类中可以含有任意数量个@ModelAttribute方法,所有的这些方法都会在@RequestMapping方法调用之前被调用。(**同一个@ModelAttribute方法,也可以通过@ControllerAdvice在多个controllers之间进行共享**)。
|
||||
@ModelAttribute方法支持可变的参数形式,其参数形式可以和@RequestMapping方法中的参数形式一样。(**但是,@ModelAttribute方法的参数中不能含有@ModelAttribute注解本身,参数也不能含有和http请求体相关的内容**)。
|
||||
```java
|
||||
@ModelAttribute
|
||||
public void populateModel(@RequestParam String number, Model model) {
|
||||
model.addAttribute(accountRepository.findAccount(number));
|
||||
// add more ...
|
||||
}
|
||||
```
|
||||
也可以通过如下方式向model中添加attribute:
|
||||
```java
|
||||
// 只向model中添加一个属性
|
||||
@ModelAttribute
|
||||
public Account addAccount(@RequestParam String number) {
|
||||
return accountRepository.findAccount(number);
|
||||
}
|
||||
```
|
||||
> 在向model中添加属性时,如果attribute name没有显式指定,那么attribute name将会基于attribute value的类型来决定。可以通过model.addAttribute(attributeName,attributeValue)来指定attribute name,或者通过指定@ModelAttribute的name属性来指定attribute name
|
||||
|
||||
#### @ModelAttribute作用于@RequestMapping方法上
|
||||
对于第三种使用,可以将@ModelAttribute注解标注在@RequestMapping方法之上,在这种情况下方法的返回值将会被解释为model attribute。**在这种情况下@ModelAttribute注解不是必须的,应为该行为是html controller的默认行为,除非返回值是String类型,此时返回值会被解释为view name。**
|
||||
可以通过如下方式来自定义返回model attribute的attribute name,如下图所示:
|
||||
```java
|
||||
// 指定attribute name为“myAccount”
|
||||
@GetMapping("/accounts/{id}")
|
||||
@ModelAttribute("myAccount")
|
||||
public Account handle() {
|
||||
// ...
|
||||
return account;
|
||||
}
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user