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