The Spring 3 RC1 maven artifacts have arrived!
Yesterday I saw a tweet of Arjen Poutsma that the new Spring 3 RC1 artifacts are released on the springsource maven repo. This made me curious off course so I started a little mavenized hello world app with Spring MVC.
The fist thing to do is to create a new maven project. You can either do this with the maven-archetype-webapp or via your favorite IDE. I just used m2eclipse which is de facto maven plugin for eclipse. It supports eclipse WTP integration which makes this really useful.
The new maven project has just some default settings. Don’t try to search and add dependencies to it just like that. The spring RC artifacts are not on the central repo so we first have to add the springsource maven repository. In the snippet below you can see the springsource repo definition and the dependency declaration.
<repositories>
<repository>
<id>springsource maven repo</id>
<url>http://maven.springframework.org/milestone</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.0.0.RC1</version>
</dependency>
</dependencies>
All we need now is some boilerplate spring MVC hello world code like a web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>hello-spring3-RC1</display-name>
<servlet>
<servlet-name>hello-spring3-RC1</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hello-spring3-RC1</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
A Spring MVC Controller:
package org.brambo.sandbox.spring3.hello.web.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloController {
@RequestMapping(value = "/helloWorld")
public ModelAndView helloWorld() {
ModelAndView mav = new ModelAndView();
mav.setViewName("helloWorld");
mav.addObject("message", "Hello Spring 3 RC1 Maven World!");
return mav;
}
}
The spring context definition which scans the classpath for spring components like our HelloController annotated with @Controller:
<?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:p="http://www.springframework.org/schema/p"
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:component-scan base-package="org.brambo.sandbox.spring3.hello.web"/>
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
and a simple jsp under WEB-INf/jsp displaying the message from the controller:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello</title>
</head>
<body>
${message}
</body>
</html>
and that’s it. You just created a mavenized web app with Spring MVC 3 RC1. You can now deploy it on, for example, tomcat or create a war file with ‘maven package’ to deploy it where you want.
Download the complete eclipse project here as a zip. Have fun and tweet me!
Bram Bruneel
Comments
You must be logged in to post a comment.