1.

What Are Model Attributes?

Answer»

Some time it is necessary to get few DATA in many JSPs, and we don’t want to pass them as a part of DTO (data objects). For example, the titles (Mr and Mrs etc). They can be used in many JSPs and tags file, like registration, delivery address etc.

In such CASES, what we do is, create a convenient method in Abstract controllers, and use model attribute annotation for them. In this way they are available from all JSPs directly using model attribute.

These can be ACCESSED directly in JSPs or even tag files. like

Current LANGUAGE : ${current Language}

Model service create method vs new operator

When we try to create a new instances of an item type programmatically, we have two options, using the java way, using new operator or the hybris way, using the model service.

ProductModel product1 = new ProductModel();

ProductModel product2 = modelService.create(ProductModel.class);

The ADVANTAGES of using model service method are below:

  • The model service create method will generate and assign the pk for product object.
  • The create method will initialize the default values, defined in items.xml for mandatory attributes.
  • While calling save all method, the object is already attached to context, and will be saved. While product1 needs to attach explicitly.

So we should always go for model service way in hybris.

Some time it is necessary to get few data in many JSPs, and we don’t want to pass them as a part of DTO (data objects). For example, the titles (Mr and Mrs etc). They can be used in many JSPs and tags file, like registration, delivery address etc.

In such cases, what we do is, create a convenient method in Abstract controllers, and use model attribute annotation for them. In this way they are available from all JSPs directly using model attribute.

These can be accessed directly in JSPs or even tag files. like

Current Language : ${current Language}

Model service create method vs new operator

When we try to create a new instances of an item type programmatically, we have two options, using the java way, using new operator or the hybris way, using the model service.

ProductModel product1 = new ProductModel();

ProductModel product2 = modelService.create(ProductModel.class);

The advantages of using model service method are below:

So we should always go for model service way in hybris.



Discussion

No Comment Found