Archive

Archive for August, 2010

Spring 3:

August 16, 2010 Leave a comment

The mvc:annotation-driven tag registers DefaultAnnotationHandlerMapping and AnnotationMethodHandlerAdapter beans that are required to dispatch requests to @Controllers. The tag configures those beans with sensible default values:

1. Support for Spring 3’s Type ConversionService in addition to JavaBeans PropertyEditors during Data Binding. A ConversionService instance produced by the org.springframework.format.support.FormattingConversionServiceFactoryBean is used by default. This can be overriden by setting the conversion-service attribute.
2. Support for formatting Number fields using the @NumberFormat annotation
3. Support for formatting Date, Calendar, Long, and Joda Time fields using the @DateTimeFormat annotation, if Joda Time 1.3 or higher is present on the classpath.
4. Support for validating @Controller inputs with @Valid, if a JSR-303 Provider is present on the classpath. The validation system can be explicitly configured by setting the validator attribute.
5. Support for reading and writing XML, if JAXB is present on the classpath.
6. Support for reading and writing JSON, if Jackson is present on the classpath.

Categories: Spring 3

Spring 3 JS & Dojo 1.4

August 14, 2010 Leave a comment

The latest version of org.springframework.js (2.0.2.RELEASE) comes with Dojo 1.3 support.
In order to enable Dojo 1.4, simply drop the dojo and dijit directories to the web application context directory. The org.springframework.js.resources.ResourceServlet servlet searches for the web application context.

The directory structure shall look like this:

+ webapp
+ WEB-INF
+ dojo
+ dijit
...

Categories: Spring 3

Spring 3 Enable Annotation based Configuration

August 10, 2010 Leave a comment

We can enable annotation based bean configuration by including the “context:annotation-config” tag in the Spring XML based configuration file. Note, the “context:component-scan” tag specifies the package directory to be scanned for the annotation configuration.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	<context:annotation-config />
	<context:component-scan base-package="sample" />
</beans>

The following code demonstrates the annotation configuration with the Spring XML configuration above. The Spring scans the classes under the sample package and finds classes with @Component or its specialized annotation and register them as a bean. Then, the Spring wires the beans by looking at @Autowired annotation. In the following case, the instance (singleton by default) of Dependency is wired with the instance variable “dependency” of Target instance. The out put from the following code is “sample.Dependency”.

package sample;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AnnotationTest {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "application-config.xml" });
		Target target = context.getBean(Target.class);
		System.out.println(target.toString());
	}
}

package sample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Target {
	@Autowired
	private Dependency dependency;
	@Override
	public String toString() {
		return dependency.toString();
	}
}

package sample;
import org.springframework.stereotype.Component;
@Component
public class Dependency {
	@Override
	public String toString() {
		return Dependency.class.getName();
	}
}

Very neat indeed!

Categories: Spring 3

Spring 3 Annotation: @Transactional

August 10, 2010 Leave a comment

@Transactional

In addition to the XML-based declarative approach to transaction configuration, you can use an annotation-based approach. Declaring transaction semantics directly in the Java source code puts the declarations much closer to the affected code. There is not much danger of undue coupling, because code that is meant to be used transactionally is almost always deployed that way anyway.

Reference: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/

Categories: Spring 3

Spring 3 Annotation: @InitBinder

August 10, 2010 1 comment

@InitBinder

Annotating controller methods with @InitBinder allows you to configure web data binding directly within your controller class. @InitBinder identifies methods which initialize the WebDataBinder which will be used for populating command and form object arguments of annotated handler methods.

Reference: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/

Categories: Spring 3

Spring 3 Annotation: @SessionAttributes

August 10, 2010 Leave a comment

@SessionAttributes

The type-level @SessionAttributes annotation declares session attributes used by a specific handler. This will typically list the names of model attributes or types of model attributes which should be transparently stored in the session or some conversational storage, serving as form-backing beans between subsequent requests.

Reference: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/

Categories: Spring 3

Spring 3 Annotation: @ModelAttribute

August 10, 2010 1 comment

@ModelAttribute

@ModelAttribute has two usage scenarios in controllers. When placed on a method parameter, @ModelAttribute is used to map a model attribute to the specific, annotated method parameter (see the populateSite() method below). This is how the controller gets a reference to the object holding the data entered in the form. In addition, the parameter can be declared as the specific type of the form backing object rather than as a generic java.lang.Object, thus increasing type safety.
@ModelAttribute is also used at the method level to provide reference data for the model (see the getPetSites() method below). For this usage the method signature can contain the same types as documented above for the @RequestMapping annotation.

Reference: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/

Categories: Uncategorized

Spring 3 Annotation: @RequestParam

August 10, 2010 Leave a comment

@RequestParam

The @RequestParam annotation is used to bind request parameters to a method parameter in your controller.

Reference: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/

Categories: Uncategorized

Spring 3 Annotation: @RequestMapping

August 10, 2010 Leave a comment

@RequestMapping

The @RequestMapping annotation is used to map portlet modes like ‘VIEW’/’EDIT’ onto an entire class or a particular handler method. Typically the type-level annotation maps a specific mode (or mode plus parameter condition) onto a form controller, with additional method-level annotations ‘narrowing’ the primary mapping for specific portlet request parameters.

Reference: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/

Categories: Uncategorized

Spring 3 Annotation: @Controller

August 10, 2010 Leave a comment

@Controller

he @Controller annotation indicates that a particular class serves the role of a controller. Spring does not require you to extend any controller base class or reference the Servlet API. However, you can still reference Servlet-specific features if you need to.

Reference: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/

Categories: Uncategorized