View Javadoc

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