KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > views > jsp > TagUtils


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.views.jsp;
6
7 import com.opensymphony.webwork.ServletActionContext;
8 import com.opensymphony.webwork.dispatcher.ApplicationMap;
9 import com.opensymphony.webwork.dispatcher.DispatcherUtils;
10 import com.opensymphony.webwork.dispatcher.RequestMap;
11 import com.opensymphony.webwork.dispatcher.SessionMap;
12 import com.opensymphony.webwork.dispatcher.mapper.ActionMapper;
13 import com.opensymphony.webwork.dispatcher.mapper.ActionMapperFactory;
14 import com.opensymphony.webwork.dispatcher.mapper.ActionMapping;
15 import com.opensymphony.webwork.util.AttributeMap;
16 import com.opensymphony.xwork.ActionContext;
17 import com.opensymphony.xwork.ActionInvocation;
18 import com.opensymphony.xwork.util.OgnlValueStack;
19
20 import javax.servlet.http.HttpServletRequest JavaDoc;
21 import javax.servlet.http.HttpServletResponse JavaDoc;
22 import javax.servlet.jsp.PageContext JavaDoc;
23 import java.util.Map JavaDoc;
24
25
26 /**
27  * User: plightbo
28  * Date: Oct 17, 2003
29  * Time: 7:09:59 AM
30  */

31 public class TagUtils {
32     //~ Methods ////////////////////////////////////////////////////////////////
33

34     public static OgnlValueStack getStack(PageContext JavaDoc pageContext) {
35         HttpServletRequest JavaDoc req = (HttpServletRequest JavaDoc) pageContext.getRequest();
36         OgnlValueStack stack = (OgnlValueStack) req.getAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY);
37
38         if (stack == null) {
39             stack = new OgnlValueStack();
40
41             HttpServletResponse JavaDoc res = (HttpServletResponse JavaDoc) pageContext.getResponse();
42             DispatcherUtils.initialize(pageContext.getServletContext());
43             DispatcherUtils du = DispatcherUtils.getInstance();
44             Map extraContext = du.createContextMap(new RequestMap(req),
45                     req.getParameterMap(),
46                     new SessionMap(req),
47                     new ApplicationMap(pageContext.getServletContext()),
48                     req,
49                     res,
50                     pageContext.getServletContext());
51             extraContext.put(ServletActionContext.PAGE_CONTEXT, pageContext);
52             stack.getContext().putAll(extraContext);
53             req.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY, stack);
54
55             // also tie this stack/context to the ThreadLocal
56
ActionContext.setContext(new ActionContext(stack.getContext()));
57         } else {
58             // let's make sure that the current page context is in the action context
59
Map context = stack.getContext();
60             context.put(ServletActionContext.PAGE_CONTEXT, pageContext);
61
62             AttributeMap attrMap = new AttributeMap(context);
63             context.put("attr", attrMap);
64         }
65
66         return stack;
67     }
68
69     public static String JavaDoc buildNamespace(OgnlValueStack stack, HttpServletRequest JavaDoc request) {
70         ActionContext context = new ActionContext(stack.getContext());
71         ActionInvocation invocation = context.getActionInvocation();
72
73         if (invocation == null) {
74             ActionMapper mapper = ActionMapperFactory.getMapper();
75             ActionMapping mapping = mapper.getMapping(request);
76
77             if (mapping != null) {
78                 return mapping.getNamespace();
79             } else {
80                 // well, if the ActionMapper can't tell us, and there is no existing action invocation,
81
// let's just go with a default guess that the namespace is the last the path minus the
82
// last part (/foo/bar/baz.xyz -> /foo/bar)
83

84                 String JavaDoc path = request.getServletPath();
85                 return path.substring(0, path.lastIndexOf("/"));
86             }
87         } else {
88             return invocation.getProxy().getNamespace();
89         }
90     }
91 }
92
Popular Tags