iText生成一个单元格中包含不同的样式的PDF表格

项目中有的时候需要生成一个这样的PDF表格: PDFTest.java package com.ehi.rc.util.export; import java.awt.Color; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import com.lowagie.text.Chunk; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Font; import com.lowagie.text.FontFactory; import com.lowagie.text.PageSize; import com.lowagie.text.Phrase; import com.lowagie.text.pdf.PdfPTable; import com.lowagie.text.pdf.PdfWriter; public class PDFTest { Font boldFont = FontFactory .getFont(FontFactory.HELVETICA, 10, Font.BOLD, Color.BLACK); Font normalFont = FontFactory .getFont(FontFactory.HELVETICA, 10, Font.NORMAL, Color.RED); public byte[] generatePDF() { // init Document doc = new Document(PageSize.A4.rotate(), 10f, 10f, 10f, 10f); ByteArrayOutputStream output = new ByteArrayOutputStream(); try { PdfWriter.getInstance(doc, output); doc.open(); // create pdf table PdfPTable pdftable = new PdfPTable(2); Phrase phrase = new Phrase(); phrase.add(new Chunk("Blod red Font", boldFont)); phrase.add(" : "); phrase.add(new Chunk("Normal blank Font", normalFont)); pdftable.addCell(phrase); pdftable.addCell("Another cell"); phrase = new Phrase(); phrase.add(new Chunk("Blod red Font", boldFont)); phrase.add(" : "); phrase.add(new Chunk("Normal blank Font", normalFont)); pdftable.addCell(phrase); pdftable.addCell("Another cell"); doc.add(pdftable); } catch (DocumentException e) { throw new RuntimeException(e); } finally { if (doc.isOpen()) { doc.close(); } } return output.toByteArray(); } public static void main(String[] args) throws IOException { File file = new File("D://test.pdf"); FileOutputStream out = new FileOutputStream(file); out.write(new PDFTest().generatePDF()); } }

July 16, 2015 · 1 分钟 · 191 字

Javascript 使用回调函数注意事项

javascript中经常会使用到回调函数, 并且为了保证代码结构清晰整洁, 会将回调后需要执行的逻辑另外定义一个函数来执行, 例如: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>IT Art</title> </head> <body> <div id="timePanel">3秒后自动隐藏</div> <script type="text/javascript"> setTimeout(function(){ var timePanel = document.getElementById("timePanel"); timePanel.style.display = 'None'; }, 3000); </script> </body> </html> 可以看到, 如果setTimeout 中直接使用匿名回调时, 当有很多的逻辑判断代码时, 会导致代码嵌套过多, 不容易阅读和维护, 所以我们经常这么做: ...

July 1, 2015 · 1 分钟 · 390 字

JavaScript减少IF的方法 - &&(与)和||(或)的特殊用法

JavaScript 中的 && 和 || 除了用来判断两个布尔变量是否都true或false的作用外, 还有一些特殊用法. || 可以用在当一个对象是否为空时设置默认值的情况下, 一般情况下我们是这样实现这个功能的: function xx(option){ var default ="init"; var value = default; if(option){ value = option; } alert(value); } 或 function xx(option){ var default ="init"; var value = option? option:"init"; alert(value); } 现在还有一种简洁的方法: function xx(option){ var default ="init"; var value = option||"init" alert(value); } 注解: value = option||“init” 当option为true时, 即option有值, value就等于option. 当option为false时, 即option 为null 或undefined 时, value 为"init" && 可以用在当一个对象是true, 就返回另外一个值, 只在为false时返回本身, 它的作用和 || 相反 var value = option||“init” 当option为true时, 即option有值, value就等于"init". 当option为false时, 即option 为null 或undefined 时, value 为option

May 17, 2015 · 1 分钟 · 389 字

mybatis-3.2.8 新增数据为null时, 报The data type 'OTHER' is not support

上下文描述: 异常代码 @Insert("insert into sys_user(user_id, user_Name, PASSWORD, UPDATED_WHEN) values(#{userId},#{userName},#{password},#{updatedWhen})") public void insert(User user); 测试代码 @Test @Transactional public void testInsert() { User user = new User(); user.setUserId(6); user.setPassword("test12113"); userService.insert(user); } 异常信息 org.springframework.dao.InvalidDataAccessApiUsageException: Error setting null for parameter #2 with JdbcType OTHER . Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. Cause: java.sql.SQLFeatureNotSupportedException: The data type 'OTHER' is not supported. ; SQL []; The data type 'OTHER' is not supported.; nested exception is java.sql.SQLFeatureNotSupportedException: The data type 'OTHER' is not supported. at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:94) at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73) at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81) at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73) at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:371) at $Proxy18.insert(Unknown Source) at org.mybatis.spring.SqlSessionTemplate.insert(SqlSessionTemplate.java:240) at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:51) at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:52) at $Proxy23.insertByProvider(Unknown Source) at top.itart.portal.system.service.UserService.insertByProvider(UserService.java:25) at top.itart.portal.system.service.UserService$$FastClassBySpringCGLIB$$5f581865.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:267) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653) at top.itart.portal.system.service.UserService$$EnhancerBySpringCGLIB$$518bd406.insertByProvider(<generated>) at top.itart.portal.system.service.UserServiceTest.testInsertByProvider(UserServiceTest.java:41) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:73) at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82) at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:73) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:217) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) Caused by: java.sql.SQLFeatureNotSupportedException: The data type 'OTHER' is not supported. at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source) at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source) at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source) at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source) at org.apache.derby.impl.jdbc.Util.checkForSupportedDataType(Unknown Source) at org.apache.derby.impl.jdbc.EmbedPreparedStatement.setNull(Unknown Source) at org.apache.commons.dbcp.DelegatingPreparedStatement.setNull(DelegatingPreparedStatement.java:108) at org.apache.commons.dbcp.DelegatingPreparedStatement.setNull(DelegatingPreparedStatement.java:108) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.apache.ibatis.logging.jdbc.PreparedStatementLogger.invoke(PreparedStatementLogger.java:70) at $Proxy29.setNull(Unknown Source) at org.apache.ibatis.type.BaseTypeHandler.setParameter(BaseTypeHandler.java:43) at org.apache.ibatis.scripting.defaults.DefaultParameterHandler.setParameters(DefaultParameterHandler.java:81) at org.apache.ibatis.executor.statement.PreparedStatementHandler.parameterize(PreparedStatementHandler.java:80) at org.apache.ibatis.executor.statement.RoutingStatementHandler.parameterize(RoutingStatementHandler.java:61) at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:74) at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:47) at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:105) at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:71) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.apache.ibatis.plugin.Invocation.proceed(Invocation.java:49) at top.itart.common.interceptor.UpdatePlugin.intercept(UpdatePlugin.java:40) at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:60) at $Proxy26.update(Unknown Source) at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:152) at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:141) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:358) ... 48 more Caused by: ERROR 0A000: The data type 'OTHER' is not supported. at org.apache.derby.iapi.error.StandardException.newException(Unknown Source) at org.apache.derby.impl.jdbc.SQLExceptionFactory.wrapArgsForTransportAcrossDRDA(Unknown Source) ... 85 more 分析: 测试数据uer对象中userName值为null, 并且没有指定jdbcType时会出现异常, 文档http://mybatis.github.io/mybatis-3/sqlmap-xml.html 中指明: ...

February 10, 2015 · 2 分钟 · 503 字

Json 转化为Map

将json字符串按照key-value的格式转化为Map,保留原有的层次结构. 引入json-lib相关联的jar包 commons-beanutils-1.8.3.jar commons-collections-3.2.1.jar commons-lang-2.6.jar commons-logging-1.1.1.jar ezmorph-1.0.6.jar json-lib-2.4-jdk15.jar 递归解析: package top.itart; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import net.sf.json.JSONObject; public class Json { public static void main(String[] args) { String json = "{'user':{'name':'Jacy','age':'100'}, 'blog':{'blog1':'itart.cn','blog2':'itart.top'}}"; Map<String, Object> map = convertToMap(JSONObject.fromObject(json)); System.out.println(map); } private static Map<String, Object> convertToMap(JSONObject jsonObject) { HashMap<String, Object> data = new HashMap<String, Object>(); Iterator<?> it = jsonObject.keys(); while (it.hasNext()) { String key = String.valueOf(it.next()); Object value = jsonObject.get(key); if (value instanceof JSONObject) { data.put(key, convertToMap((JSONObject) value)); } else { data.put(key, value); } } return data; } }

February 5, 2015 · 1 分钟 · 148 字