This tutorial extends Spring MVC form submission tutorial step by step. This tutorial gets rid of web.xml and applicationContext.xml files.
pom.xml changes
Step 1: Add “JSTL” (i.e. JSP Standard Tag Library ) jar dependency, and also override default “maven-war-plugin” by explicitly adding the plugin and setting the “failOnMissingWebXml” to false.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mytutorial</groupId> <artifactId>simpleWeb</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>simpleWeb Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.9.RELEASE</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> </dependencies> <build> <finalName>simpleWeb</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.1.0</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build> </project> |
Java based Spring configs
Step 2: Create a new package “com.mytutorial.config” in “src/main/java”
Step 3: Add a Spring Java config file named “SimpleWebConfiguration.java” with the required Spring annotations @Configuration, @EnableWebMvc and @ComponentScan to replace what we defined in the “applicationContext.xml”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | package com.mytutorial.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; 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 @EnableWebMvc @ComponentScan(basePackages = "com.mytutorial.controller") public class SimpleWebConfiguration { @Bean public ViewResolver viewResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setViewClass(JstlView.class); viewResolver.setPrefix("/WEB-INF/views/"); viewResolver.setSuffix(".jsp"); return viewResolver; } } |
Step 4: Define “SimpleWebAppInitializer.java” that implements “WebApplicationInitializer”, which builds on Servlet 3’s javax.servlet.ServletContainerInitializer facility. This class will be scanned by Spring on application startup and bootstrapped. WebApplicationInitializer implementation called AbstractAnnotationConfigDispatcherServletInitializer is extended.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | package com.mytutorial.config; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; public class SimpleWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return new Class[]{SimpleWebConfiguration.class}; } @Override protected Class<?>[] getServletConfigClasses() { return null; } @Override protected String[] getServletMappings() { return new String[] { "/entry/*" }; } } |
Step 5: Delete “web.xml” & “applicationContext.xml” files in “src/main/webapp/WEB-INF”. Now we are XMLs free with the help of Java based config files and annotations.
Build & deploy simpleWeb.war
All other artifacts and deployment/running the simpleWeb are same as described in Spring MVC form submission tutorial step by step.