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