This tutorial extends Getting started with Apache Tomcat application server by deploying a simple JEE Application to introduce Spring MVC.
Spring dependencies via Maven
Step 1: Modify the pom.xml file in the simpleWeb project to bring in the Spring web dependency jars.
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 |
<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> </dependencies> <build> <finalName>simpleWeb</finalName> </build> </project> |
Define a controller & store a model value with a key say “modelValue”
Step 2: Create a base package under src/main/java for the the controllers “com.mytutorial.controller”
Step 3: Create a controller named “SimpleController” in the package “com.mytutorial.controller”.
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 |
package com.mytutorial.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping("/myc/") public class SimpleController { @RequestMapping(method = RequestMethod.GET) public String sayMvc(ModelMap model) { model.addAttribute("modelValue", "Simple Spring 4 MVC"); return "output"; //WEB-INF/views/output.jsp } @RequestMapping(value = "/simpleAgain", method = RequestMethod.GET) public String sayHelloAgain(ModelMap model) { model.addAttribute("modelValue", "Simple again from Spring 4 MVC"); return "output"; //WEB-INF/views/output.jsp } } |
Define a view & extract the model value “modelValue”
Step 4: Create a folder named “views” for the JSP page views in the “src/main/webapp/WEB-INF” folder.
Step 5: Create a JSP page “src/main/webapp/WEB-INF/views/output.jsp“.
1 2 3 4 5 6 7 8 9 10 |
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Simple page</title> </head> <body> Output: ${modelValue} </body> </html> |
Wire up the controller & view
Step 6: Define the spring beans config xml file “applicationContext.xml” in the folder src/main/webapp/WEB-INF.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <context:component-scan base-package="com.mytutorial.controller" /> <mvc:annotation-driven /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/views/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> </beans> |
Step 7: Define the deployment descriptor file “web.xml” in the folder “src/main/webapp/WEB-INF“.
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 |
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name>Simple Spring MVC web app</display-name> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/entry/*</url-pattern> </servlet-mapping> </web-app> |
Build the simpleWeb.war
Step 8: Use the “mvn clean install” from a DOS console from the folder “c:\projects\simpleWeb” or from within maven by right mouse clicking on the project “simpleWeb” and then select “Run As –> Maven Install”
Deploy simpleWeb.war to Tomcat & start the server
Step 9: Copy the “C:\projects\simpleWeb\target\simpleWeb.war” to the tomcat installation folder “C:\tools\apache-tomcat-9.0.0.M21-windows-x64\apache-tomcat-9.0.0.M21\webapps”
Step 10: Start the Tomcat server
1 |
c:\tools\apache-tomcat-9.0.0.M21-windows-x64\apache-tomcat-9.0.0.M21\bin>startup.bat |
Access the URLs in a browser
1) http://localhost:8080/simpleWeb/entry/myc/
2) http://localhost:8080/simpleWeb/entry/myc/simpleAgain