Difference Between Using GetterUtils and ParamUtils

For example, when to use

GetterUtil.getBoolean() 

and when

 ParamUtil.getBoolean()? 

Are both the same or are they supposed to be used differently according to parameter, variable, etc.? Can you give some examples for both?

+8
methods api liferay
source share
2 answers

Both methods are used to avoid Null Pointer Exception .

GetterUtil internally returns the default type and does the listing too. Therefore, if someone passed a null value, it will return the default value for this type.

Example:
Suppose you have a String value of "true" and you expect it to always be of type boolean . So you are using GetterUtil.getBoolean("true") , which internally casts for boolen and returns the value as boolean-true . If someone skips garbage characters such as "tr" , he will be converted to boolean-false .

As mentioned, ParamUtil does the same processing with request parameters. ParamUtil internally uses GetterUtil to have the above behavior. First, it extracts the parameter (which will always be a string), and then passes it to the GetterUtil.getType() method and, in turn, returns the correct type.

+8
source share

GetterUtil and ParmUtil are different classes.

GetterUtil - Get default values ​​for Java base data types.

ParamUtil should retrieve values ​​(from primitive data types) from HttpReqeust.

Check out the source code here for these two classes here

For GetterUtil http://docs.liferay.com/portal/6.0/javadocs/src-html/com/liferay/portal/kernel/util/GetterUtil.html

For ParamUtil http://docs.liferay.com/portal/5.1/javadocs/portal-kernel/com/liferay/portal/kernel/util/ParamUtil.java.html

+2
source share

All Articles