JSTL/JSP EL (Expression Language) in a non JSP (standalone) context

前端 未结 10 1678
一向
一向 2020-12-01 08:28

Can anyone recommend a framework for templating/formatting messages in a standalone application along the lines of the JSP EL (Expression Language)?

I would expect t

10条回答
  •  感情败类
    2020-12-01 09:05

    I would go for the Spring Expression language:

    http://docs.spring.io/spring/docs/current/spring-framework-reference/html/expressions.html

    A few examples which demonstrate the power (the first two are from the documentation):

    int year = (Integer) parser.parseExpression("Birthdate.Year + 1900").getValue(context);
    
    String city = (String) parser.parseExpression("placeOfBirth.City").getValue(context);
    
    // weekday is a String, e.g. "Mon", time is an int, e.g. 1400 or 900
    {"Thu", "Fri"}.contains(weekday) and time matches '\d{4}'
    

    Expressions can also use object properties:

    public class Data {
        private String name; // getter and setter omitted
    }
    
    Data data = new Data();
    data.setName("John Doe");
    
    ExpressionParser p = new SpelExpressionParser();
    Expression e = p.parseExpression("name == 'John Doe'");
    Boolean r = (Boolean) e.getValue(data); // will return true
    
    e = p.parseExpression("Hello " + name + ", how are you ?");
    String text = e.getValue(data, String.class); // text will be "Hello John Doe, how are you ?"
    

提交回复
热议问题