a lot of people seem to be interested in this and there was a discussion at
http://forum.springframework.org/showthread.php?t=11086
To summarise though the MultiActionController will handle binds to a command object but doesnt really help with the error handling.
i have written a couple of classes for doing some similar stuff. Although this may not be what you are after hope this can help you develop something that works for you
Code:
public class SubmitParameterPropertiesMethodNameResolver implements
MethodNameResolver {
private String defaultMethodName;
private Properties mappings;
/**
* Set URL to method name mappings from a Properties object.
* @param mappings properties with URL as key and method name as value
*/
public void setMappings(Properties mappings) {
this.mappings = mappings;
}
/**
* @param defaultMethodName The defaultMethodName to set.
*/
public void setDefaultMethodName(String defaultMethodName) {
this.defaultMethodName = defaultMethodName;
}
public void afterPropertiesSet() {
if (this.mappings == null || this.mappings.isEmpty()) {
throw new IllegalArgumentException("'mappings' property is required");
}
}
public String getHandlerMethodName(HttpServletRequest request)
throws NoSuchRequestHandlingMethodException {
for (Iterator it = this.mappings.keySet().iterator(); it.hasNext();) {
String submitParamter = (String) it.next();
if (WebUtils.hasSubmitParameter(request, submitParamter)) {
return (String) this.mappings.get(submitParamter);
}
}
return defaultMethodName;
}
and
Code:
public abstract class SimpleMultiActionFormController extends SimpleFormController {
private MethodNameResolver methodNameResolver = new SubmitParameterPropertiesMethodNameResolver();
public final void setMethodNameResolver(MethodNameResolver methodNameResolver) {
this.methodNameResolver = methodNameResolver;
}
public final MethodNameResolver getMethodNameResolver() {
return this.methodNameResolver;
}
protected ModelAndView processFormSubmission(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception {
if (errors.hasErrors() || isFormChangeRequest(request)) {
if (logger.isDebugEnabled()) {
logger.debug("Data binding errors: " + errors.getErrorCount());
}
return showForm(request, response, errors);
}
else {
String methodName = this.methodNameResolver.getHandlerMethodName(request);
Method m = (Method) this.getClass().getMethod(methodName,
new Class[] {HttpServletRequest.class,
Object.class,
BindException.class});
if (m == null) {
throw new NoSuchRequestHandlingMethodException(methodName, getClass());
}
List params = new ArrayList(3);
params.add(request);
params.add(command);
params.add(errors);
return (ModelAndView) m.invoke(this, params.toArray(new Object[params.size()]));
}
}
}
an example is
Code:
<bean id="submitActionParamResolver" class="com.codebitches.springframework.web.servlet.mvc.multiaction.SubmitParameterPropertiesMethodNameResolver">
<property name="mappings">
<props>
<prop key="_addSubject">addSubject</prop>
<prop key="_finish">finalSubmit</prop>
</props>
</property>
<property name="defaultMethodName"><value>finalSubmit</value></property>
</bean>
<bean id="createEditItemFormController" class="com.codebitches.springxmldb.examples.tilesnews.web.spring.CreateEditItemFormController">
<property name="methodNameResolver"><ref bean="submitActionParamResolver"/></property>
<property name="sessionForm"><value>true</value></property>
<property name="successView"><value>createEditItemConfirmation</value></property>
<property name="commandName"><value>item</value></property>
<property name="formView"><value>createEditItemForm</value></property>
</bean>
Code:
public class CreateEditItemFormController extends SimpleMultiActionFormController {
private ITilesNewsCMS cms;
private static Log log = LogFactory.getLog(CreateEditItemFormController.class);
/**
* @param cms The cms to set.
*/
public void setCms(ITilesNewsCMS cms) {
this.cms = cms;
}
public CreateEditItemFormController() {
}
protected Object formBackingObject(HttpServletRequest request) throws ModelAndViewDefiningException {
String id = request.getParameter("itemId");
Item item = new Item();
if (id != null && !id.equals("")) {
item = cms.getItem(id);
}
return item;
}
public ModelAndView addSubject(HttpServletRequest request, Object command, BindException errors) throws Exception {
Item item = (Item) command;
Collection subs = item.getMetadata().getSubjects();
subs.add(new ItemSubject());
return this.showForm(request, errors, getFormView());
}
public ModelAndView finalSubmit(HttpServletRequest request, Object command, BindException errors) throws Exception {
Item item = (Item) command;
item = cms.createOrUpdateItem(item);
Map model = new HashMap();
model.put("item", item);
return new ModelAndView(getSuccessView(), model);
}
/* (non-Javadoc)
* @see org.springframework.web.servlet.mvc.SimpleFormController#referenceData(javax.servlet.http.HttpServletRequest)
*/
protected Map referenceData(HttpServletRequest request) throws Exception {
return super.referenceData(request);
}