View Javadoc

1   package org.seasar.cubby.action;
2   
3   import javax.servlet.http.HttpServletRequest;
4   import javax.servlet.http.HttpServletResponse;
5   
6   import org.seasar.cubby.controller.ActionContext;
7   import org.seasar.cubby.util.CubbyUtils;
8   import org.seasar.framework.log.Logger;
9   import org.seasar.framework.util.StringUtil;
10  
11  /**
12   * 指定されたパスにリダイレクトする {@link ActionResult} です。
13   * <p>
14   * アクションメソッドの戻り値としてこのインスタンスを指定することで、指定されたパスにリダイレクトします。
15   * </p>
16   * <p>
17   * 使用例1 : リダイレクト先を相対パスで指定
18   * 
19   * <pre>
20   * return new Redirect(&quot;list&quot;);
21   * </pre>
22   * 
23   * </p>
24   * <p>
25   * 使用例2 : リダイレクト先を絶対パスで指定
26   * 
27   * <pre>
28   * return new Redirect(&quot;/todo/list&quot;);
29   * </pre>
30   * 
31   * </p>
32   * 
33   * @author baba
34   */
35  public class Redirect extends AbstractActionResult {
36  
37  	private final Logger logger = Logger.getLogger(this.getClass());
38  
39  	private final String path;
40  
41  	/**
42  	 * インスタンスを生成します。
43  	 * 
44  	 * @param path
45  	 *            リダイレクト先のパス
46  	 */
47  	public Redirect(final String path) {
48  		this.path = path;
49  	}
50  
51  	public void execute(final ActionContext context,
52  			final HttpServletRequest request, final HttpServletResponse response)
53  			throws Exception {
54  
55  		final String absolutePath;
56  		final String contextPath = request.getContextPath();
57  		if (this.path.charAt(0) == '/') {
58  			absolutePath = contextPath + this.path;
59  		} else {
60  			final String actionClassName = CubbyUtils
61  					.getActionClassName(context.getComponentDef()
62  							.getComponentClass());
63  			if (StringUtil.isEmpty(actionClassName)) {
64  				absolutePath = contextPath + "/" + this.path;
65  			} else {
66  				absolutePath = contextPath + "/" + actionClassName + "/"
67  						+ this.path;
68  			}
69  		}
70  		if (logger.isDebugEnabled()) {
71  			logger.log("DCUB0003", new String[] { absolutePath });
72  		}
73  		response.sendRedirect(absolutePath);
74  	}
75  
76  }