Commons.beanutils.ConversionException:No Value specified solution

Source: Internet
Author: User
Tags dateformat log log


This exception occurs when a non-built-in object such as time is used, and if the object is null. The easiest way to do this is to ensure that non-built-in objects are not NULL.



There are other experts to modify the Commons package related to the source of the solution, also given:



<1> Time Solutions:
Programme I:
Problem solving:
Read his source program, found that the date java.sql.date mainly with the Org.apache.commons.beanutils.converters.SqlDateConverter class for the transfer:
Just modify the code below.


Public Sqldateconverter (Object DefaultValue) {
This.defaultvalue = defaultvalue;
This.usedefault = true; Originally false;
}





Public Sqldateconverter () {
This.defaultvalue = null;
This.usedefault = true;   Originally false; I don't remember that it was false, anyway, it's OK to change to true
}


Programme II:



1, Actionform inside has the java.util.Date type, but the page did not enter, encountered the error.



2, the use of Beanutils.copyproperties (Actionform, Model), because the actionform inside the java.util.Date that field null and error occurred.



Both of these problems are related to the Beanutils.copyproperties method, so the solution is to solve the beanutils problem. Since this method of beanutils does not have built-in ava.util.Date type conversion support, the Java.util.Date type is first switched to java.sql.Date type.



For 1, use the following code to resolve the bridging mode:




Private Java.sql.Date Date;





Public String Getdatedisplay () {
if (this.date==null) return null;
DateFormat dateformatter = dateformat.getdateinstance (Dateformat.default,this.getlocale ());
Return Dateformatter.format (this.date);
}





public void Setdatedisplay (String dispaly) {
if (dispaly==null| | Display.trim (). Equals ("")) {
This.date = null;
} else {
DateFormat dateformatter = dateformat.getdateinstance (Dateformat.default,this.getlocale ());
This.date = Dateformatter.parse (display);
}
}



For question 2nd, the method is to implement the Convert method of your own Sqldateconverter and Sqltimestampconverter classes:




Package org.study.common.util;
Import Java.sql.Date;

Import org.apache.commons.beanutils.ConversionException;
Import Org.apache.commons.beanutils.Converter;
Import org.apache.commons.logging.*;

Public final class Sqldateconverter implements Converter {

Private log log = Logfactory.getlog (This.getclass ());

Public Sqldateconverter () {

This.defaultvalue = null;
This.usedefault = false;
}

Public Sqldateconverter (Object DefaultValue) {

This.defaultvalue = defaultvalue;
This.usedefault = true;
}

Private Object defaultvalue = null;

Private Boolean usedefault = true;

public object Convert (Class type, Object value) {


if (value = NULL | | "". Equals (value)) {
if (Usedefault) {
return (defaultvalue);
} else {
throw new Conversionexception ("No value specified");
}
}

if (value instanceof Date) {
return (value);
}

        try {
             return (date.valueof (value.tostring ());
       } catch (Exception e) {
          log.error ("Convert error ocured.", e);
            if (usedefault) {
                 return (defaultvalue);
           } else {
                 throw new Conversionexception (e);
           }
       }
   }
}



Similar to Sqltimestampconverter, and then add the following code in Baseaction
static {
Convertutils.register (new Sqldateconverter (null), Java.sql.Date.class);
Convertutils.register (new Sqltimestampconverter (null), Java.sql.Timestamp.class);
}



In solving this problem, I found a 3rd place Bean Mapper Tool Dozer is very good, it is based on beanutils development of class copy and attribute conversion open source package.



3, found that the following code actually reported bean error not defined in any scope.




<logic:messagesPresent>
<bean:write name= "Error"/>
</logic:messagesPresent>


After careful dispatch, the problem is that the error message is saved in the action:
Errors.add (Globals.error_key, New Actionmessage (
"Error.savestudent", Student.getname (), Ex.getmessage ());
, you forget to define error.savestudent in the resource file, causing the error message to not actually be generated, and of course there is no bean: cold ah ...



4, Eclipse PropertyEditor every time the Chinese resources file into garbled. Treatment methods:
1) Set up Java propertiy files for GB2312 encoding in window-->preference-->general-->content types.
2) use Ant to do native2ascii conversion:

<?xml version= "1.0" encoding= "GB2312"?>





<project basedir= "." >
<property name= "App.home" value= "."/>
<property name= "Src.home" value= "${app.home}/src"/>
<property name= "Web.home" value= "${app.home}/webroot"/>
<property name= "Lib.home" value= "${web.home}/web-inf/lib"/>
<property name= "Classes.home" value= "${web.home}/web-inf/classes"/>
<property name= "Resources.package" value= "Java/resources"/>
<property name= "resources.src" value= "${src.home}/${resources.package}"/>
<property name= "Resources.dest" value= "${classes.home}/${resources.package}"/>





<path id= "Compile.classpath"
   <pathelement location= "${lib.home}"/>
    <pathelement path= "${lib.home}"/>
</path>





<!--The main completion of the resource file encoding file format conversion and copy to the target resource. -->
<target name= "Resources" >
<delete includeemptydirs= "true" quiet= "true" >
<fileset dir= "${resources.dest}" >
<include name= "*.properties"/>
</fileset>
</delete>
<mkdir dir= "${resources.dest}"/>
<native2ascii encoding= "GB2312" src= "${resources.src}" dest= ${resources.dest} "includes=" **/*_zh_cn.properties "/>
<copy todir= "${resources.dest}" >
<fileset dir= "${resources.src}" >
<include name= "*.properties"/>
<exclude name= "*.zh_cn.properties"/>
</fileset>
</copy>
</target>





</project>





<2> Large Segment Solution:
When you make a property copy the value of the type BigDecimal property is null, so this error occurs.
Because if you look at Beanutils's source code, you know that the corresponding converter class for the BigDecimal type of attribute Bigdecimalconverter when invoking the Convert method is judged as follows:




Java Code

public object Convert (Class type, Object value) {
[Color= #FF6600]
if (value = = null) {
if (Usedefault) {
return (defaultvalue);
} else {
throw new Conversionexception ("No value specified");
}
}[/color]





        if (value instanceof BigDecimal) {
             return (value);
       }





try {
Return (New BigDecimal (Value.tostring ()));
catch (Exception e) {
if (Usedefault) {
return (defaultvalue);
} else {
throw new Conversionexception (e);
}
}





}



The red part is the reason for this error, the first copy of the property value of the object is null, and Usedefault is False (this is defined when the Bigdecimalconverter class is instantiated by default, as follows:)




Java Code


Public Bigdecimalconverter () { This.defaultvalue = null;
This.usedefault = false;

}



So it will create the mistakes you have encountered.



You can avoid your errors by assigning values to the BigDecimal type attributes in the Bbean object and then copying the properties.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.