Monday, July 2, 2012

Spring MVC and Velocity WebApp

This tutorial shows how to use Velocity templates with Spring MVC. Velocity is more flexible templating engine than JSP/JSTL standard templates. We will create a simple application that contains 2 pages: list and detail. These pages will have common layout.

Tuesday, May 8, 2012

MySQL: Sort by column with NULLs on top

Common task, when you need to sort table data by a column, that may have null values and you want to put nulls to the top.

Friday, April 27, 2012

Java: Release resources on program termination

Applications use different resources (database connections, file descriptors, etc.) that need to be released when program terminates.

Sunday, March 18, 2012

Wicket TinyMCE and AjaxButton

TinyMCE is a WYSIWYG editor, that can be used in Wicket to replace your TextAreas with a Rich Text Editor.

Tuesday, March 13, 2012

Maven Jar Plugin: Setting Main-Class and Class-Path in Manifest

To start a Java application you have to tell JVM where is the program's entry point (Main Class) and where to search for classes (Class Path). Main class must have a public static method called main, which takes command arguments and does the initialization. If your Java application is packaged into JAR you can define Main-Class and Class-Path in manifest file, so that JVM would be able to find your program's entry point and load necessary classes. To do so you can use maven-jar-plugin that generates manifest with Main-Class and Class-Path parameters.

Debugging PHP with Xdebug and Netbeans

Many PHP programmers tend to develop their applications without special debugging tools. PHP developers are happy, because they do not have to wait for recompilation and redeploy, they just press F5 and see the results in browser. I call it "echo" debug method :). In this article I will show you how easy it is to install Xdebug debugger tool and use it in Netbeans IDE.

Wednesday, February 29, 2012

Integer overflow in Java

What would happen in Java when you increment an Integer variable that already contains its maximum value?

Tuesday, February 21, 2012

Log4j filter to mask Payment Card numbers (PCI DSS)

According to PCI DSS (Payment Card Industry Data Security Standard) your application must not store payment card numbers. This requirement includes database, files and logs. The following filter will allow you to mask card numbers in your logs on the fly, so even if you accidentally turned debug mode on for network communication, you can be confident that your data is PCI compliant.

Sunday, February 19, 2012

Stop reinventing wheel, use Apache Commons

Developer's life is full of routine tasks. We need to validate strings, convert data from different formats, compare objects and so on. Some developers tend to write everything on their own, reinventing common algorithms again and again. Every Java developer should look at Apache Commons Lang library as it already has everything we need every day.

Wednesday, February 8, 2012

NetBeans: syntax highlighting for html templates with unsupported file extension

NetBeans editor supports syntax highlighting and validation for several languages as Java, PHP, JavaScript, HTML. But if your file has different extension e.g. .template, NetBeans is unable to recognise the language. What you have to do if you want to turn on highlighting for Smarty or Velocity templates?

CloseUtils: closing resources silently

When working with resources in Java it is important that they are closed after use. Usually it is done by calling close() method in finally block. The problem is that close() method can throw an Exception. The following class allows you to close resources silently, so you save some try-catch lines.

Sunday, February 5, 2012

Integrating Wicket with Spring

In this short example I will show you how to integrate Wicket 1.5 with Spring 3. Wicket is a rich web framework that allows creating web applications using simple html and Java code. Wicket has its own IOC/DI engine, but Spring has become a de-facto standard for service layer.

Tuesday, January 31, 2012

My Wicket improvement for StringValue was supported!

Working with Wicket I was sadly surprised by the behaviour of StringValue class. I needed to get optional parameters from URL, such as page number or document id. This parameters may have values, but can be omited and should be replaced by default value.

So I used this code to get page number, default page is 1.

PageParameters parameters = ...
int currentPage = parameters.get("page").toInt(1);  // default value is 1

Unfortunatelly, toInt(1) throwed StringValueConversionException instead of returning 1.

Wednesday, January 25, 2012

Suggest using JQuery Autocomplete and Wicket

You know that cool Google feature called Suggest? When you type into search input box, it offers you some possible search queries. JQuery has a plugin for this, it is called JQuery Autocomplete. In this article I will show you how to make Suggest feature using JQuery and Wicket.

Tuesday, January 24, 2012

Resizing Images in Java. Creating thumbnails

In this article we will find out how to create thumbnails from images with Java. Thumbnails are used everywhere on web e.g. eshop product catalogue, image galleries etc.  So what does Java offer to a developer?  We can use standard Java libraries or imgscalr library for this.

External Image in Wicket

This component allows creating an image that is loaded from external URL. All we need is to instantiate a new ExternalImage component by passing image URL to constructor and add it on a page.

Using Wicket Panel to create Reusable Component (Widget)

Why do we like components? They save our time. Once developed, they can be used many times. In this article we will create reusable component using Wicket Panel.

How to calculate the number of pages for search results

Imagine we have some records in our database, and we want to show them separately, divided in pages. Under the list we want to show hyperlinks to the next and previous pages. In the previous artice Long list pagination in Java I explained how to make Pagination. But for the pagination to work we need to know total number of rows. There are several techniques that are suitable in different situations.

Wednesday, January 11, 2012

Wicket 1.5 mountPage with optional parameters

I am using Wicket 1.5 as web layer for application. I needed a page that could take parameter but it should be optional.

So I have an URL like this, it has 2 parameters search (keyword) and page (1), but I want page to be optional, so that URL like should be mapped to the same page.

I tried to map it with mountPage(“/search/${keyword}/${page}”, SearchPage.class);

But the problem was that page parameter was mandatory.

Tuesday, January 10, 2012

Generate URL query string from key value parameters

Java class to generate URL query string from key value pairs. Source code below.

Long list pagination in Java

Information on web pages is usually represented as a list e.g. search results, product list, messages. That list can be very long (hundreds of rows). You can print the whole list on the page, but it would take too long to load the page and the user would have to scroll it. That is not user friendly.

So we need to split the list into ranges and to show each range on a separate page. We have to calculate the number of pages by dividing the number of rows by the number of rows we want to show per page.

Total rows: 100; Rows per page: 20; Total pages = Total rows / Rows per page = 5.

The class does this calculation. To instantiate we need to pass 2 parameters: totalItems and itemsPerPage. Current page is 1 by default. To change current page number we call setter method setCurrentPage(int currentPage).

After initializaton is complete, we can query Pagination for the following data:

  • getTotalPages() 
  • hasPreviousPage()
  • hasNextPage() 
  • getPreviousPage() 
  • getNextPage()

Then we need to show available pages as hyperlinks. It is good to have liquid pagination, to show several pages before and after current page. Call method List<Integer> getPagesList(int radius) to get a list of pages. Radius is the number of pages before and after the current page.

The source code of the Pagination class is placed below. You can use it for free.