1 24 package org.riotfamily.forms.controller; 25 26 import java.lang.reflect.Method ; 27 28 import javax.servlet.http.HttpServletRequest ; 29 import javax.servlet.http.HttpServletResponse ; 30 31 import org.riotfamily.forms.Form; 32 import org.springframework.util.Assert; 33 import org.springframework.web.servlet.ModelAndView; 34 import org.springframework.web.servlet.mvc.Controller; 35 36 public class NamedMethodHandler implements FormSubmissionHandler { 37 38 private static final Class [] HANDLER_PARAM_TYPES = new Class [] { 39 Form.class, HttpServletRequest .class, HttpServletResponse .class 40 }; 41 42 private Controller controller; 43 44 private Method handlerMethod; 45 46 public NamedMethodHandler(Controller controller, String methodName) { 47 this.controller = controller; 48 try { 49 handlerMethod = controller.getClass().getMethod( 50 methodName, HANDLER_PARAM_TYPES); 51 } 52 catch (SecurityException e) { 53 throw new RuntimeException (e); 54 } 55 catch (NoSuchMethodException e) { 56 throw new RuntimeException (e); 57 } 58 59 Assert.isTrue(ModelAndView.class.isAssignableFrom( 60 handlerMethod.getReturnType()), 61 "Handler method must return a ModelAndView"); 62 } 63 64 public ModelAndView handleFormSubmission(Form form, 65 HttpServletRequest request, HttpServletResponse response) 66 throws Exception { 67 68 Object [] args = new Object [] {form, request, response}; 69 return (ModelAndView) handlerMethod.invoke(controller, args); 70 } 71 72 } 73 | Popular Tags |