View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.taglib.core;
20  
21  import javax.faces.component.UIComponent;
22  import javax.faces.component.UINamingContainer;
23  import javax.faces.component.UIOutput;
24  import javax.faces.context.FacesContext;
25  import javax.faces.webapp.UIComponentELTag;
26  
27  import org.apache.myfaces.application.jsp.ServletViewResponseWrapper;
28  
29  /**
30   * @author Thomas Spiegl (latest modification by $Author$)
31   * @version $Revision$ $Date$
32   */
33  public class SubviewTag extends UIComponentELTag
34  {
35      public SubviewTag()
36      {
37          super();
38      }
39  
40      @Override
41      public String getComponentType()
42      {
43          return UINamingContainer.COMPONENT_TYPE;
44      }
45  
46      @Override
47      public String getRendererType()
48      {
49          return null;
50      }
51  
52      /**
53       * Creates a UIComponent from the BodyContent If a Subview is included via the <jsp:include> tag 
54       * the corresponding jsp is rendered with 
55       * getServletContext().getRequestDispatcher("includedSite").include(request,response) and it is
56       * possible that something was written to the Response direct. So is is necessary that the content of the wrapped
57       * response is added to the componenttree.
58       * 
59       * @return UIComponent or null
60       */
61      @Override
62      protected UIComponent createVerbatimComponentFromBodyContent()
63      {
64          UIOutput component = (UIOutput)super.createVerbatimComponentFromBodyContent();
65          FacesContext facesContext = FacesContext.getCurrentInstance();
66          Object response = facesContext.getExternalContext().getResponse();
67          String wrappedOutput;
68  
69          if (response instanceof ServletViewResponseWrapper)
70          {
71              ServletViewResponseWrapper wrappedResponse = (ServletViewResponseWrapper)response;
72              wrappedOutput = wrappedResponse.toString();
73              if (wrappedOutput != null && wrappedOutput.length() > 0)
74              {
75                  String componentvalue = null;
76                  if (component != null)
77                  {
78                      // save the Value of the Bodycontent
79                      componentvalue = (String)component.getValue();
80                  }
81                  component = super.createVerbatimComponent();
82                  if (componentvalue != null)
83                  {
84                      component.setValue(wrappedOutput + componentvalue);
85                  }
86                  else
87                  {
88                      component.setValue(wrappedOutput);
89                  }
90                  wrappedResponse.reset();
91              }
92          }
93          return component;
94      }
95  
96  }