⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmsjsptaginclude.java

📁 内容管理
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        ServletRequest req = pageContext.getRequest();
        ServletResponse res = pageContext.getResponse();
        
        // This will always be true if the page is called through OpenCms 
        if (CmsFlexController.isCmsRequest(req)) {
            CmsFlexController controller = (CmsFlexController)req.getAttribute(CmsFlexController.ATTRIBUTE_NAME); 
            String target = null;                       
            
            // Try to find out what to do
            if (m_target != null) {
                // Option 1: target is set with "page" or "file" parameter
                target = m_target + getSuffix();
            } else if (m_property != null) {            
                // Option 2: target is set with "property" parameter
                try { 
                    String prop = controller.getCmsObject().readProperty(controller.getCmsObject().getRequestContext().getUri(), m_property, true);
                    if (prop != null) target = prop + getSuffix();
                } catch (Exception e) {} // target will be null
            } else if (m_attribute != null) {            
                // Option 3: target is set in "attribute" parameter
                try { 
                    String attr = (String)req.getAttribute(m_attribute);
                    if (attr != null) target = attr + getSuffix();
                } catch (Exception e) {} // target will be null
            } else {
                // Option 4: target might be set in body
                String body = null;
                if (getBodyContent() != null) {
                    body = getBodyContent().getString();
                    if ((body != null) && (! "".equals(body.trim()))) {
                        // target IS set in body
                        target = body + getSuffix();
                    } 
                    // else target is not set at all, default will be used 
                }
            } 
              
            // no perfrom the include action
            includeTagAction(pageContext, target, m_element, m_parameterMap, req, res);
            
            // must call release here manually to make sure m_parameterMap is cleared
            release();
        }
        
        return EVAL_PAGE;
    }
    
    /**
     * Include action method.<p>
     * 
     * The logic in this mehod is more complex than it should be.
     * This is because of the XMLTemplate integration, which requires some settings 
     * to the parameters understandable only to XMLTemplate gurus.
     * By putting this logic here it is not required to care about these issues
     * on JSP pages, and you end up with considerable less JSP code.
     * Also JSP developers need not to know the intrinsics of XMLTemplates this way.<p>
     * 
     * @param context the current JSP page context
     * @param target the target for the include, might be <code>null</code>
     * @param element the element to select form the target might be <code>null</code>
     * @param paramMap a map of parameters for the include, will be merged with the request 
     *      parameters, might be <code>null</code>
     * @param req the current request
     * @param res the current response
     * @throws JspException in case something goes wrong
     */
    static void includeTagAction(PageContext context, String target, String element, Map paramMap, ServletRequest req, ServletResponse res) 
    throws JspException {
        
        CmsFlexController controller = (CmsFlexController)req.getAttribute(CmsFlexController.ATTRIBUTE_NAME);
        
        if (target == null) {
            // set target to default
            target = controller.getCmsObject().getRequestContext().getUri();
        }
        
        Map parameterMap = new HashMap();      
        if (paramMap != null) {
            // add all parameters 
            parameterMap.putAll(paramMap);
        }                    
        
        if (element != null) {            
            // add template element selector for JSP templates
            addParameter(parameterMap, CmsJspTagTemplate.C_TEMPLATE_ELEMENT, element, true);
            if (!("body".equals(element) || "(default)".equals(element))) {
                // add template selector for multiple body XML files
                addParameter(parameterMap, CmsXmlTemplate.C_FRAME_SELECTOR, element, true);
            }
        }
                       
        // try to figure out if the target is a page or XMLTemplate file        
        boolean isPageTarget = false;         
        try {
            target = controller.getCurrentRequest().toAbsolute(target);
            CmsResource resource = controller.getCmsObject().readFileHeader(target);
            isPageTarget = ((controller.getCmsObject().getResourceType("page").getResourceType() == resource.getType()));
        } catch (CmsException e) {
            // any exception here and we will treat his as non-Page file
            throw new JspException("File not found: " + target, e);
        }
                
        String bodyAttribute = (String) controller.getCmsObject().getRequestContext().getAttribute(I_CmsConstants.C_XML_BODY_ELEMENT);               
        if (bodyAttribute == null) {
            // no body attribute is set: this is NOT a sub-element in a XML mastertemplate
            if (isPageTarget) {
                // add body file path to target 
                if (! target.startsWith(I_CmsWpConstants.C_VFS_PATH_BODIES)) {
                    target = I_CmsWpConstants.C_VFS_PATH_BODIES + target.substring(1);
                }              
                // save target as "element replace" parameter  
                addParameter(parameterMap, CmsXmlLauncher.C_ELEMENT_REPLACE, "body:" + target, true);  
                target = C_BODYLOADER_URI;              
            } 
            // for other cases setting of "target" is fine 
        } else {
            // body attribute is set: this is a sub-element in a XML mastertemplate
            if (target.equals(controller.getCmsObject().getRequestContext().getUri())) {
                // target can be ignored, set body attribute as "element replace" parameter  
                addParameter(parameterMap, CmsXmlLauncher.C_ELEMENT_REPLACE, "body:" + bodyAttribute, true);
                // redirect target to body loader
                target = C_BODYLOADER_URI;                
            } else {
                if (isPageTarget) {
                    // add body file path to target 
                    if (! target.startsWith(I_CmsWpConstants.C_VFS_PATH_BODIES)) {
                        target = I_CmsWpConstants.C_VFS_PATH_BODIES + target.substring(1);
                    }              
                    // save target as "element replace" parameter  
                    addParameter(parameterMap, CmsXmlLauncher.C_ELEMENT_REPLACE, "body:" + target, true);  
                    target = C_BODYLOADER_URI;                     
                }
            }
            // for other cases setting of "target" is fine             
        }          
               
        // save old parameters from request
        Map oldParamterMap = req.getParameterMap();
        controller.getCurrentRequest().addParameterMap(parameterMap);  
        
        try {         
            // Write out a C_FLEX_CACHE_DELIMITER char on the page, this is used as a parsing delimeter later
            context.getOut().print((char)com.opencms.flex.cache.CmsFlexResponse.C_FLEX_CACHE_DELIMITER);
            
            // Add an element to the include list (will be initialized if empty)
            controller.getCurrentResponse().addToIncludeList(target, parameterMap);
            
            controller.getCurrentRequest().getRequestDispatcher(target).include(req, res);    
            
        } catch (javax.servlet.ServletException e) {
            if (DEBUG) System.err.println("JspTagInclude: ServletException in Jsp 'include' tag processing: " + e);
            if (DEBUG) System.err.println(com.opencms.util.Utils.getStackTrace(e));                
            throw new JspException(e);            
        } catch (java.io.IOException e) {
            if (DEBUG) System.err.println("JspTagInclude: IOException in Jsp 'include' tag processing: " + e);
            if (DEBUG) System.err.println(com.opencms.util.Utils.getStackTrace(e));                
            throw new JspException(e);
        } finally {
            if (oldParamterMap != null) controller.getCurrentRequest().setParameterMap(oldParamterMap);            
        }           
    }
    
	/**
     * This methods adds parameters to the current request.
     * Parameters added here will be treated like parameters from the 
     * HttpRequest on included pages.<p>
     * 
     * Remember that the value for a parameter in a HttpRequest is a 
     * String array, not just a simple String. If a parameter added here does
     * not already exist in the HttpRequest, it will be added. If a parameter 
     * exists, another value will be added to the array of values. If the 
     * value already exists for the parameter, nothing will be added, since a 
     * value can appear only once per parameter.<p>
     * 
     * @param name the name to add
     * @param value the value to add
	 * @see com.opencms.flex.jsp.I_CmsJspTagParamParent#addParameter(String, String)
	 */
	public void addParameter(String name, String value) {
        // No null values allowed in parameters
        if ((name == null) || (value == null)) return;

        if (DEBUG) System.err.println("CmsJspIncludeTag.addParameter: param=" + name + " value=" + value);
        
        // Check if internal map exists, create new one if not
        if (m_parameterMap == null) {
            m_parameterMap = new HashMap();
        }
        
        addParameter(m_parameterMap, name, value, false);
    }

    /**
     * Internal method to add parameters.<p>
     * 
     * @param parameters the Map to add the parameters to
     * @param name the name to add
     * @param value the value to add
     */
    private static void addParameter(Map parameters, String name, String value, boolean overwrite) {
        // No null values allowed in parameters
        if ((parameters == null) || (name == null) || (value == null)) return;
        
        // Check if the parameter name (key) exists
        if (parameters.containsKey(name) && (! overwrite)) {
            // Yes: Check name values if value exists, if so do nothing, else add new value
            String[] values = (String[]) parameters.get(name);
            String[] newValues = new String[values.length+1];
            System.arraycopy(values, 0, newValues, 0, values.length);
            newValues[values.length] = value;
            parameters.put(name, newValues);
        } else {
            // No: Add new parameter name / value pair
            String[] values = new String[] { value };
            parameters.put(name, values);
        } 
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -