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