Even if springmvc provides @ModelAttribute, it could only bind name that not start with root variable name, for instance:


class OrderLog
    private Client client;
    private Restaurant restaurant;
class Client
    private Integer cid;
class Restaurant
    private Integer rid;

Action class:

@RequestMapping(value = "/order")
public class OrderAction

    private static final Logger LOG = LoggerFactory.getLogger(OrderAction.class.getName());

    private OrderLogService orderLogService;

    private ClientService clientService;

    private RestaurantService restaurantService;

    @RequestMapping(method = RequestMethod.POST)
    public Object create(@FormModel("orderLog") OrderLog orderLog)
        return new Message("SUCCESS", "Ordered");

Without any specified assistance, one could only bind variables below onto OrderLog object.
curl -d"restaurant.rid=1&client.cid=2" "localhost:8080/order"
It did not includes root name: orderLog, makes it not easy and cosy enough to be binded onto a specified model, at the meantime, makes it harder for maintemance.


After Googling, I found a great solution and assist.
By adding @formModel annotation and related resolving methods, springmvc now could tackle with it with great convenience.
While in integration, you need to register the customized arguments resolver into springmvc framework by add them in springmvc-servlet.xml to prevent potential risks.

<mvc:annotation-driven  content-negotiation-manager="contentNegotiationManager">
        <bean class="rugal.common.springmvc.method.annotation.RequestJsonParamMethodArgumentResolver"/>
        <bean class="rugal.common.springmvc.method.annotation.FormModelMethodArgumentResolver"/>

Now let us use:
curl -d"orderLog.restaurant.rid=1&orderLog.client.cid=2" "localhost:8080/order"

Everything runs smoothly! great!


09 April 2014