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