Model Attribute in Spring MVC & Liferay MVC

thumbnail
Aravinth Kumar, modified 5 Years ago. Regular Member Posts: 152 Join Date: 6/26/13 Recent Posts
Hi All,I m using Liferay DXP.
In Liferay MVC, Is there any way to fetch all the form fields in controller as a single object same as using Model Attribute in Spring MVC ?I am using aui:modelContext in the jsp to prepopulate already.
Thanks in Advance.

Regards,
Aravinth
thumbnail
Mohammed Yasin, modified 5 Years ago. Liferay Master Posts: 593 Join Date: 8/8/14 Recent Posts
Hi,
You can use ServiceContext Object  as  single object for all your form fields, You can fetch your form field 
ServiceContext serviceContext = null;
    try {
        serviceContext = ServiceContextFactory.getInstance(renderRequest);
    } catch (PortalException e) {
        _log.error(e);
    }
    serviceContext.getAttribute(formField);

thumbnail
Aravinth Kumar, modified 5 Years ago. Regular Member Posts: 152 Join Date: 6/26/13 Recent Posts
Hi Yasin,Thanks for your reply.  I don't want to set the values to object manually. Hope serviceContext way will make us to fetch the form values and set it to the object manually. Kindly share any sample code if you have. 
thumbnail
Mohammed Yasin, modified 5 Years ago. Liferay Master Posts: 593 Join Date: 8/8/14 Recent Posts
Hi,
ServiceContext will not build/generate model object , but will provided single object from where all form values can be fetched as  attributes .For generating model object may be you can use a model constructor with service context as parameter where it will generate object for you .
class xyz {
private  long field1;
  public modelConstructor(ServiceContext serviceContext) {
      this.field1 =  GetterUtil.getLong(serviceContext.getAttribute("formField1"));
}
}