Forums de discussion
The reason to use BeanParamUtil.getLong?
旻 吴, modifié il y a 2 mois.
The reason to use BeanParamUtil.getLong?
Junior Member Publications: 56 Date d'inscription: 17/05/17 Publications récentes
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:
Is there any special reason of using BeanParamUtil instead of the getter method? Thank you very much.
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();
David H Nebinger, modifié il y a 6 années.
RE: The reason to use BeanParamUtil.getLong?
Liferay Legend Publications: 14919 Date d'inscription: 02/09/06 Publications récentes
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!
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!
Amos Fong, modifié il y a 6 années.
RE: The reason to use BeanParamUtil.getLong?
Liferay Legend Publications: 2047 Date d'inscription: 07/10/08 Publications récentes
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.
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.
David H Nebinger, modifié il y a 6 années.
RE: The reason to use BeanParamUtil.getLong?
Liferay Legend Publications: 14919 Date d'inscription: 02/09/06 Publications récentesAmos Fong:
David, I think you have it backwards.
Wouldn't necessarily be the first time
Come meet me at Devcon 2017 or 2017 LSNA!
旻 吴, modifié il y a 2 mois.
RE: The reason to use BeanParamUtil.getLong?
Junior Member Publications: 56 Date d'inscription: 17/05/17 Publications récentesDavid 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.