1 package jodd.servlet; 2 3 import java.io.IOException; 4 import java.lang.reflect.Method; 5 6 import javax.servlet.RequestDispatcher; 7 import javax.servlet.ServletException; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 import jodd.util.StringUtil; 13 14 85 public abstract class ActionServlet extends HttpServlet { 86 87 public static String parameterActionName = "action"; 88 public static String parameterForwardName = "forward"; 89 public static String parameterRedirectName = "redirect"; 90 91 92 94 ActionController controller = null; 95 96 102 public ActionController getController() { 103 return controller; 104 } 105 106 108 public final static int METHOD_UNKNOWN = 0; 109 public final static int METHOD_GET = 1; 110 public final static int METHOD_POST = 2; 111 112 private int method = METHOD_UNKNOWN; 113 114 115 120 public final int getMethod() { 121 return method; 122 } 123 124 134 protected final void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { 135 method = METHOD_GET; 136 doRequest(request, response); 137 } 138 139 149 protected final void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { 150 method = METHOD_POST; 151 doRequest(request, response); 152 } 153 154 164 public void doRequest(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { 165 } 166 167 184 public String doAction(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { 185 return null; 186 } 187 188 190 202 public String invokeAction(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { 203 String actionName = request.getParameter(parameterActionName); 204 return invokeAction(request, response, actionName); 205 } 206 207 219 public String invokeAction(HttpServletRequest request, HttpServletResponse response, String actionName) throws IOException, ServletException { 220 if (actionName == null) { 221 return null; 222 } 223 try { 224 Method m = this.getClass().getMethod(actionName, new Class[] {HttpServletRequest.class, HttpServletResponse.class}); 225 Object result = m.invoke(this, new Object[] {request, response}); 226 return StringUtil.toString(result); 227 } catch (Exception e) { 228 } 229 return null; 230 } 231 232 233 247 public String invokeExternalAction(HttpServletRequest request, HttpServletResponse response, String actionPath) throws IOException, ServletException { 248 return controller.invokeAction(request, response, actionPath); 249 } 250 251 253 263 protected boolean forward(HttpServletRequest request, HttpServletResponse response, String url) throws IOException, ServletException { 264 RequestDispatcher dispatcher = request.getRequestDispatcher(url); 265 if (dispatcher != null) { 266 dispatcher.forward(request, response); 267 return true; 268 } 269 return false; 270 } 271 272 283 protected boolean forwardParam(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { 284 return forwardParam(request, response, null); 285 } 286 287 301 protected boolean forwardParam(HttpServletRequest request, HttpServletResponse response, String s) throws IOException, ServletException { 302 String paramName = parameterForwardName; 303 if ((s != null) && (s.equals("") == false)) { 304 paramName += "-" + s; 305 } 306 String url = request.getParameter(paramName); 307 if (url != null) { 308 return forward(request, response, url); 309 } 310 return false; 311 } 312 313 315 325 protected void redirect(HttpServletRequest request, HttpServletResponse response, String url) throws IOException { 326 if (url.startsWith("/") == true) { 327 url = request.getContextPath() + url; 328 } 329 response.sendRedirect(url); 330 } 331 332 343 protected boolean redirectParam(HttpServletRequest request, HttpServletResponse response) throws IOException { 344 return redirectParam(request, response, null); 345 } 346 347 360 protected boolean redirectParam(HttpServletRequest request, HttpServletResponse response, String s) throws IOException { 361 String paramName = parameterRedirectName; 362 if ((s != null) && (s.equals("") == false)) { 363 paramName += "-" + s; 364 } 365 String url = request.getParameter(paramName); 366 if (url != null) { 367 redirect(request, response, url); 368 return true; 369 } 370 return false; 371 } 372 373 374 376 385 public String getActionParameter(HttpServletRequest request, String name) { 386 ActionData actionData = (ActionData) request.getAttribute(ActionController.ACTION_DATA); 387 return actionData.getParameter(name); 388 } 389 390 391 400 public String getActionForwardPath(HttpServletRequest request, String name) { 401 ActionData actionData = (ActionData) request.getAttribute(ActionController.ACTION_DATA); 402 return actionData.getForwardPath(name); 403 } 404 405 413 public String getActionMethodName(HttpServletRequest request) { 414 ActionData actionData = (ActionData) request.getAttribute(ActionController.ACTION_DATA); 415 return actionData.getMethod(); 416 } 417 418 419 427 public String getActionType(HttpServletRequest request) { 428 ActionData actionData = (ActionData) request.getAttribute(ActionController.ACTION_DATA); 429 return actionData.getType(); 430 } 431 432 441 public boolean isActionForwardRedirect(HttpServletRequest request, String name) { 442 ActionData actionData = (ActionData) request.getAttribute(ActionController.ACTION_DATA); 443 return actionData.isForwardRedirect(name); 444 } 445 446 447 455 public String getActionPath(HttpServletRequest request) { 456 return (String) request.getAttribute(ActionController.ACTION_PATH); 457 } 458 459 460 } 461 | Popular Tags |