Archive for the ‘Examples’ Category

Spring MVC URI Template Example

Monday, April 18th, 2011

Background

So, I’m working on an existing Spring 3.x web project. This project heavily favours the use of annotations over XML configuration. As I was trawling through the source code I noticed “some wierd” syntax: “/{xxxx}” (which of course I had to look up!).

Most of the following is based upon the excellent Spring Documentation. I’ve lifted and rewritten the parts I require to serve as a condensed “aide-mémoire”.

Description

A URI Template is a URI-like string, containing one or more variable names. When you substitute values for these variables, the template becomes a URI.
(more…)

Tags: , ,
Posted in Spring | No Comments »

Spring MVC 3 @ModelAttribute Annotation Example

Monday, April 18th, 2011

Background

So, I’m working on an existing Spring 3.x web project. This project heavily favours the use of annotations over XML configuration files. As I was trawling through the source code I noticed the “@ModelAttribute” annotation (which of course I had to look up!).

If the above background seems familiar that’s because it’s the same as for a previous Spring Annotation post I made (ed: this is in danger of turning into a mini-series)

Description

Most of the following is based upon the excellent Spring Documentation. I’ve simply lifted and rewritten the parts I require to serve as an “aide-mémoire”.

The @ModelAttribute annotation serves two purposes depending on how it is used:

(more…)

Tags: , ,
Posted in Spring | 12 Comments »

Spring MVC 3.x @PathVariable Annotation Example

Friday, April 15th, 2011

Background

So, I’m working on an existing Spring 3.x web project. This project heavily favours the use of annotations over XML configuration files. As I was trawling through the source code I noticed the “@PathVariable” annotation (which of course I had to look up!).

Example

Imagine you’d like to create a controller that would allow users to see view products by using a URL such as http://foo.bar.com/productDetails/someProduct

e.g.

http://foo.bar.com/productDetails/widgetA - display details for widgetA
http://foo.bar.com/productDetails/widgetB - display details for widgetB

The @PathVariable annotation makes this easy…

package foo.bar;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;	 
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

@Controller
public class ShowProductController {
	 
    @RequestMapping("/productDetails/{productName}") // Handle any request of the form "/productDetails/XXXXX"
    public String showProduct(Model model, @PathVariable("productName") String productName) {
        model.addAttribute("productName", productName);
        return "showProduct"; // viewname
    }
}

What just happened?

Note the {productName} in the @RequestMapping – this is the path variable. The @PathVariable annotation on the next line captures the value and binds it to the method argument

Tags: , ,
Posted in Development, Examples | No Comments »

EasyMock Capture Example

Thursday, September 2nd, 2010

Background

EasyMock version 2.4 introduced some new functionality – the ability to capture the arguments passed to mock objects. As ever a few lines of code speaks volumes.

Capture Example

Test Class

package foo.bar;

import static org.junit.Assert.assertEquals;

import org.easymock.Capture;
import org.easymock.EasyMock;
import org.easymock.IMocksControl;
import org.junit.Before;
import org.junit.Test;

public class TestServiceImpl {
    private Service service;
    private Dao dao;
    private IMocksControl controller;
    private final String description = "description";

    @Before
    public void setUp() throws Exception {
        controller = EasyMock.createStrictControl();
        dao = controller.createMock(Dao.class);
        service = new ServiceImpl();  
        service.setDao(dao);
    }
    @Test
    public void testSave() {
        Capture <DomainObject> capturedArgument = new Capture <DomainObject>();
        dao.save(EasyMock.and(EasyMock.capture(capturedArgument), EasyMock.isA(DomainObject.class)));
        controller.replay();
        service.save(description);
        controller.verify();
        DomainObject record = capturedArgument.getValue();
        assertEquals(description, record.getDescription());
        assertEquals("getNextId", record.getId());
    }
}

Source code for classes under test after the break…….
(more…)

Tags: , ,
Posted in Development, Examples | No Comments »

Using Ibatis Typehandlers to fix Oracle Date “missing time”

Friday, June 25th, 2010

The Problem

When using Ibatis to retrieve Date type values, on Oracle 10g (some driver versions), the time portion of the value returned from the database is not mapped onto the Java object.

i.e.

In DB: 02-JAN-10 13:30:00
In Java Object after mapping: 02-JAN-10 00:00:00

The Ibatis ResultMapper, snippet, looks like this:

....
<result property="savedDate" column="SAVED_DATE" javaType="java.util.Date" jdbcType="DATE"/>
....

(more…)

Tags: , ,
Posted in Examples | 2 Comments »