java - Spring MVC ModelAttribute as Interface -
using spring mvc @controller, how have @requestmapping endpoint have @modelattribute declared interface?
i want have 3 different forms being posted mapping underlying classes of interface type.
so example, can have 3 different form objects action following:
@requestmapping(path="/dosomething", method=requestmethod.post) public string dosomething(objectinterface forminfo) { ... }
(where objectinterface interface, not concrete class.)
figured out. write , register custom handlermethodargumentresolver. below core code. need figure out concrete bean class pass webdatabinderfactory. controller can written accept interface , provided concrete implementing bean behind interface.
public class messageresolvertest implements handlermethodargumentresolver { public boolean supportsparameter(methodparameter methodparameter) { return methodparameter.getparametertype().equals(<interface>.class); } public object resolveargument(methodparameter methodparameter, modelandviewcontainer modelandviewcontainer, nativewebrequest nativewebrequest, webdatabinderfactory webdatabinderfactory) throws exception { string name = modelfactory.getnameforparameter(methodparameter); webdatabinder webdatabinder = webdatabinderfactory.createbinder(nativewebrequest, new <concretebean>(), name); mutablepropertyvalues mutablepropertyvalues = new mutablepropertyvalues(nativewebrequest.getparametermap()); webdatabinder.bind(mutablepropertyvalues); return webdatabinder.getbindingresult().gettarget(); } }
Comments
Post a Comment