eclipse maven spring MVC project

Eclipse maven spring mvc hello world:

Eclipse provides m2eclipse plugin to integrate Maven and Eclipse together.

Steps to create maven java web project in eclipse:

  1. In eclipse, click on File menu → New → Maven Project. Select maven-archetype-webapp template to create java project and Click on Next button.
  2. Now provide the group Id, artifact Id and Package. Click on Finish button. Complete directory structure and all files like web.xml file, pom.xml file, test case file etc will be created automatically.

Example:

<html>
<body>
<h2>Hello World!</h2>
</body>
</html>

Edit Auto created 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>Archetype Created Web Application</display-name>
 
        <servlet>
		<servlet-name>dispatcher</servlet-name>
		<servlet-class>
			org.springframework.web.servlet.DispatcherServlet
		</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
 
	<servlet-mapping>
		<servlet-name>dispatcher</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
 
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
	</context-param>
 
	<listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>
</web-app>

Add dependencies in auto created 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/maven-v4_0_0.xsd">
  <modelversion>4.0.0</modelversion>
  <groupid>tutorialspointexamples</groupid>
  <artifactid>SpringMVCHelloWorld</artifactid>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>SpringMVCHelloWorld Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
      <dependency>
        <groupid>junit</groupid>
        <artifactid>junit</artifactid>
        <version>3.8.1</version>
        <scope>test</scope>
      </dependency>
 
      <!-- Spring dependencies -->
      <dependency>
		<groupid>org.springframework</groupid>
		<artifactid>spring-core</artifactid>
		<version>${spring.version}</version>
	</dependency>
 
	<dependency>
		<groupid>org.springframework</groupid>
		<artifactid>spring-web</artifactid>
		<version>${spring.version}</version>
	</dependency>
 
	<dependency>
		<groupid>org.springframework</groupid>
		<artifactid>spring-webmvc</artifactid>
		<version>${spring.version}</version>
	</dependency>
  </dependencies>
  <build>
    <finalname>SpringMVCHelloWorld</finalname>
  </build>
</project>

Create an xml file dispatcher-servlet.xml file under the /WEB-INF/ directory.

<?xml version="1.0" encoding="UTF-8"?>
 
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	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-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="com.w3spoint" />
 
	<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>

Here prefix property specifies the directory of jsp files (view files). The suffix property specifies the file extension of view files.

Create Spring Controller

Create the HelloWorldController (HelloWorldController.java) under src/main/java/ directory:

package com.w3spoint.controller;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
 
@Controller
public class HelloWorldController {
	String message = "Hello ";
 
	@RequestMapping("/hello")
	public ModelAndView showMessage(
			@RequestParam(value = "name", required = false, 
			defaultValue = "World") String name) {
 
		ModelAndView mv = new ModelAndView("helloWorld");
		mv.addObject("message", message);
		mv.addObject("name", name);
		return mv;
	}
}

The @RequestMapping annotation maps web requests to specific handler classes or handler methods. The @RequestParam annotation is used for method parameter which should be bound to a web request parameter. The new ModelAndView(“helloWorld”) refers to the target view.

Please follow and like us:
Content Protection by DMCA.com