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 java.util.Map;
25
26 import javax.faces.application.NavigationHandler;
27 import javax.faces.context.FacesContext;
28
29 import com.opensymphony.xwork2.ActionContext;
30 import com.opensymphony.xwork2.config.entities.ActionConfig;
31 import com.opensymphony.xwork2.config.entities.ResultConfig;
32
33 /***
34 * Overrides the JFS navigation by delegating the result to handling by the core
35 * result code lookup and execution. If a result cannot be found, the previous
36 * NavigationHandler is called.
37 */
38 public class StrutsNavigationHandler extends NavigationHandler {
39
40 private NavigationHandler parent;
41
42 /***
43 * Creates the handler
44 *
45 * @param handler The old NavigationHandler to possibly delegate to
46 */
47 public StrutsNavigationHandler(NavigationHandler handler) {
48 this.parent = handler;
49 }
50
51 /***
52 * Stores any outcomes as the result code, failing over to the old
53 * NavigationHandler
54 *
55 * @param facesContext The faces context
56 * @param fromAction The action we are coming from
57 * @param outcome The String return code
58 */
59 @Override
60 public void handleNavigation(FacesContext facesContext, String fromAction, String outcome) {
61 ActionContext ctx = ActionContext.getContext();
62 if (outcome != null) {
63 if (ctx == null && ctx.getActionInvocation() == null) {
64 delegateToParentNavigation(facesContext, fromAction, outcome);
65 } else {
66 ActionConfig config = ctx.getActionInvocation().getProxy().getConfig();
67 Map results = config.getResults();
68
69 ResultConfig resultConfig = null;
70
71 synchronized (config) {
72 try {
73 resultConfig = (ResultConfig) results.get(outcome);
74 } catch (NullPointerException e) {
75 }
76 if (resultConfig == null) {
77
78 resultConfig = (ResultConfig) results.get("*");
79 }
80 }
81 if (resultConfig != null) {
82 ctx.getActionInvocation().setResultCode(outcome);
83 } else {
84 delegateToParentNavigation(facesContext, fromAction, outcome);
85 }
86 }
87 }
88 }
89
90 private void delegateToParentNavigation(FacesContext facesContext, String fromAction, String outcome) {
91
92 parent.handleNavigation(facesContext, fromAction, outcome);
93 }
94
95 }