请教:castor MappingException: Could not find the class异常

05-04-27 sea0183

想用castor绑定数据生成xml文件,生成没问题.用castor读时报org.exolab.castor.mapping.MappingException: Could not find the class int异常.请高手不吝赐教!!!!!!!

错误全文如下:

org.exolab.castor.mapping.MappingException: Could not find the class int

at org.exolab.castor.mapping.loader.MappingLoader.createFieldDesc(Unknown Source)

at org.exolab.castor.xml.XMLMappingLoader.createFieldDesc(Unknown Source)

at org.exolab.castor.mapping.loader.MappingLoader.createFieldDescs(Unknown Source)

at org.exolab.castor.mapping.loader.MappingLoader.createDescriptor(Unknown Source)

at org.exolab.castor.xml.XMLMappingLoader.createDescriptor(Unknown Source)

at org.exolab.castor.mapping.loader.MappingLoader.loadMapping(Unknown Source)

at org.exolab.castor.mapping.Mapping.getResolver(Unknown Source)

at org.exolab.castor.mapping.Mapping.getResolver(Unknown Source)

at org.exolab.castor.xml.Unmarshaller.setMapping(Unknown Source)

at cms.Test.testReadNavlinkXML.main(testReadNavlinkXML.java:27)

代码如下:

第一个类:

package cms.Model;

public class MenuModel {
  /*mapping.xml文件中对应的属性*/
  private int id=0;
  private String icon=null;
  private String name=null;
  private String link=null;
  private String datalink=null;

  public MenuModel(){}

  public MenuModel(int id,String icon,String name,String link,String datalink){
    this.id=id;
    this.icon=icon;
    this.link=link;
    this.name=name;
    this.datalink=datalink;
  }

  /*所有属性的访问方法*/
  public int getId() {return id;}
  public String getIcon(){return icon;}
  public String getName(){return name;}
  public String getLink(){return link;}
  public String getDatalink(){return datalink;}
  /*所有属性的操作方法*/
  public void setId(int id){this.id=id;}
  public void setIcon(String icon){this.icon=icon;}
  public void setName(String name){this.name=name;}
  public void setLink(String link){this.link=link;}
  public void setDatalink(String datalink){this.datalink=datalink;}
}

<p>

第二个类:

package cms.Model;

import java.util.*;

public class NavlinkModel {
  /*mapping.xml文件中对应的属性*/
  private Collection menus=new ArrayList();

  public NavlinkModel(){}

  /*所有属性的访问方法*/
  public Collection getMenus(){return this.menus;}

  /*所有属性的操作方法*/
  public void addMenus(MenuModel menu){menus.add(menu);}
}
<p>

mapping.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE databases PUBLIC
"-//EXOLAB/Castor Mapping DTD Version 1.0//EN"
"http://castor.exolab.org/mapping.dtd">
<mapping>
  <class name="cms.Model.NavlinkModel" auto-complete="true">
    <map-to xml="navlink"/>
    <field name="menus" type="cms.Model.MenuModel" collection="collection">
    <bind-xml name="menuas"/>
    </field>
  </class>
  <class name="cms.Model.MenuModel" auto-complete="true">
    <map-to xml="menu"/>
    <field name="id" type="int">
      <bind-xml name="id" node="attribute"/>
    </field>
    <field name="name"/>
    <field name="icon"/>
    <field name="link"/>
    <field name="datalink"/>
  </class>

  <class name="cms.Model.BodyModel">
    <field name="id" type="int">
      <bind-xml name="id" node="attribute"/>
    </field>
    <field name="title" type="string"/>
    <field name="content" type="string"/>
    <field name="date" type="string"/>
    <field name="author" type="string"/>
    <field name="hits" type="string"/>
    <field name="status" type="string"/>
  </class>
</mapping>
<p>

生成的xml文件

<?xml version="1.0" encoding="UTF-8"?>
<navlink-model>
    <menus id="1" xsi:type="java:cms.Model.MenuModel" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <name>name1</name>
        <icon>icon1</icon>
        <link>link1</link>
        <datalink>datalink1</datalink>
    </menus>
    <menus id="2" xsi:type="java:cms.Model.MenuModel" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <name>name2</name>
        <icon>icon2</icon>
        <link>link2</link>
        <datalink>datalink2</datalink>
    </menus>
</navlink-model>

<p>

sea0183
2005-04-27 12:13

错误全文如下:

org.exolab.castor.mapping.MappingException: Could not find the class int

at org.exolab.castor.mapping.loader.MappingLoader.createFieldDesc(Unknown Source)

at org.exolab.castor.xml.XMLMappingLoader.createFieldDesc(Unknown Source)

at org.exolab.castor.mapping.loader.MappingLoader.createFieldDescs(Unknown Source)

at org.exolab.castor.mapping.loader.MappingLoader.createDescriptor(Unknown Source)

at org.exolab.castor.xml.XMLMappingLoader.createDescriptor(Unknown Source)

at org.exolab.castor.mapping.loader.MappingLoader.loadMapping(Unknown Source)

at org.exolab.castor.mapping.Mapping.getResolver(Unknown Source)

at org.exolab.castor.mapping.Mapping.getResolver(Unknown Source)

at org.exolab.castor.xml.Unmarshaller.setMapping(Unknown Source)

at cms.Test.testReadNavlinkXML.main(testReadNavlinkXML.java:27)

代码如下:

第一个类:

package cms.Model;

public class MenuModel {

/*mapping.xml文件中对应的属性*/

private int id=0;

private String icon=null;

private String name=null;

private String link=null;

private String datalink=null;

public MenuModel(){}

public MenuModel(int id,String icon,String name,String link,String datalink){

this.id=id;

this.icon=icon;

this.link=link;

this.name=name;

this.datalink=datalink;

}

/*所有属性的访问方法*/

public int getId() {return id;}

public String getIcon(){return icon;}

public String getName(){return name;}

public String getLink(){return link;}

public String getDatalink(){return datalink;}

/*所有属性的操作方法*/

public void setId(int id){this.id=id;}

public void setIcon(String icon){this.icon=icon;}

public void setName(String name){this.name=name;}

public void setLink(String link){this.link=link;}

public void setDatalink(String datalink){this.datalink=datalink;}

}

第二个类:

package cms.Model;

import java.util.*;

public class NavlinkModel {

/*mapping.xml文件中对应的属性*/

private Collection menus=new ArrayList();

public NavlinkModel(){}

/*所有属性的访问方法*/

public Collection getMenus(){return this.menus;}

/*所有属性的操作方法*/

public void addMenus(MenuModel menu){menus.add(menu);}

}

mapping.xml文件:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE databases PUBLIC

"-//EXOLAB/Castor Mapping DTD Version 1.0//EN"

"http://castor.exolab.org/mapping.dtd">

<mapping>

<class name="cms.Model.NavlinkModel" auto-complete="true">

<map-to xml="navlink"/>

<field name="menus" type="cms.Model.MenuModel" collection="collection">

<bind-xml name="menuas"/>

</field>

</class>

<class name="cms.Model.MenuModel" auto-complete="true">

<map-to xml="menu"/>

<field name="id" type="int">

<bind-xml name="id" node="attribute"/>

</field>

<field name="name"/>

<field name="icon"/>

<field name="link"/>

<field name="datalink"/>

</class>

<class name="cms.Model.BodyModel">

<field name="id" type="int">

<bind-xml name="id" node="attribute"/>

</field>

<field name="title" type="string"/>

<field name="content" type="string"/>

<field name="date" type="string"/>

<field name="author" type="string"/>

<field name="hits" type="string"/>

<field name="status" type="string"/>

</class>

</mapping>

生成的xml文件:

<?xml version="1.0" encoding="UTF-8"?>

<navlink-model>

<menus id="1" xsi:type="java:cms.Model.MenuModel" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<name>name1</name>

<icon>icon1</icon>

<link>link1</link>

<datalink>datalink1</datalink>

</menus>

<menus id="2" xsi:type="java:cms.Model.MenuModel" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<name>name2</name>

<icon>icon2</icon>

<link>link2</link>

<datalink>datalink2</datalink>

</menus>

</navlink-model>

banq
2005-04-27 21:19

Could not find the class int

有关一个整数型int映射错误, 你大概没有正确设置映射文件中有关int对应的class,一般应该是Integer