01: // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
02:
03: package jodd.madvoc.result;
04:
05: import jodd.madvoc.ActionRequest;
06: import jodd.servlet.DispatcherUtil;
07:
08: import javax.servlet.RequestDispatcher;
09: import javax.servlet.http.HttpServletRequest;
10: import javax.servlet.http.HttpServletResponse;
11:
12: /**
13: * Dispatches to a JSP page.
14: *
15: * @see ServletForwardResult
16: */
17: public class ServletDispatcherResult extends ActionResult {
18:
19: protected static String EXTENSION = ".jsp";
20: public static final String NAME = "dispatch";
21:
22: public ServletDispatcherResult() {
23: super (NAME);
24: }
25:
26: /**
27: * Dispatches to the given location. Does its forward via a RequestDispatcher. If the
28: * dispatch fails a 404 error will be sent back in the http response.
29: */
30: @Override
31: public void execute(ActionRequest actionRequest, String resultValue)
32: throws Exception {
33: HttpServletRequest request = actionRequest
34: .getHttpServletRequest();
35: HttpServletResponse response = actionRequest
36: .getHttpServletResponse();
37: String target = resultValue + EXTENSION;
38:
39: RequestDispatcher dispatcher = request
40: .getRequestDispatcher(target);
41: if (dispatcher == null) {
42: response
43: .sendError(404, "Result '" + target + "' not found");
44: return;
45: }
46:
47: // If we're included, then include the view, otherwise do forward.
48: // This allow the page to, for example, set content type.
49: if (DispatcherUtil.isPageIncluded(request, response)) {
50: dispatcher.include(request, response);
51: } else {
52: dispatcher.forward(request, response);
53: }
54: }
55:
56: }
|