1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts2.views.jsp;
19
20 import java.util.Map;
21
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
24 import javax.servlet.jsp.PageContext;
25
26 import org.apache.struts2.RequestUtils;
27 import org.apache.struts2.ServletActionContext;
28 import org.apache.struts2.dispatcher.ApplicationMap;
29 import org.apache.struts2.dispatcher.Dispatcher;
30 import org.apache.struts2.dispatcher.RequestMap;
31 import org.apache.struts2.dispatcher.SessionMap;
32 import org.apache.struts2.dispatcher.mapper.ActionMapper;
33 import org.apache.struts2.dispatcher.mapper.ActionMapperFactory;
34 import org.apache.struts2.dispatcher.mapper.ActionMapping;
35 import org.apache.struts2.util.AttributeMap;
36
37 import com.opensymphony.xwork2.ActionContext;
38 import com.opensymphony.xwork2.ActionInvocation;
39 import com.opensymphony.xwork2.util.ValueStack;
40 import com.opensymphony.xwork2.util.ValueStackFactory;
41
42
43 /***
44 */
45 public class TagUtils {
46
47 public static ValueStack getStack(PageContext pageContext) {
48 HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
49 ValueStack stack = (ValueStack) req.getAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY);
50
51 if (stack == null) {
52 stack = ValueStackFactory.getFactory().createValueStack();
53
54 HttpServletResponse res = (HttpServletResponse) pageContext.getResponse();
55 Dispatcher du = Dispatcher.getInstance();
56 Map extraContext = du.createContextMap(new RequestMap(req),
57 req.getParameterMap(),
58 new SessionMap(req),
59 new ApplicationMap(pageContext.getServletContext()),
60 req,
61 res,
62 pageContext.getServletContext());
63 extraContext.put(ServletActionContext.PAGE_CONTEXT, pageContext);
64 stack.getContext().putAll(extraContext);
65 req.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
66
67
68 ActionContext.setContext(new ActionContext(stack.getContext()));
69 } else {
70
71 Map context = stack.getContext();
72 context.put(ServletActionContext.PAGE_CONTEXT, pageContext);
73
74 AttributeMap attrMap = new AttributeMap(context);
75 context.put("attr", attrMap);
76 }
77
78 return stack;
79 }
80
81 public static String buildNamespace(ValueStack stack, HttpServletRequest request) {
82 ActionContext context = new ActionContext(stack.getContext());
83 ActionInvocation invocation = context.getActionInvocation();
84
85 if (invocation == null) {
86 ActionMapper mapper = ActionMapperFactory.getMapper();
87 ActionMapping mapping = mapper.getMapping(request,
88 Dispatcher.getInstance().getConfigurationManager());
89
90 if (mapping != null) {
91 return mapping.getNamespace();
92 } else {
93
94
95
96
97 String path = RequestUtils.getServletPath(request);
98 return path.substring(0, path.lastIndexOf("/"));
99 }
100 } else {
101 return invocation.getProxy().getNamespace();
102 }
103 }
104 }