1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts2.util;
22
23 import java.io.CharArrayWriter;
24 import java.io.IOException;
25
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28
29 import org.apache.struts2.views.velocity.VelocityManager;
30 import org.apache.velocity.app.VelocityEngine;
31 import org.apache.velocity.context.Context;
32 import org.apache.velocity.exception.MethodInvocationException;
33 import org.apache.velocity.exception.ParseErrorException;
34 import org.apache.velocity.exception.ResourceNotFoundException;
35
36 import com.opensymphony.xwork2.util.ValueStack;
37
38
39 /***
40 * Struts velocity related util.
41 *
42 */
43 public class VelocityStrutsUtil extends StrutsUtil {
44
45 private Context ctx;
46 private VelocityEngine velocityEngine;
47
48 public VelocityStrutsUtil(VelocityEngine engine, Context ctx, ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
49 super(stack, request, response);
50 this.ctx = ctx;
51 this.velocityEngine = engine;
52 }
53
54 public String evaluate(String expression) throws IOException, ResourceNotFoundException, MethodInvocationException, ParseErrorException {
55 CharArrayWriter writer = new CharArrayWriter();
56 velocityEngine.evaluate(ctx, writer, "Error parsing " + expression, expression);
57
58 return writer.toString();
59 }
60
61 }