Save This Page
Home » struts-2.0.11.2-src » org.apache » struts2 » views » jsp » [javadoc | source]
    1   /*
    2    * $Id: TagUtils.java 572226 2007-09-03 03:25:47Z mrdon $
    3    *
    4    * Licensed to the Apache Software Foundation (ASF) under one
    5    * or more contributor license agreements.  See the NOTICE file
    6    * distributed with this work for additional information
    7    * regarding copyright ownership.  The ASF licenses this file
    8    * to you under the Apache License, Version 2.0 (the
    9    * "License"); you may not use this file except in compliance
   10    * with the License.  You may obtain a copy of the License at
   11    *
   12    *  http://www.apache.org/licenses/LICENSE-2.0
   13    *
   14    * Unless required by applicable law or agreed to in writing,
   15    * software distributed under the License is distributed on an
   16    * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   17    * KIND, either express or implied.  See the License for the
   18    * specific language governing permissions and limitations
   19    * under the License.
   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               // also tie this stack/context to the ThreadLocal
   76               ActionContext.setContext(new ActionContext(stack.getContext()));
   77           } else {
   78               // let's make sure that the current page context is in the action context
   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                   // well, if the ActionMapper can't tell us, and there is no existing action invocation,
  101                   // let's just go with a default guess that the namespace is the last the path minus the
  102                   // last part (/foo/bar/baz.xyz -> /foo/bar)
  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   }

Save This Page
Home » struts-2.0.11.2-src » org.apache » struts2 » views » jsp » [javadoc | source]