View Javadoc

1   /*
2    * $Id: FacesResult.java 454455 2006-10-09 18:49:38Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * 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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.jsf;
19  
20  import javax.faces.application.ViewHandler;
21  import javax.faces.component.UIViewRoot;
22  import javax.faces.context.FacesContext;
23  
24  import org.apache.struts2.dispatcher.StrutsResultSupport;
25  
26  import com.opensymphony.xwork2.ActionInvocation;
27  import com.opensymphony.xwork2.Result;
28  
29  /***
30   * Executes the JSF render phase
31   */
32  public class FacesResult extends StrutsResultSupport implements Result {
33  
34      private static final long serialVersionUID = -3548970638740937804L;
35  
36      public FacesResult() {
37      	super();
38      }
39      
40      public FacesResult(String location) {
41      	super(location);
42      }
43      /***
44       * Checks to see if we need to build a new JSF ViewId from the Struts Result
45       * config and then renders the result by delegating to the
46       * FacesRender.render().
47       * 
48       * @see org.apache.struts2.dispatcher.StrutsResultSupport#doExecute(java.lang.String,
49       *      com.opensymphony.
50       */
51      protected void doExecute(String finalLocation, ActionInvocation invocation)
52              throws Exception {
53          performNavigation(finalLocation, FacesContext.getCurrentInstance());
54          new FacesRender().render(FacesContext.getCurrentInstance());
55      }
56  
57      /***
58       * Compares the Struts Result uri to the faces viewId. If they are different
59       * use the Struts uri to build a new faces viewId.
60       * 
61       * @param finalLocation
62       *            The result uri
63       * @param facesContext
64       *            The FacesContext
65       */
66      private void performNavigation(String finalLocation,
67              FacesContext facesContext) {
68          String facesViewId = facesContext.getViewRoot().getViewId();
69          if (finalLocation != null) {
70              if (finalLocation.equals(facesViewId) == false) {
71                  ViewHandler viewHandler = facesContext.getApplication()
72                          .getViewHandler();
73                  UIViewRoot viewRoot = viewHandler.createView(facesContext,
74                          finalLocation);
75                  facesContext.setViewRoot(viewRoot);
76                  facesContext.renderResponse();
77              }
78          }
79      }
80  
81  }