Home  >  Q&A  >  body text

How to assemble templates (EL technology related)?

As the title says, I have a page template in the background, which contains some places where specific data needs to be filled in. I call a method to pass in the template file and context object, and then return an assembled page html code. May I ask what technology is more convenient to implement this? I have considered Spring's EL expression and Java EE's JSTL, but I think it is not very useful. It would be best if you could give me a simple example.

template:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Spring EL Test</title>
</head>
<body>
    Hello, ${user.name}!
</body>
</html>

It is possible to call such a method

String result = parse(File file, Object contextObj);    // 得到组装后数据

Data after assembly:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Spring EL Test</title>
</head>
<body>
    Hello, ZhangSan!
</body>
</html>
ringa_leeringa_lee2702 days ago589

reply all(1)I'll reply

  • 大家讲道理

    大家讲道理2017-05-27 17:42:58

    First of all, let me correct you. EL expressions are not provided by Spring, but by JSP 2.x.

    The so-called assembly means using request.setAttribute()设一个名为user objects to parse the objects in jsp.

    This should be enough:

    User user = new User("ZhangSan"); // user.getName() 应该得到"ZhangSan"
    request.setAttribute("user", user);
    // 然后是forward到这个jsp

    reply
    0
  • Cancelreply