以上就是给各位分享JavaJRBeanCollectionDataSource:如何显示来自JavaBean的java.util.List中的数据?,其中也会对java怎么显示数据库的数据进行解释,同
以上就是给各位分享Java JRBeanCollectionDataSource:如何显示来自JavaBean的java.util.List中的数据?,其中也会对java怎么显示数据库的数据进行解释,同时本文还将给你拓展03-封装BeanUtil工具类(javabean转map和map转javabean对象)、android java.lang.IllegalStateException: System services not available to Activities before onCreate、AnnotationTransactionAttributeSource is only available on Java 1.5 and higher、Cause: java.lang.IllegalArgumentException: invalid comparison: java.util.ArrayList and java.lang....等相关知识,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:- Java JRBeanCollectionDataSource:如何显示来自JavaBean的java.util.List中的数据?(java怎么显示数据库的数据)
- 03-封装BeanUtil工具类(javabean转map和map转javabean对象)
- android java.lang.IllegalStateException: System services not available to Activities before onCreate
- AnnotationTransactionAttributeSource is only available on Java 1.5 and higher
- Cause: java.lang.IllegalArgumentException: invalid comparison: java.util.ArrayList and java.lang....
Java JRBeanCollectionDataSource:如何显示来自JavaBean的java.util.List中的数据?(java怎么显示数据库的数据)
我的JavaBean
包含java.util.List
。
Userinfo { private String username; private String password; List<Address> listAddress;}
如何在明细栏中显示此列表的数据?
答案1
小编典典这是工作示例。
该样本的关键点:
- 使用的_THIS表达;
- 使用列表(JR:列表) 在组分详细频带
用于生成报告的代码段:
public static void testBuildPdf() { try { Map<String, Object> params = new HashMap<String, Object>(); JasperReport jasperReport = JasperCompileManager.compileReport(reportSource); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, getDataSource()); JasperExportManager.exportReportToPdfFile(jasperPrint, outputFileName); } catch (Exception e) { e.printStackTrace(); }}private static JRDataSource getDataSource() { Collection<BeanWithList> coll = new ArrayList<BeanWithList>(); coll.add(new BeanWithList(Arrays.asList("London", "Paris"), 1)); coll.add(new BeanWithList(Arrays.asList("London", "Madrid", "Moscow"), 2)); coll.add(new BeanWithList(Arrays.asList("Rome"), 3)); return new JRBeanCollectionDataSource(coll);}
JavaBean代码:
public class BeanWithList { // The member''s name can be any. The JR engine is using public getter for extracting field''s value private List<String> cities; private Integer id; public BeanWithList(List<String> cities, Integer id) { this.cities = cities; this.id = id; } // getter should be public public List<String> getCities() { return this.cities; } public Integer getId() { return this.id; }}
jrxml文件:
<?xml version="1.0" encoding="UTF-8"?><jasperReport ...> <subDataset name="dataset1"> <field name="city"> <fieldDescription><![CDATA[_THIS]]></fieldDescription> </field> </subDataset> <field name="id"/> <field name="cities"/> <title> <band height="103" splitType="Stretch"> <staticText> <reportElement x="138" y="28" width="258" height="20"/> <textElement textAlignment="Center" verticalAlignment="Middle"> <font isBold="true" isItalic="true"/> </textElement> <text><![CDATA[Bean with List sample]]></text> </staticText> </band> </title> <columnHeader> <band height="20"> <staticText> <reportElement x="0" y="0" width="100" height="20"/> <box> <topPen lineWidth="1.0"/> <leftPen lineWidth="1.0"/> <bottomPen lineWidth="1.0"/> <rightPen lineWidth="1.0"/> </box> <textElement textAlignment="Center" verticalAlignment="Middle"> <font isBold="true" isItalic="true" isUnderline="false"/> </textElement> <text><![CDATA[Id]]></text> </staticText> <staticText> <reportElement x="100" y="0" width="100" height="20"/> <box> <topPen lineWidth="1.0"/> <leftPen lineWidth="1.0"/> <bottomPen lineWidth="1.0"/> <rightPen lineWidth="1.0"/> </box> <textElement textAlignment="Center" verticalAlignment="Middle"> <font isBold="true" isItalic="true" isUnderline="false"/> </textElement> <text><![CDATA[City name]]></text> </staticText> </band> </columnHeader> <detail> <band height="20" splitType="Stretch"> <textField> <reportElement stretchType="RelativeToTallestObject" x="0" y="0" width="100" height="20"/> <box leftPadding="10"> <topPen lineWidth="1.0"/> <leftPen lineWidth="1.0"/> <bottomPen lineWidth="1.0"/> <rightPen lineWidth="1.0"/> </box> <textElement/> <textFieldExpression><![CDATA[$F{id}]]></textFieldExpression> </textField> <componentElement> <reportElement x="100" y="0" width="400" height="20"/> <jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical"> <datasetRun subDataset="dataset1"> <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{cities})]]></dataSourceExpression> </datasetRun> <jr:listContents height="20" width="400"> <textField> <reportElement x="0" y="0" width="100" height="20"/> <box leftPadding="10"> <topPen lineWidth="1.0"/> <leftPen lineWidth="1.0"/> <bottomPen lineWidth="1.0"/> <rightPen lineWidth="1.0"/> </box> <textElement/> <textFieldExpression><![CDATA[$F{city}]]></textFieldExpression> </textField> </jr:listContents> </jr:list> </componentElement> </band> </detail></jasperReport>
03-封装BeanUtil工具类(javabean转map和map转javabean对象)
package com.oa.test;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.util.HashMap;
import java.util.Map;
public class BeanUtil {
/**
* 将JavaBean对象封装到Map集合当中
* @param bean
* @return
* @throws Exception
*/
public static Map<String, Object> bean2map(Object bean) throws Exception
{
//创建Map集合对象
Map<String,Object> map=new HashMap<String, Object>();
//获取对象字节码信息,不要Object的属性
BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass(),Object.class);
//获取bean对象中的所有属性
PropertyDescriptor[] list = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor pd : list) {
String key = pd.getName();//获取属性名
Object value = pd.getReadMethod().invoke(bean);//调用getter()方法,获取内容
map.put(key, value);//增加到map集合当中
}
return map;
}
/**
* 将Map集合中的数据封装到JavaBean对象中
* @param map 集合
* @param classType 封装javabean对象
* @throws Exception
*/
public static <T> T map2bean(Map<String, Object> map,Class<T> classType) throws Exception
{
//采用反射动态创建对象
T obj = classType.newInstance();
//获取对象字节码信息,不要Object的属性
BeanInfo beanInfo = Introspector.getBeanInfo(classType,Object.class);
//获取bean对象中的所有属性
PropertyDescriptor[] list = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor pd : list) {
String key = pd.getName(); //获取属性名
Object value=map.get(key); //获取属性值
pd.getWriteMethod().invoke(obj, value);//调用属性setter()方法,设置到javabean对象当中
}
return obj;
}
}
测试代码如下
package com.oa.test;
import java.util.Map;
import com.oa.domain.User;
public class Demo3 {
/**
* 刘诗华
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// User(id=28, userName=刘诗华, password=123456)
User user=new User(28,"刘诗华","123456");
//将JavaBean集合转换成Map集合
Map<String, Object> m = BeanUtil.bean2map(user);
System.out.println(m); //{id=28, userName=刘诗华, password=123456}
//将Map集合转换JavaBean对象
User o = BeanUtil.map2bean(m, User.class);
System.out.println(o); //User(id=28, userName=刘诗华, password=123456)
}
}
android java.lang.IllegalStateException: System services not available to Activities before onCreate
java.lang.IllegalStateException: System services not available to Activities before onCreate()出现这种情况一般是因为 通过使用new关键字实例化Activity类,这样是错误的。因为Activity是系统通过ActivityManagerService创建管理的。 new之后,创建了对象,但没有创建这个Activity,要知道Activity的对象和在Android中真正表示的东西,不是一样的。 对象创建完成后,需要系统对其进行一系列的init和管理。在这个创建的过程中,会回调Acitivity中的onCreate方法,这个方法调用之后,才能说明Activity是创建完成了。
所以就不能使用new关键字实例化Activity对象,要通过startActivity的方式,实例化这个Activity对象。
原文链接: http://blog.csdn.net/centralperk/article/details/7496877
AnnotationTransactionAttributeSource is only available on Java 1.5 and higher
前言:
在eclipse中用到spring2.0的web项目,启动elipse自带的tomcat7,tomcat7报错如下:
AnnotationTransactionAttributeSource is only available on Java 1.5 and higher
出错原因:
因为spring core org.springframework.core.JdkVersion.java不支持Jdk1.8。支持的Jdk版本分别为1.3(default),1.4, 1.5, 1.6 and 1.7,如果检测到Tomcat配置的不是1.4, 1.5, 1.6, 1.7,那么就认为是1.3,所以会报这个错。
解决方案:
解决方法是将Jdk1.8降到1.7,或者升级spring版本,使其支持Jdk1.8。
我尝试将Jdk1.8降到1.7,亲测可行,但要注意几个地方都要改。(仅作用于elipse,其他IDE大体思路雷同)
1、右键该项目->Build Pah->Configure Build Path->java Build Path->选中当前jdk点击编辑(具体如下图)
2、点击alternate JRE->选择jdk1.7(具体如下图)
3.去修改该项目所在ecipse工作空间中的 .setting 文件夹下的org.eclipse.wst.common.project.facet.core.xml中的<installed facet="java" version="1.7"/>,
否则其自带的Tomcat7将会报Project facet Java version 1.8 is not supported错误。
p.s.
因为eclipse的使用需要读取JAVA_HOME环境变量,较新的要求是jdk1.8y以上,否则还打不开呢。
当我们项目需要用到低版本的jdk,重复上文的1步骤,然后再添加低版本jdk安装位置。(具体如下图)
最后选择你的其他jdk版本即可。
附:
原文出处:https://www.cnblogs.com/caihongmin/p/10171740.html
Cause: java.lang.IllegalArgumentException: invalid comparison: java.util.ArrayList and java.lang....
翻译过来就是
原因:java.lang.IllegalArgumentException:无效比较:java.util.ArrayList和java.lang.String
这个情况在list集合查找数据的sql中出的问题,在接受list的时候加了判断 list!='''' ,引起了集合与String类型的比较
<choose>
<when test="names!= null and names.size!=''''">
and name in
<foreach collection="names" item="name" index="index" open="(" close=")" separator=",">
#{name}
</foreach>
</when>
<otherwise>
and name= ''''
</otherwise>
</choose>
换成
<choose>
<when test="names!= null and names.size>0">
and name in
<foreach collection="names" item="name" index="index" open="(" close=")" separator=",">
#{name}
</foreach>
</when>
<otherwise>
and name= ''''
</otherwise>
</choose>
关于Java JRBeanCollectionDataSource:如何显示来自JavaBean的java.util.List中的数据?和java怎么显示数据库的数据的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于03-封装BeanUtil工具类(javabean转map和map转javabean对象)、android java.lang.IllegalStateException: System services not available to Activities before onCreate、AnnotationTransactionAttributeSource is only available on Java 1.5 and higher、Cause: java.lang.IllegalArgumentException: invalid comparison: java.util.ArrayList and java.lang....等相关知识的信息别忘了在本站进行查找喔。
本文标签: