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   * 
37   * @author baba
38   */
39  public class Forward extends AbstractActionResult {
40  
41  	private static final Logger logger = Logger.getLogger(Forward.class);
42  
43  	private final String path;
44  
45  	/**
46  	 * インスタンスを生成します。
47  	 * 
48  	 * @param path
49  	 *            フォワード先のパス
50  	 */
51  	public Forward(final String path) {
52  		this.path = path;
53  	}
54  
55  	public void execute(final ActionContext context,
56  			final HttpServletRequest request, final HttpServletResponse response)
57  			throws ServletException, IOException {
58  		final Action action = context.getAction();
59  		final String actionClassName = CubbyUtils.getActionClassName(context
60  				.getComponentDef().getComponentClass());
61  
62  		final String absolutePath;
63  		if (this.path.startsWith("/")) {
64  			absolutePath = this.path;
65  		} else if (StringUtil.isEmpty(actionClassName)) {
66  			absolutePath = "/" + this.path;
67  		} else {
68  			absolutePath = "/" + actionClassName + "/" + this.path;
69  		}
70  		if (logger.isDebugEnabled()) {
71  			logger.log("DCUB0001", new String[] { absolutePath });
72  		}
73  		final RequestDispatcher dispatcher = request
74  				.getRequestDispatcher(absolutePath);
75  		dispatcher.forward(request, response);
76  		if (logger.isDebugEnabled()) {
77  			logger.log("DCUB0002", new String[] { absolutePath });
78  		}
79  		action.postrender();
80  
81  		action.getFlash().clear();
82  	}
83  
84  	/**
85  	 * フォワード前の処理を行います。
86  	 * <p>
87  	 * {@link Action#prerender()} を実行します。
88  	 * </p>
89  	 */
90  	@Override
91  	public void prerender(final ActionContext context) {
92  		final Action action = context.getAction();
93  		action.prerender();
94  	}
95  
96  }