View Javadoc

1   package org.seasar.cubby.action;
2   
3   import java.io.IOException;
4   
5   import javax.servlet.RequestDispatcher;
6   import javax.servlet.ServletException;
7   import javax.servlet.http.HttpServletRequest;
8   import javax.servlet.http.HttpServletResponse;
9   
10  import org.seasar.cubby.controller.ActionContext;
11  import org.seasar.cubby.util.CubbyUtils;
12  import org.seasar.framework.log.Logger;
13  import org.seasar.framework.util.StringUtil;
14  
15  /**
16   * 指定されたパスにフォワードする {@link ActionResult} です。
17   * <p>
18   * アクションメソッドの戻り値としてこのインスタンスを指定することで、指定されたパスにフォワードします。
19   * </p>
20   * <p>
21   * 使用例1 : フォワード先を相対パスで指定
22   * 
23   * <pre>
24   * return new Forward(&quot;list.jsp&quot;);
25   * </pre>
26   * 
27   * </p>
28   * <p>
29   * 使用例2 : フォワード先を絶対パスで指定
30   * 
31   * <pre>
32   * return new Forward(&quot;/todo/list.jsp&quot;);
33   * </pre>
34   * 
35   * </p>
36   * <p>
37   * また、フォワードの前処理として {@link #prerender(ActionContext, HttpServletRequest)}
38   * によって、リクエストにいくつかの属性が設定されます。
39   * </p>
40   * 
41   * @author baba
42   */
43  public class Forward extends AbstractActionResult {
44  
45  	private final Logger logger = Logger.getLogger(this.getClass());
46  
47  	private final String path;
48  
49  	/**
50  	 * インスタンスを生成します。
51  	 * 
52  	 * @param path
53  	 *            フォワード先のパス
54  	 */
55  	public Forward(final String path) {
56  		this.path = path;
57  	}
58  
59  	public void execute(final ActionContext context,
60  			final HttpServletRequest request, final HttpServletResponse response)
61  			throws ServletException, IOException {
62  		final Action action = context.getAction();
63  		final String actionClassName = CubbyUtils.getActionClassName(context
64  				.getComponentDef().getComponentClass());
65  
66  		final String absolutePath;
67  		if (this.path.startsWith("/")) {
68  			absolutePath = this.path;
69  		} else if (StringUtil.isEmpty(actionClassName)) {
70  			absolutePath = "/" + this.path;
71  		} else {
72  			absolutePath = "/" + actionClassName + "/" + this.path;
73  		}
74  		final HttpServletRequest wrappedRequest = new ForwardHttpServletRequestWrapper(
75  				request, context);
76  		if (logger.isDebugEnabled()) {
77  			logger.log("DCUB0001", new String[] { absolutePath });
78  		}
79  		final RequestDispatcher dispatcher = request
80  				.getRequestDispatcher(absolutePath);
81  		dispatcher.forward(wrappedRequest, response);
82  		if (logger.isDebugEnabled()) {
83  			logger.log("DCUB0002", new String[] { absolutePath });
84  		}
85  		action.postrender();
86  
87  		action.getFlash().clear();
88  	}
89  
90  	/**
91  	 * フォワード前の処理を行います。
92  	 * <p>
93  	 * 以下の処理を行います。
94  	 * <ul>
95  	 * <li>{@link Action#prerender()} を実行します。</li>
96  	 * <li>リクエストのアトリビュートに {@value org.seasar.cubby.CubbyConstants#ATTR_ACTION}
97  	 * を設定します。</li>
98  	 * <li>リクエストのアトリビュートに
99  	 * {@value org.seasar.cubby.CubbyConstants#ATTR_OUTPUT_VALUES} を設定します。</li>
100 	 * <li>リクエストのアトリビュートに、アクションのプロパティを設定します。</li>
101 	 * </ul>
102 	 * </p>
103 	 */
104 	@Override
105 	public void prerender(final ActionContext context,
106 			final HttpServletRequest request) {
107 
108 		final Action action = context.getAction();
109 		action.prerender();
110 	}
111 
112 }