/**
*在spring中onSubmit作为普通的提交方法,这样一个控制器的onSubmit只能处理一个请求,
*当我们想只用一个控制器来处理所有简单的表单操作,而在不同的业务对象里进行操作时,应用反射可以做到。
*在前端页面上设置一个隐藏域绑定在command里,在控制器里对注入的业务对象进反射,从command取得相应的标志,
*如果反射出来的方法名与传入的标志相等,就执行此业务操作,如果出现重载的方法,将通过参数的个数和类型来区别。
*/
protected ModelAndView onSubmit(HttpServletRequest request,HttpServletResponse response, Object command, BindException error)throws Exception {
String returnURL = getSuccessView() + ".admin"; // return URL by default
boolean invoked = false; // set true when method has invoked
//反射业务层的方法
ArrayList doo = getDo(objectBA);
// all commandMethods
Method[] commandMethods = command.getClass().getMethods();
try {
Method[] allMethod = objectBA.getClass().getMethods();
for (int i = 0; i < allMethod.length; i++) {
//如果找到有validator这样的方法,就会执行它
if (allMethod[i].getName().equals("validator")) {
if (allMethod[i].getParameterTypes().length == 2) {
allMethod[i].invoke(objectBA, new Object[] { command,
error });
if (error.hasErrors())
return showForm(request, response, error);
break;
}
}
}
Method submit = getSubmit(command);
if (submit != null) {
// invoke getSubmitXxx() method. If user press a button
// submitValue is not null and it length > 0
//取得前端设置的标志,标志表示要执行的业务方法,这个标志就绑定在command里。
String submitValue = (String) submit.invoke(command, new Object[]{});
if (submitValue != null && submitValue.length() != 0) {
Iterator doIter = doo.iterator(); // do methods iterator
String urlAddress = request.getParameter(addressPrefix
+ submitValue);
//循环业务对象的方法
while (!invoked && doIter.hasNext()) { // looking for the
// same do method
Method doMethod = (Method) doIter.next();
//将业务对象的方法与传入的标志(submitValue)进行对比,如果相等,就执行此业务方法
if (isDo(doMethod).equals(toFirstCharToLowerCase(submitValue))) {
//再根据业务对象的方法的参数个数进行反射调用
if (doMethod.getParameterTypes().length == 1) {
doMethod.invoke(objectBA,
new Object[] { command });
invoked = true;
} else if (doMethod.getParameterTypes().length == 2) {
doMethod.invoke(objectBA, new Object[] {
request, command });
invoked = true;
} else if (doMethod.getParameterTypes().length == 3) {
doMethod.invoke(objectBA, new Object[] {
request, command, error });
if (error.hasErrors())
return showForm(request, response, error);
invoked = true;
} else if (doMethod.getParameterTypes().length == 4) {
doMethod.invoke(objectBA, new Object[] {
request, response, command, error });
if (error.hasErrors())
return showForm(request, response, error);
invoked = true;
}
}
}
// check if this button have special URL address?
if (urlAddress != null)
returnURL = urlAddress;
}
}
if (!invoked) {
for (int i = 0; i < allMethod.length; i++)
if (allMethod[i].getName().equals("onSubmit")) {
if (allMethod[i].getParameterTypes().length == 1) {
allMethod[i].invoke(objectBA,
new Object[] { command });
invoked = true;
break;
} else if (allMethod[i].getParameterTypes().length == 2) {
allMethod[i].invoke(objectBA, new Object[] {
request, command });
invoked = true;
break;
}
}
}
Method redirect = getRedirect(command);
if (redirect != null) {
String redirectValue = (String) redirect.invoke(command, new Object[]{});
if (redirectValue != null && redirectValue.length() != 0)
returnURL = redirectValue;
}
// try to locate method getUrlParams() into command class
for (int i = 0; returnURL != null && i < commandMethods.length; i++) {
Method method = commandMethods[i];
if (method.getName().equals(urlParamsName)
&& method.getParameterTypes().length == 0
&& method.getReturnType().getName().equals(
String.class.getName())) {
// if method is exist in command - invoke it and store
// result
String addParams = (String) method.invoke(command,
new Object[] {});
// if additional CGI-parameters exist add it to URL
if (addParams != null && addParams.length() != 0) {
// append additional parameters to URL
if (returnURL.indexOf("?") == -1)
returnURL += "?" + addParams;
else
returnURL += "&" + addParams;
}
break;
}
}
} catch (IllegalAccessException e) {
throw new ControllerException(e);
} catch (ExceptionInInitializerError e) {
throw new ControllerException(e);
} catch (InvocationTargetException e) {
if (e.getTargetException() instanceof MessageException) {
MessageException me = (MessageException) e.getTargetException();
try {
String message = getMessageSourceAccessor().getMessage(
me.getMessageId(), me.getParams());
me.setMessage(message);
throw me;
} catch (NoSuchMessageException ne) {
// if message was not found in property file
// throw controller exception
throw new ControllerException(ne);
}
} else {
throw (Exception) e.getTargetException();
}
}
if (returnURL == null || returnURL.startsWith("return_to_request_page")) {
return showForm(request, response, error);
}
return new ModelAndView(new RedirectView(returnURL));
}
spring控制扩展
最新推荐文章于 2024-07-12 15:32:25 发布