Accessing Enums in Thymeleaf

classic Classic list List threaded Threaded
1 message Options
uma
Reply | Threaded
Open this post in threaded view
|

Accessing Enums in Thymeleaf

uma
This post was updated on .
I am trying to read Enum via thymeleaf template

    public enum OrderStatus{
     COMPLETE("Completed"),
     PENDING("Pending");

     private String status;
     
     public String getStatus(){
        return this.status;
     }
    }
When I try to access this enum from thymeleaf

    <div th:text=${T(com.order.enum.OrderStatus).COMPLETE.getStatus()}"/>
    <div th:text=${T(com.order.enum.OrderStatus).valueOf('COMPLETE').getStatus()}"/>

it works. But The Status COMPLETE and PENDING is based on input from my controller. I am trying some thing like this

    <div th:text=${T(com.order.enum.OrderStatus).${order.requestedStatus}.getStatus()}"/>
    <div th:text=${T(com.order.enum.OrderStatus).valueOf('${order.requestedStatus}').getStatus()}"/>

I am getting the Could not parse as expression: for the first one
No enum constant  com.order.enum.OrderStatus.${order.requestedStatus} for the second one
 Is there a way i can invoke the enum dynamically

My Order class

    Class Order{
       private String requestedStatus;
       
       getters & setters
   }

My controller Method

    @GetMapping
    public string getOrder(Model model){
      model.addAttribute("order", Order);
      return "result"
    }


I have posted in stackoverflow as well
https://stackoverflow.com/questions/60439841/accessing-enums-in-thymeleaf