View Javadoc

1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.cubby.action;
17  
18  import java.io.IOException;
19  
20  import javax.servlet.RequestDispatcher;
21  import javax.servlet.ServletException;
22  import javax.servlet.http.HttpServletRequest;
23  import javax.servlet.http.HttpServletResponse;
24  
25  import org.seasar.cubby.controller.ActionContext;
26  import org.seasar.cubby.util.CubbyUtils;
27  import org.seasar.framework.log.Logger;
28  import org.seasar.framework.util.StringUtil;
29  
30  /**
31   * 指定されたパスにフォワードする {@link ActionResult} です。
32   * <p>
33   * アクションメソッドの戻り値としてこのインスタンスを指定することで、指定されたパスにフォワードします。
34   * </p>
35   * <p>
36   * 使用例1 : フォワード先を相対パスで指定
37   * 
38   * <pre>
39   * return new Forward(&quot;list.jsp&quot;);
40   * </pre>
41   * 
42   * </p>
43   * <p>
44   * 使用例2 : フォワード先を絶対パスで指定
45   * 
46   * <pre>
47   * return new Forward(&quot;/todo/list.jsp&quot;);
48   * </pre>
49   * 
50   * </p>
51   * 
52   * @author baba
53   * @since 1.0.0
54   */
55  public class Forward extends AbstractActionResult {
56  
57  	private static final Logger logger = Logger.getLogger(Forward.class);
58  
59  	private final String path;
60  
61  	/**
62  	 * インスタンスを生成します。
63  	 * 
64  	 * @param path
65  	 *            フォワード先のパス
66  	 */
67  	public Forward(final String path) {
68  		this.path = path;
69  	}
70  
71  	/**
72  	 * パスを取得します。
73  	 * 
74  	 * @return パス
75  	 */
76  	public String getPath() {
77  		return this.path;
78  	}
79  
80  	/**
81  	 * {@inheritDoc}
82  	 */
83  	public void execute(final ActionContext context,
84  			final HttpServletRequest request, final HttpServletResponse response)
85  			throws ServletException, IOException {
86  		final Action action = context.getAction();
87  		final String actionDirectory = CubbyUtils.getActionDirectory(context
88  				.getComponentDef().getComponentClass());
89  
90  		final String absolutePath;
91  		if (this.path.startsWith("/")) {
92  			absolutePath = this.path;
93  		} else if (StringUtil.isEmpty(actionDirectory)) {
94  			absolutePath = "/" + this.path;
95  		} else {
96  			absolutePath = "/" + actionDirectory + "/" + this.path;
97  		}
98  		if (logger.isDebugEnabled()) {
99  			logger.log("DCUB0001", new String[] { absolutePath });
100 		}
101 		final RequestDispatcher dispatcher = request
102 				.getRequestDispatcher(absolutePath);
103 		dispatcher.forward(request, response);
104 		if (logger.isDebugEnabled()) {
105 			logger.log("DCUB0002", new String[] { absolutePath });
106 		}
107 		action.postrender();
108 
109 		action.getFlash().clear();
110 	}
111 
112 	/**
113 	 * フォワード前の処理を行います。
114 	 * <p>
115 	 * {@link Action#prerender()} を実行します。
116 	 * </p>
117 	 */
118 	@Override
119 	public void prerender(final ActionContext context) {
120 		final Action action = context.getAction();
121 		action.prerender();
122 	}
123 
124 }