I am trying to get Spring to instantiate a specific object type depending on a certain field in the form but i'm not sure how to do it. I am able to do it properly by parsing the URL as below :
@ModelAttribute("order")
public Order getOrderTypeFromUrl(final HttpServletRequest request)
{
String url = request.getRequestURI();
String type = url.substring(url.lastIndexOf("-") + 1, url.length());
switch (type)
{
case "PRNOrder" : return new PRNOrder();
case "Frequency Scheduled Order" : return new FrequencyScheduledOrder();
}
return null;
}
However, i am looking to change my URL to no longer contain the order type the user is adding. I would like for this method to have access to the fields that were in the form when it was submitted to the controller but i'm not sure what parameter to add to my method to achieve this. The request object's parameters and attributes do not contain the form data.
Thanks
Turns out this works fine PROVIDED YOU SET AN ID AND NAME TO YOUR HIDDEN TEXT FIELD CONTAINING THE ORDER TYPE !!!! (time to stop coding for today!) :
@ModelAttribute("order")
public Order getOrderTypeFromRequest(final HttpServletRequest request, final Model model)
{
try
{
String orderType = request.getParameter("type");
switch (orderType)
{
case "PRNOrder" : return new PRNOrder();
case "frequencyScheduledOrder" : return new FrequencyScheduledOrder();
}
}
catch (Exception ex)
{
return null;
}
return null;
}