View Javadoc
1   /*
2    * $Id$
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  
22  package org.apache.struts.faces.application;
23  
24  
25  import java.io.IOException;
26  import java.util.Locale;
27  
28  import org.apache.struts.Globals;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  import jakarta.faces.FacesException;
33  import jakarta.faces.application.ViewHandler;
34  import jakarta.faces.application.ViewHandlerWrapper;
35  import jakarta.faces.component.UIViewRoot;
36  import jakarta.faces.context.ExternalContext;
37  import jakarta.faces.context.FacesContext;
38  
39  
40  /**
41   * Custom {@code ViewHandler} implementation that adds features
42   * specific to the Struts-Faces Integration Library. It leverages the
43   * "decorator pattern" customization strategy that JSF supports, by
44   * delegating most processing to the {@code ViewHandler} instance
45   * handed to our constructor.
46   */
47  public class ViewHandlerImpl extends ViewHandlerWrapper {
48  
49  
50      // -------------------------------------------------------- Static Variables
51  
52  
53      /**
54       * The {@code Log} instance for this class.
55       */
56      private final Logger log =
57          LoggerFactory.getLogger(ViewHandlerImpl.class);
58  
59  
60      // ------------------------------------------------------------ Constructors
61  
62  
63      /**
64       * Construct a {@code ViewHandlerImpl} decorating the
65       * specified {@code ViewHandler} instance.
66       *
67       * @param oldViewHandler {@code ViewHandler} to be decorated
68       */
69      public ViewHandlerImpl(ViewHandler oldViewHandler) {
70          super(oldViewHandler);
71          log.debug("Creating ViewHandler instance, wrapping handler {}",
72              oldViewHandler);
73      }
74  
75  
76      // ----------------------------------------------------- Specialized Methods
77  
78  
79      /**
80       * If the Struts application has set a {@code Locale}, pass it
81       * on to JSF prior to delegating the actual rendering.
82       *
83       * @param context {@code FacesContext} for the current request
84       * @param viewToRender {@code UIViewRoot} to be rendered
85       */
86      @Override
87      public void renderView(FacesContext context, UIViewRoot viewToRender)
88              throws IOException, FacesException {
89  
90          log.debug("renderView({})", viewToRender.getViewId());
91          ExternalContext econtext = context.getExternalContext();
92          if (econtext.getSession(false) != null) {
93              Locale locale = (Locale)
94                  econtext.getSessionMap().get(Globals.LOCALE_KEY);
95              if (locale != null) {
96                  log.trace("Setting view locale to {}", locale);
97                  viewToRender.setLocale(locale);
98              }
99          }
100         super.renderView(context, viewToRender);
101     }
102 }