springboot EL @Value

看一下代码:

@Controller
public class HelloController {
    //读取枚举值
    @Value("#{T(com.example.demo.model.EnumList.EnumList.TrackTraceState).Booking.getEnumItem().getItemCN()}")
    private String pwd;
 
    //读取方法
    @Value("#{T(com.example.demo.controller.HelloController).GetName()}")
    private String name;
 
    public static String GetName()
    {
        return "hello";
    }
 
    //读取配置文件
    @Value("${girl.age}")
    private Integer age;
 
    @RequestMapping("/index.do")
    public String say(ModelMap mode) {
 
        User u=new User();
        u.setUserName(name);
        u.setAge(age);
        u.setPassword(pwd);
        mode.addAttribute("user", u);
        return "say";
    }
 
}

application.yml:

girl:
  name: uiw
  age: 33

html:

<div th:text="${user.userName}">div>
<div th:text="${user.password}">div>
<div th:text="${user.age}">div>
<div th:text="${T(com.example.demo.model.EnumList.EnumList.IsApprovalEnum).Approved.getEnumItem().getItemCN()}" />
 

pwd: 调用的枚举值,以及方法返回枚举值的中文名

name:调用的静态方法GetName

age:读取配置文件

最后一个:是thymeleaf 调用后台枚举值的方法。

 

呈现:

springboot EL @Value_第1张图片

PS:

${} 读取上下文的属性值

#{} 启用Spring表达式,具有运算能力

 

转载于:https://www.cnblogs.com/hanjun0612/p/11102583.html

你可能感兴趣的:(springboot EL @Value)