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.jsp;
23
24 import java.util.Map;
25
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28 import javax.servlet.jsp.PageContext;
29
30 import org.apache.struts2.RequestUtils;
31 import org.apache.struts2.ServletActionContext;
32 import org.apache.struts2.dispatcher.ApplicationMap;
33 import org.apache.struts2.dispatcher.Dispatcher;
34 import org.apache.struts2.dispatcher.RequestMap;
35 import org.apache.struts2.dispatcher.SessionMap;
36 import org.apache.struts2.dispatcher.mapper.ActionMapper;
37 import org.apache.struts2.dispatcher.mapper.ActionMapping;
38 import org.apache.struts2.util.AttributeMap;
39
40 import com.opensymphony.xwork2.ActionContext;
41 import com.opensymphony.xwork2.ActionInvocation;
42 import com.opensymphony.xwork2.config.ConfigurationException;
43 import com.opensymphony.xwork2.util.ValueStack;
44 import com.opensymphony.xwork2.util.ValueStackFactory;
45
46
47 /***
48 */
49 public class TagUtils {
50
51 public static ValueStack getStack(PageContext pageContext) {
52 HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
53 ValueStack stack = (ValueStack) req.getAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY);
54
55 if (stack == null) {
56
57 HttpServletResponse res = (HttpServletResponse) pageContext.getResponse();
58 Dispatcher du = Dispatcher.getInstance();
59 if (du == null) {
60 throw new ConfigurationException("The Struts dispatcher cannot be found. This is usually caused by "+
61 "using Struts tags without the associated filter. Struts tags are only usable when the request "+
62 "has passed through its servlet filter, which initializes the Struts dispatcher needed for this tag.");
63 }
64 stack = du.getContainer().getInstance(ValueStackFactory.class).createValueStack();
65 Map<String, Object> extraContext = du.createContextMap(new RequestMap(req),
66 req.getParameterMap(),
67 new SessionMap(req),
68 new ApplicationMap(pageContext.getServletContext()),
69 req,
70 res,
71 pageContext.getServletContext());
72 extraContext.put(ServletActionContext.PAGE_CONTEXT, pageContext);
73 stack.getContext().putAll(extraContext);
74 req.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
75
76
77 ActionContext.setContext(new ActionContext(stack.getContext()));
78 } else {
79
80 Map<String, Object> context = stack.getContext();
81 context.put(ServletActionContext.PAGE_CONTEXT, pageContext);
82
83 AttributeMap attrMap = new AttributeMap(context);
84 context.put("attr", attrMap);
85 }
86
87 return stack;
88 }
89
90 public static String buildNamespace(ActionMapper mapper, ValueStack stack, HttpServletRequest request) {
91 ActionContext context = new ActionContext(stack.getContext());
92 ActionInvocation invocation = context.getActionInvocation();
93
94 if (invocation == null) {
95 ActionMapping mapping = mapper.getMapping(request,
96 Dispatcher.getInstance().getConfigurationManager());
97
98 if (mapping != null) {
99 return mapping.getNamespace();
100 } else {
101
102
103
104
105 String path = RequestUtils.getServletPath(request);
106 return path.substring(0, path.lastIndexOf("/"));
107 }
108 } else {
109 return invocation.getProxy().getNamespace();
110 }
111 }
112 }