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.views.util;
22
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28
29 import org.apache.struts2.StrutsConstants;
30 import org.apache.struts2.util.StrutsUtil;
31 import org.apache.struts2.views.jsp.ui.OgnlTool;
32
33 import com.opensymphony.xwork2.ActionContext;
34 import com.opensymphony.xwork2.ActionInvocation;
35 import com.opensymphony.xwork2.inject.Inject;
36 import com.opensymphony.xwork2.util.ValueStack;
37
38 /***
39 * Value Stack's Context related Utilities.
40 *
41 */
42 public class ContextUtil {
43 public static final String REQUEST = "request";
44 public static final String REQUEST2 = "request";
45 public static final String RESPONSE = "response";
46 public static final String RESPONSE2 = "response";
47 public static final String SESSION = "session";
48 public static final String BASE = "base";
49 public static final String STACK = "stack";
50 public static final String OGNL = "ognl";
51 public static final String STRUTS = "struts";
52 public static final String ACTION = "action";
53
54 public static boolean altSyntax;
55
56 @Inject(StrutsConstants.STRUTS_TAG_ALTSYNTAX)
57 public static void setAltSyntax(String val) {
58 altSyntax = "true".equals(val);
59 }
60
61 public static Map getStandardContext(ValueStack stack, HttpServletRequest req, HttpServletResponse res) {
62 HashMap map = new HashMap();
63 map.put(REQUEST, req);
64 map.put(REQUEST2, req);
65 map.put(RESPONSE, res);
66 map.put(RESPONSE2, res);
67 map.put(SESSION, req.getSession(false));
68 map.put(BASE, req.getContextPath());
69 map.put(STACK, stack);
70 map.put(OGNL, OgnlTool.getInstance());
71 map.put(STRUTS, new StrutsUtil(stack, req, res));
72
73 ActionInvocation invocation = (ActionInvocation) stack.getContext().get(ActionContext.ACTION_INVOCATION);
74 if (invocation != null) {
75 map.put(ACTION, invocation.getAction());
76 }
77 return map;
78 }
79
80 /***
81 * Return true if either Configuration's altSyntax is on or the stack context's useAltSyntax is on
82 * @param context stack's context
83 * @return boolean
84 */
85 public static boolean isUseAltSyntax(Map context) {
86
87
88
89 return altSyntax ||(
90 (context.containsKey("useAltSyntax") &&
91 context.get("useAltSyntax") != null &&
92 "true".equals(context.get("useAltSyntax").toString())));
93 }
94
95 /***
96 * Returns a String for overriding the default templateSuffix if templateSuffix is on the stack
97 * @param context stack's context
98 * @return String
99 */
100 public static String getTemplateSuffix(Map context) {
101 return context.containsKey("templateSuffix") ? (String) context.get("templateSuffix") : null;
102 }
103 }