Foros de discusión

The reason to use BeanParamUtil.getLong?

旻 吴, modificado hace 2 meses.

The reason to use BeanParamUtil.getLong?

Junior Member Mensajes: 56 Fecha de incorporación: 17/05/17 Mensajes recientes
Hello everyone. I am learning to write jsp files in liferay 7.0. I mentioned that the attributes of models are got by BeanParamUtil but not getter methods of the models. For example, in view_record.jsp of Dynamic Data List Web portlet, it can be found:
DDLRecord record = (DDLRecord)request.getAttribute(DDLWebKeys.DYNAMIC_DATA_LISTS_RECORD);
long recordId = BeanParamUtil.getLong(record, request, "recordId");

Is there any special reason of using BeanParamUtil instead of the getter method? Thank you very much.
long recordId = record.getRecordId();
thumbnail
David H Nebinger, modificado hace 6 años.

RE: The reason to use BeanParamUtil.getLong?

Liferay Legend Mensajes: 14919 Fecha de incorporación: 2/09/06 Mensajes recientes
All of the derivations of GetterUtil.getXxx() are in there to support safe conversion of typically string data to a target type.

In this example, the second argument is the request and it would have a param value as a string.

So BeanParamUtil.getLong() is going to try to use the value from the record field, but record might be null or not set. In that case it falls back to the request parameter check and must possibly convert a string or a null into a regular long value.

Now sure you can code all of these various checks yourself, no big deal. But when you start picturing what impact this would have to do all of this manually throughout the portal code, it starts to become clear why you would want to use one of these methods instead.











Come meet me at Devcon 2017 or 2017 LSNA!
thumbnail
Amos Fong, modificado hace 6 años.

RE: The reason to use BeanParamUtil.getLong?

Liferay Legend Mensajes: 2047 Fecha de incorporación: 7/10/08 Mensajes recientes
David, I think you have it backwards. In BeanParamUtil, the request value takes precedence, and if the request value is null, it falls back on the bean's value.

I believe the main use case is if you are editing a bean. Validation may fail and in that case the page is reloaded and when it's reloaded, the request values should take precedence over the original values.
thumbnail
David H Nebinger, modificado hace 6 años.

RE: The reason to use BeanParamUtil.getLong?

Liferay Legend Mensajes: 14919 Fecha de incorporación: 2/09/06 Mensajes recientes
Amos Fong:
David, I think you have it backwards.


Wouldn't necessarily be the first time emoticon









Come meet me at Devcon 2017 or 2017 LSNA!
旻 吴, modificado hace 2 meses.

RE: The reason to use BeanParamUtil.getLong?

Junior Member Mensajes: 56 Fecha de incorporación: 17/05/17 Mensajes recientes
David H Nebinger:
All of the derivations of GetterUtil.getXxx() ...

Amos Fong:
David, I think you have it backwards. ....

Thank you very much! I've got the ideal of BeamParamUtil now.