View Javadoc

1   /*
2    * $Id: FacesResult.java 477379 2006-11-20 22:41:56Z ddewolf $
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.jsf;
22  
23  import javax.faces.application.ViewHandler;
24  import javax.faces.component.UIViewRoot;
25  import javax.faces.context.FacesContext;
26  
27  import org.apache.struts2.dispatcher.StrutsResultSupport;
28  
29  import com.opensymphony.xwork2.ActionInvocation;
30  import com.opensymphony.xwork2.Result;
31  
32  /***
33   * Executes the JSF render phase
34   */
35  public class FacesResult extends StrutsResultSupport implements Result {
36  
37      private static final long serialVersionUID = -3548970638740937804L;
38  
39      public FacesResult() {
40          super();
41      }
42  
43      public FacesResult(String location) {
44          super(location);
45      }
46      /***
47       * Checks to see if we need to build a new JSF ViewId from the Struts Result
48       * config and then renders the result by delegating to the
49       * FacesRender.render().
50       *
51       * @see org.apache.struts2.dispatcher.StrutsResultSupport#doExecute(String, ActionInvocation)
52       */
53      protected void doExecute(String finalLocation, ActionInvocation invocation)
54              throws Exception {
55          performNavigation(finalLocation, FacesContext.getCurrentInstance());
56          new FacesRender().render(FacesContext.getCurrentInstance());
57      }
58  
59      /***
60       * Compares the Struts Result uri to the faces viewId. If they are different
61       * use the Struts uri to build a new faces viewId.
62       *
63       * @param finalLocation
64       *            The result uri
65       * @param facesContext
66       *            The FacesContext
67       */
68      private void performNavigation(String finalLocation,
69              FacesContext facesContext) {
70          String facesViewId = facesContext.getViewRoot().getViewId();
71          if (finalLocation != null) {
72              if (finalLocation.equals(facesViewId) == false) {
73                  ViewHandler viewHandler = facesContext.getApplication()
74                          .getViewHandler();
75                  UIViewRoot viewRoot = viewHandler.createView(facesContext,
76                          finalLocation);
77                  facesContext.setViewRoot(viewRoot);
78                  facesContext.renderResponse();
79              }
80          }
81      }
82  
83  }