I'm making changes in a very old application. which is using Spring MVC 4.
I need to post data from a form:form tag within in JSP for Spring controller. UI is fixed and I can only make changes at server side. Based upon a specific field in my submitted Form, I want to have the correct child Object instance in my Controller Handler method parameter.
For example,
class Payment {...}
class CardPayment extends Payment{...}
class CheckPayment extends Payment{...}
In UI form, there will be a input value called paymentType. payment will be commandObject or ModelAttribute
I want my @PostMapping Controller to have the correct child object in the argument. I don't want to manually instantiate it in controller code.
@PostMapping
public ModelAndView doSomePay(@ModelAttribute("payment") Payment paymentInput, BindingResult result){
Now I want this paymentInput
object above to be of type CardPayment or checkPayment.
I tried to create a @initBinder and WebDatabinder
but in reality I have close to 10 subclasses, do i need to create "Editor" for all those ?
If yes,whats the best way to create the propertyEditor short and quick
@InitBinder
public void initBinder(WebDataBinder binder, HttpServletRequest request) {
String paymentType = request.getParameter("paymentType");
PropertyEditor productEditor;
//somehow I can find the correct child class that I need to see, for example CardPatment.class , Now how to do following
binder.set(Product.class,productEditor); ???
}