1 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 ; 21 import javax.servlet.http.HttpServletResponse ; 22 import javax.servlet.jsp.PageContext ; 23 import java.util.Map ; 24 25 26 31 public class TagUtils { 32 34 public static OgnlValueStack getStack(PageContext pageContext) { 35 HttpServletRequest req = (HttpServletRequest ) pageContext.getRequest(); 36 OgnlValueStack stack = (OgnlValueStack) req.getAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY); 37 38 if (stack == null) { 39 stack = new OgnlValueStack(); 40 41 HttpServletResponse res = (HttpServletResponse ) 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 ActionContext.setContext(new ActionContext(stack.getContext())); 57 } else { 58 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 buildNamespace(OgnlValueStack stack, HttpServletRequest 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 84 String path = request.getServletPath(); 85 return path.substring(0, path.lastIndexOf("/")); 86 } 87 } else { 88 return invocation.getProxy().getNamespace(); 89 } 90 } 91 } 92 | Popular Tags |