如何使用Struts2将空字符串转换为int.当应用程序遇到没有值的此参数时,例如从空文本字段,它会抛出以下异常.
java.lang.NoSuchMethodException:
com.XXXXXXXXXXXX.setID([Ljava.lang.String;)
其中ID是整数,URL是:
Something.action?ID=&other=rawr
有没有办法在不使用Integer(类)的情况下执行此操作?我必须写一个类型转换器吗?
解决方法:
如果将id参数声明为Integer struts,则会将空字符串转换为null.
public void setId(Integer id){
...
}
Null and Blank Values
Some properties cannot be set to null.
Primitives like boolean and int cannot
be null. If your action needs to or
will accept null or blank values, use
the object equivalents Boolean and
Integer. Similarly, a blank string “”
cannot be set on a primitive. At the
time of writing, a blank string also
cannot be set on a BigDecimal or
BigInteger. Use server-side validation
to prevent invalid values from being
set on your properties (or handle the
conversion errors appropriately).
标签:java,type-conversion,struts2
来源: https://codeday.me/bug/20190606/1190604.html