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   */
54  public class Forward extends AbstractActionResult {
55  
56  	private static final Logger logger = Logger.getLogger(Forward.class);
57  
58  	private final String path;
59  
60  	/**
61  	 * インスタンスを生成します。
62  	 * 
63  	 * @param path
64  	 *            フォワード先のパス
65  	 */
66  	public Forward(final String path) {
67  		this.path = path;
68  	}
69  
70  	/**
71  	 * パスを取得します。
72  	 * @return パス
73  	 */
74  	public String getPath() {
75  		return this.path;
76  	}
77  	
78  	public void execute(final ActionContext context,
79  			final HttpServletRequest request, final HttpServletResponse response)
80  			throws ServletException, IOException {
81  		final Action action = context.getAction();
82  		final String actionClassName = CubbyUtils.getActionClassName(context
83  				.getComponentDef().getComponentClass());
84  
85  		final String absolutePath;
86  		if (this.path.startsWith("/")) {
87  			absolutePath = this.path;
88  		} else if (StringUtil.isEmpty(actionClassName)) {
89  			absolutePath = "/" + this.path;
90  		} else {
91  			absolutePath = "/" + actionClassName + "/" + this.path;
92  		}
93  		if (logger.isDebugEnabled()) {
94  			logger.log("DCUB0001", new String[] { absolutePath });
95  		}
96  		final RequestDispatcher dispatcher = request
97  				.getRequestDispatcher(absolutePath);
98  		dispatcher.forward(request, response);
99  		if (logger.isDebugEnabled()) {
100 			logger.log("DCUB0002", new String[] { absolutePath });
101 		}
102 		action.postrender();
103 
104 		action.getFlash().clear();
105 	}
106 
107 	/**
108 	 * フォワード前の処理を行います。
109 	 * <p>
110 	 * {@link Action#prerender()} を実行します。
111 	 * </p>
112 	 */
113 	@Override
114 	public void prerender(final ActionContext context) {
115 		final Action action = context.getAction();
116 		action.prerender();
117 	}
118 
119 }