Monthly Archives: January 2016

Layering in Java Webapps – My Final Version

What a good layering solution should do It must handle the following problems: 1. Dividing a system into layers based on modularization principles, such as single-direction dependency, limited entry of components and so on.  2. Compatible with modern dependency management tools such as Maven  3. Allowing for evolving into a distributed system in the future  […]

Code Snippet: A common duplet class

Java doesn’t allow multi-value method returning. To return 2 values for a method, you can create a "pair" object which contains two values. /** * a pair of objects. like other containers, you’d rather return an empty * container than a null one * * * */ public class MyDuplet<L,R> { public L left; public […]

MyBatis: Automatically set timestamps for beans saved

The Problem When a bean is inserted or updated, its "createdAt" or "updatedAt" should be set as the current time. MyBatis won’t do that for you by default. You can manually set them before inserting them. But you may forget it. Solution The solution is to have a MyBatis interceptor which does the job before […]

Code Snippet: Trim all string fields of a bean

You may find it useful where you can’t use Spring MVC’s StringTrimEditor the trim method /** * trim string fields to null. * Note: only the root string fields are trimmed. string fields of the * bean’s composite fields are let alone. * All reflection related security exceptions are ignored. * * @param bean */ […]

bean-validation + spring example

Add bean validation dependency Hibernate provides an implementation of bean-validation. So we will dependent on it. But it has nothing to do with Hiberante ORM. org.hibernate hibernate-validator 5.1.3.Final Create a validator class as a spring bean @Component public class MyBeanValidator { @Resource private Validator validator; public <T> List<String> validateBean(T bean) { List<string> errors = new […]