Skip to main content

Posts

Showing posts from February, 2018

Chain of responsibility design pattern

Greetings! In this behavioral design pattern, request is handled using a chain of objects. This is used to eliminate series of if...else if...else if... statements. Thus, each of the processing object contains only the related logic (as in an if statement). It is also possible to execute all the processing objects. Since the chain work independently new handlers can be added easily, and also existing handlers can be removed. Image source wikipedia Let's see a sample.

IoC, DI and Spring

Greetings! "program to interface not implementation" IoC and DI Both words are used interchangeably in Java world but there is a difference. Inversion of Control is a more general concept Dependency Injection is a concrete pattern IoC - Inversion of Control Inverts responsibility of managing object life cycle (create, initialize, destroy, etc). In simple term, let someone else to create objects for you instead of using "new" in code itself. Normally this someone else is Ioc Container. IoC Container Framework to create dependencies and inject them automatically when required. DI - Dependency Injection Objects are depending on other Objects. In DI, Objects are given (injects) their dependencies (other objects) through their constructors, methods, fields. IoC with DI So if DI means objects are injected who is doing that. Here is where IoC comes in to play. With IoC dependencies are managed by the container. Spring Framework Spring Framework

Implement a JPA attribute converter

Greetings! Using JPA we can easily map our column values to Java variable and so forth. What if we want our column to have some value and mapped variable has another? One way to achieve this is to have a another method to convert the values. JPA comes with a another handy way to fulfill this. That is AttributeConverter. Only thing we want to do is to implement AttributeConverter and add our logic. Here is an example using Enums.

Thymeleaf hidden field value

Greetings! Thymeleaf gives us almost everything we need. But there are some cases that thymeleaf does not work properly. When we have a hidden field and we want to assign it a value, strangely thymeleaf will ignore the value property. Solution : Use th:attr th :attr= "name=' <tr th :each= "user, stat : ${users}" > <input type= "hidden" th :attr= "name='userDtos[' + ${stat.index} + '].userId'" th :value= "${user.id}" /> <!--some other inputs--> <td> <input type= "checkbox" th :field= "*{userDtos[__${stat.index}__].active}" /> </td> </tr>

Spring PathVariable with dots

Greetings! Spring helps us in many ways. But in sometimes it do more than we want. If we want to have dots like xx.yy.zz in our path Spring will remove last part probably thinking it as a file type. xx.yy.zz will become xx.yy @GetMapping ( value = "/{myvar}" ) public String someMethod ( @PathVariable String myvar) { // myvar is xx.yy here // do something with myvar // return } Solution: Use regex to allow dots @GetMapping ( value = "/{myvar:.+}" ) public String someMethod ( @PathVariable String myvar) { // myvar is xx.yy.zz here // do something with myvar // return } for more information check this stackoverflow answer.