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 the corresponding
54       * jsp is rendered with getServletContext().getRequestDispatcher("includedSite").include(request,response) and it is
55       * possible that something was written to the Response direct. So is is necessary that the content of the wrapped
56       * response is added to the componenttree.
57       * 
58       * @return UIComponent or null
59       */
60      @Override
61      protected UIComponent createVerbatimComponentFromBodyContent()
62      {
63          UIOutput component = (UIOutput)super.createVerbatimComponentFromBodyContent();
64          FacesContext facesContext = FacesContext.getCurrentInstance();
65          Object response = facesContext.getExternalContext().getResponse();
66          String wrappedOutput;
67  
68          if (response instanceof ServletViewResponseWrapper)
69          {
70              ServletViewResponseWrapper wrappedResponse = (ServletViewResponseWrapper)response;
71              wrappedOutput = wrappedResponse.toString();
72              if (wrappedOutput != null && wrappedOutput.length() > 0)
73              {
74                  String componentvalue = null;
75                  if (component != null)
76                  {
77                      // save the Value of the Bodycontent
78                      componentvalue = (String)component.getValue();
79                  }
80                  component = super.createVerbatimComponent();
81                  if (componentvalue != null)
82                  {
83                      component.setValue(wrappedOutput + componentvalue);
84                  }
85                  else
86                  {
87                      component.setValue(wrappedOutput);
88                  }
89                  wrappedResponse.reset();
90              }
91          }
92          return component;
93      }
94  
95  }