1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 }