Java – how do I access static members in a velocity template?

I don't know if there is a way to do this in velocity:

I have a user POJO, an attribute called status. It looks like an enumeration (but not because I'm stuck in Java 1.4). The definition looks like this:

public class User  {

    // default status to User
    private Status status = Status.USER;

    public void setStatus(Status status) {
        this.status = status;
    }

    public Status getStatus() {
        return status;
    }

The state is a static inner class:

public static final class Status {

    private String statusString;

    private Status(String statusString) {
        this.statusString = statusString;
    }

    public final static Status USER = new Status("user");
    public final static Status ADMIN = new Status("admin");
    public final static Status STATUS_X = new Status("blah");

    //.equals() and .hashCode() implemented as well
}

Using this pattern, user state can be easily tested in conditions

if(User.Status.ADMIN.equals(user.getStatus())) ...

... without reference to any constants such as status ID, any magic number, etc

However, I don't know how to use VTL to test these conditions in my velocity template I want to print a simple string according to the user's status, such as:

Welcome <b>${user.name}</b>!
<br/>
<br/>

#if($user.status == com.company.blah.User.Status.USER)
    You are a regular user
#elseif($user.status == com.company.blah.User.Status.ADMIN)
    You are an administrator
#etc...

#end

But this throws an exception that looks like org apache. veLocity. exception. Parseerrorexception: in webpages / include / dashboard "User" [line 10, column 21] encountered in Inc. expected: "["“

Since the VTL user guide, there is no mention of directly accessing Java classes / static members in VTL. It seems that the right side of the condition (RHS) can only be numeric text, string text, attribute reference or method reference

So is there any way to access static Java properties / references in the velocity template? I know that as a solution, I can embed status IDs or other identifiers in my controller as references (this is a web MVC application using velocity as view technology), but I strongly don't want to embed any numbers or constants in the magic view layer

Solution

I came up with a solution that allows me to put each user The status object is added to the velocity context to avoid any reference to constants or magic numbers in the template

On the controller / Java side:

// put the statuses directly into the model
Map statusMap = new HashMap();
statusMap.put("user",User.Status.USER);
statusMap.put("groupOperator",User.Status.ADMIN);
...
modelAndView.addObject("statusmap",statusMap);

Then, in the template, these values can be referenced as follows:

#if($user.status == $statusmap.user)
   You are a regular user
#elseif($user.status == $statusmap.admin)
    You are an administrator
##etc...
#end
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>