Xpage应用程序中的Java类在技术上有效,但第二次将字符串解析为Long会导致日志中出现大量错误消息。我没有开发这个应用程序,只是试图维护它。我理解第一个try-catch块内部发生的所有机制,并理解第一个try-catch块中变量“retval”的范围。但我不明白为什么开发人员创建了第二个try-catch块并再次解析名为“value”的参数,此时此参数为空,这就是造成问题的原因。我试图修改retval变量的范围并调整代码以具有一个try-catch块但它会导致错误。为什么要处理第二个解析,如何将其作为空值停止?
public Object getAsObject(FacesContext context, UIComponent component,
String value) {
// TODO Auto-generated method stub
UIOutputLabel label = (UIOutputLabel) FacesUtilExtsn
.getLabelFor(component);
UIInput uiinput = (UIInput) component;
if (UiValidationTools.doValidation((UIInputText)component,
context)) {
String msg = UiValidationTools.getFriendlyname((UIInput)
component);
try {
Long retval = Long.parseLong(value);
if (value.isEmpty()) {
if(msg!=null){
msg+=" - Value must be a valid number eg 3";
}else{
msg="Value must be a valid number eg 3";
}
uiinput.setValid(false);
FacesMessage message = new FacesMessage(
msg);
throw new ConverterException(message);
}
Integer max = UiValidationTools.getMax((UIInputText)
component);
Integer min = UiValidationTools.getMin((UIInputText)
component);
if (min != null) {
if (retval < min.doubleValue()) {
if(msg!=null){
msg+=" - Value must be between " + min + " and " + max;
}else{
msg="Value must be between " + min + " and " + max;
}
uiinput.setValid(false);
FacesMessage message = new FacesMessage(
msg);
throw new ConverterException(message);
}
}
if (max != null) {
if (retval > max.doubleValue()) {
if(msg!=null){
msg+=" - Value must be between " + min + " and " + max;
}else{
msg="Value must be between " + min + " and " + max;
}
uiinput.setValid(false);
FacesMessage message = new FacesMessage(
msg);
throw new ConverterException(message);
}
}
// At this point the the parameter named "value" has
//content as inside scope
return retval;
} catch (NumberFormatException nume) {
System.out.println("Getting caught here...");
uiinput.setValid(false);
if(msg!=null){
msg+=" - Not a valid number: "
+ nume.getMessage();
}else{
msg="Not a valid number: "
+ nume.getMessage();
}
FacesMessage message = new FacesMessage(msg);
nume.printStackTrace();
throw new ConverterException(message);
}
}else{
System.out.println("Value is "+value);
}
try{
// At this point the parameter named "value" is empty as outside scope
// and this is what is causing issues as it is empty
Long retval = Long.parseLong(value);
return retval;
}catch(NumberFormatException numfe){
System.out.println("Our error has been caught*!*!*!*!*!*!*!*!");
numfe.printStackTrace();
return new Long(0);
}
}