0
Sponsored Links


Ad by Google
In our previous post we have already seen Spring 4 MVC Hello World Example, but one of my friend requested to write a post using maven as our previous post was using jar files instead of pom dependency. So here we are going to show you simple Hello World example using maven.
Spring Framework is a very popular open source application framework developed using Java language. Spring Framework is nowadays top in the framework list and off course high payee job and yes very easy to learn. Current stable release of Spring is 4.1.4 and we are going to use the same version of Spring.

Just follow the below given simple steps and you will complete your Spring+Maven Hello World example, you can download this sample application from the download link at the end of this tutorial.

Tools and Technologies we are using here:

  • JDK 7
  • Spring 4.1.1
  • Eclipse Kepler 4.3
  • Maven 3.2
Overview of the Project Structure:

Main Objects of this project are:
  • pom.xml
  • web.xml
  • spring4demo-servlet.xml
  • HelloWorldController.java
Below are the list of dependencies required for this project.
  • spring-core
  • spring-web
  • spring-webmvc
  • jstl
  • javax.servlet-api

Step 1. Create a Maven Project:
Step A: Go to File->New->Other..


Step B: Select Maven Project from the select wizard.


Step C: Select project name and location from New Maven Project wizard.


Step D: Configure project, provide GroupId, artifactId, project name etc. See the screenshot we have selecting packaging type as war because we are going to develop web project.

After completion of all the above steps now your project will look like this screen shot.

2. Add project dependencies into pom.xml file:
Double click on your project's pom.xml file, it will looks like this with very limited information.
<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/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.javamakeuse</groupId>
 <artifactId>Spring4_Maven_MVC_Hello_World</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>war</packaging>
 <name>Spring4_Maven_MVC_Hello_World</name>
 <description>Spring4_Maven_MVC_Hello_World example</description>

 <build>
  <finalName>Spring4_MVC_Hello_World_Example</finalName>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
     <source>1.7</source>
     <target>1.7</target>
    </configuration>
   </plugin>
  </plugins>
 </build>
</project>

Now add dependencies entry inside pom.xml file. Below is the complete code of pom.xml file.

pom.xml
<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/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.javamakeuse</groupId>
 <artifactId>Spring4_Maven_MVC_Hello_World</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>war</packaging>
 <name>Spring4_Maven_MVC_Hello_World</name>
 <description>Spring4_Maven_MVC_Hello_World example</description>

 <dependencies>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-core</artifactId>
   <version>4.1.4.RELEASE</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-web</artifactId>
   <version>4.1.4.RELEASE</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-webmvc</artifactId>
   <version>4.1.4.RELEASE</version>
  </dependency>
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>jstl</artifactId>
   <version>1.2</version>
   <scope>runtime</scope>
  </dependency>
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>javax.servlet-api</artifactId>
   <version>3.0.1</version>
   <scope>provided</scope>
  </dependency>

 </dependencies>

 <build>
  <finalName>Spring4_MVC_Hello_World_Example</finalName>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
     <source>1.7</source>
     <target>1.7</target>
    </configuration>
   </plugin>
  </plugins>
 </build>
</project>

Step 3: Create WEB-INF folder inside webapp and inside WEB-INF create web.xml file

Here is complete 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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Spring4_MVC_Hello_World_Example</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>    
  <servlet-name>spring4demo</servlet-name>    <!-- servlet name can be anything -->
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <!-- Known as front controller in spring -->
  <load-on-startup>1</load-on-startup>    
 </servlet>    
 <servlet-mapping>    
  <servlet-name>spring4demo</servlet-name>    
  <url-pattern>*.htm</url-pattern>    
 </servlet-mapping>  
</web-app>

In web.xml we used DispatcherServlet, DispatcherServlet is known as front controller in Spring MVC you can read more about front controller from our previous post Spring front controller.

Step 4: Create spring4demo-servlet.xml parallel to web.xml and place the below code.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
 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.1.xsd  
http://www.springframework.org/schema/context  
http://www.springframework.org/schema/context/spring-context-4.1.xsd">

 <context:component-scan base-package="com.javamakeuse.spring.helloworld" />

 <bean id="viewResolver"
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix">
   <value>/WEB-INF/pages/</value>
  </property>
  <property name="suffix">
   <value>.jsp</value>
  </property>
 </bean>
</beans>  
Here we used InternalResourceViewResolver, It's an implementation of ViewResolver interface. There are three different implementation of ViewResolver and those are listed here with example.

Step 5: Create index.jsp Right click on webapp folder then New->JSP File and provide name as index.jsp

Here is a complete index.jsp code place this code in your index.jsp page.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring 4 MVC Hello World Example</title>
</head>
<body>
 <div style="width: 200px; margin: auto;">
  <p>
   Click <a href="printHello.htm">here</a> to call spring controller.
  </p>
 </div>
</body>
</html>
Step 6: Create a new folder named it as pages inside webapp->WEB-INF->pages and create another jsp file helloworld.jsp inside this folder.

Here is a complete helloworld.jsp file.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello World</title>
</head>
<body>
 <div style="width: 200px; margin: auto;">
  <p>${msg}</p>
 </div>
</body>
</html>
Step 7: Create HelloWorldController.java In Spring 4 any class can be a controller if that class is uses @Controller above the class. Below is the complete HelloWorldController.java class.
package com.javamakeuse.spring.helloworld;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloWorldController {

 @RequestMapping(value = "printHello")
 public ModelAndView returnHelloWorld() {
  System.out
    .println("Controller is called going to return helloworld.jsp");
  
  String dynamicMessage = "You did it !!!";
  
  return new ModelAndView("helloworld", "msg", dynamicMessage);
 }
}

Finally Right click on Project and select Run As->Run On Server

URL: http://localhost:8080/Spring4_MVC_Hello_World_Example/

Click on the link and here is another page


That's it.
While learning Spring MVC most of us getting java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet exception if you are also facing the same here is the fix of this exception

Download the complete example from here Source Code


References:
Reference 1
Reference 2


Sponsored Links

0 comments:

Post a Comment