Alfred’s Computing Weblog

Alfred Java-cored Computing Weblog

Archive for October 2008

Java Output Formatting

without comments

Here are some often used java output formatting with using String formatter..

// format string output
System.out.println(String.format("String  [%s]", "a string"));
/* $> String  [a string] */

// format integer output
System.out.println(String.format("Integer [%d]", 12345));
/* $> Integer [12345] */

// format double output
System.out.println(String.format("Double  [%.2f]", 123.456d));
/* $> Double  [123.46] */

// format floating point output
System.out.println(String.format("Float   [%.3f]", 0.123f));
/* $> Float   [0.123] */

// format boolean output
System.out.println(String.format("Boolean [%b]", true));
/* $> Boolean [true] */

// format date output
System.out.println(String.format(
"SimpleDateFormat-1 [%s]", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date())));
/* $> SimpleDateFormat-1 [2008-10-20 14:36:50.234] */

// format date output
System.out.println(String.format(
"SimpleDateFormat-2 [%s]", new SimpleDateFormat("yyMMdd_HHmmss").format(new Date())));
/* $> SimpleDateFormat-2 [081020_143650] */

Written by Alfred

October 20, 2008 at 14:44

Posted in Java

Tagged with ,

The prefix “tx” for element “tx:advice” is not bound

with one comment

Error:
The prefix “tx” for element “tx:advice” is not bound

Story:
I was updating my code, to add in DB data insertion into 1 of my action class.
hit the above error “The prefix “tx” for element “tx:advice” is not bound”
From the error message, i think its complaining the missing of to define the schema location.
So, I add the schema location, and restart the tomcat…. eh, same error again?!
OK, i did a google and found out that, I not only missed to define the schema location;
I also forget to defind the “tx” header. (something like “tablib” in jsp).
So, I add in the following line “xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” and test again.
Wow, its working now!

Lessons Learned:
The root cause of this error is missing the “xmlns:tx” being defined in applicationContext.xml

Solution:
Final applicationContext.xml header:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"
>

Written by Alfred

October 17, 2008 at 17:58

Posted in Java, Spring

Tagged with , ,