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 http://example.com/search/keyword/1, it has 2 parameters search (keyword) and page (1), but I want page to be optional, so that URL like example.com/search/keyword 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 Pagination.java 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.