Spring 4 Framework introduced with lots of improvements and features. Here we are going to deal with developing web application using Spring 4 Framework.
Project Requirements
- Spring 4.0.1.RELEASE jars
- JDK 1.6
- JSTL 1.2.jar
- Eclipse Java EE IDE(Eclipse JUNO)
Project Setup
Spring Configuration
Spring MVC 4 has replaced XML configuration with Java file configuration. You can use either one of them to build web application.
1.Configuring Spring Beans(spring-servlet.xml)
Spring MVC 4 facilitates by providing following annotations to define spring beans.
- @Configuration: It marks the file configuration file.
- @ComponentScan :It is used to scan the service classes.
- @EnableWebMvc : It is equivalent to which enables Web MVC environment.
AppConfig.java
The @Bean annotation is used to define spring beans here ViewResolver is annotated using @Bean annotation.
package com.expertwebindia.services; import java.util.List; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.view.InternalResourceViewResolver; import org.springframework.web.servlet.view.JstlView; @Configuration @ComponentScan(basePackages = {"com.expertwebindia.controllers"}) @EnableWebMvc public class AppConfig { @Bean public ViewResolver viewResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setViewClass(JstlView.class); viewResolver.setPrefix("/WEB-INF/views/"); viewResolver.setSuffix(".jsp"); return viewResolver; } }
The above configuration is same as below spring-servlet.xml.
<?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" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <context:component-scan base-package="com.expertwebindia.controllers"/> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
2.Configuring Dispatcher Servlet
As in Spring 3 we need to configure Dispatcher Servlet in web.xml. Now in Servlet 3.0 we can add servlet entry without web.xml.
WebAppInitializer.java
package com.expertwebindia; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRegistration.Dynamic; import org.springframework.web.WebApplicationInitializer; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.servlet.DispatcherServlet; public class WebAppInitializer implements WebApplicationInitializer { public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.register(AppConfig.class); ctx.setServletContext(servletContext); Dynamic dynamic = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx)); dynamic.addMapping("/"); dynamic.setLoadOnStartup(1); } }
The below configuration is equivalent to above Java file configuration which are doing in web.xml.
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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
3.Defining Spring Controller
Defining controller is same as Spring 3, using @Controller annotation.
HelloWorldController.java
package com.expertwebindia.controllers; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloWorldController { @RequestMapping(value="/hello") public String hello(ModelMap model){ model.addAttribute("msg","Hello World Example by Expertwebindia"); return "hello"; } }
4.Defining Views(JSP files)
Here is one view used which hello.jsp.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!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>Spring Web MVC 4 Example</title> </head> <body> <h1>${msg}</h1> </body> </html>
Download Source Code
You need to put Spring 4 Jars in WEB-INF/lib.
Download Spring MVC 4 ExampleRun Application
http://localhost:8080/SpringMVC4/hello
This post first appeared on Expertwebindia|Online Web Solutions, please read the originial post: here