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.config.ConfigurationException;
42 import com.opensymphony.xwork2.util.ValueStack;
43 import com.opensymphony.xwork2.util.ValueStackFactory;
44
45
46 /***
47 */
48 public class TagUtils {
49
50 public static ValueStack getStack(PageContext pageContext) {
51 HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
52 ValueStack stack = (ValueStack) req.getAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY);
53
54 if (stack == null) {
55 stack = ValueStackFactory.getFactory().createValueStack();
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 Map extraContext = du.createContextMap(new RequestMap(req),
65 req.getParameterMap(),
66 new SessionMap(req),
67 new ApplicationMap(pageContext.getServletContext()),
68 req,
69 res,
70 pageContext.getServletContext());
71 extraContext.put(ServletActionContext.PAGE_CONTEXT, pageContext);
72 stack.getContext().putAll(extraContext);
73 req.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
74
75
76 ActionContext.setContext(new ActionContext(stack.getContext()));
77 } else {
78
79 Map context = stack.getContext();
80 context.put(ServletActionContext.PAGE_CONTEXT, pageContext);
81
82 AttributeMap attrMap = new AttributeMap(context);
83 context.put("attr", attrMap);
84 }
85
86 return stack;
87 }
88
89 public static String buildNamespace(ActionMapper mapper, ValueStack stack, HttpServletRequest request) {
90 ActionContext context = new ActionContext(stack.getContext());
91 ActionInvocation invocation = context.getActionInvocation();
92
93 if (invocation == null) {
94 ActionMapping mapping = mapper.getMapping(request,
95 Dispatcher.getInstance().getConfigurationManager());
96
97 if (mapping != null) {
98 return mapping.getNamespace();
99 } else {
100
101
102
103
104 String path = RequestUtils.getServletPath(request);
105 return path.substring(0, path.lastIndexOf("/"));
106 }
107 } else {
108 return invocation.getProxy().getNamespace();
109 }
110 }
111 }