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