JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Spring MVC hello world example


Here is the first step to start Spring MVC application development. This page helps you to understand basic Spring MVC hello world example.

In the maven project, pom.xml should contain all spring MVC dependencies.

<?xml version="1.0" encoding="UTF-8"?>
<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>spring-mvc</groupId>
  <artifactId>spring-mvc</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>spring-mvc</name>
  <properties>
		<spring.version>3.2.0.RELEASE</spring.version>
	</properties>
	<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>
</project>

Our first spring controller with @RequestMapping annotation:

package com.java2novice.controllers;

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("/first-app")
public class MyFirstController {

	@RequestMapping(method = RequestMethod.GET)
	public String firstActionMethod(ModelMap mdlMap){
		
		mdlMap.addAttribute("message", "My First Spring MVC Application");
		return "helloWorld";
	}
}

A simple JSP view:

<html>
	<head>
		<title>My first spring mvc application</title>
	</head>
	<body>
		<h1>${message}</h1>
	</body>
</html>

XML based spring configuration file mvc-dispatcher-servlet.xml. Make sure that you are enabled component-scan to detect controllers.

<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.2.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd">
 
	<context:component-scan base-package="com.java2novice.controllers" />
 
	<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>

Deployment descriptor web.xml configurations:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" metadata-complete="false" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
    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_3_0.xsd">

	<display-name>Spring MVC Web Application</display-name>

	<servlet>
		<servlet-name>mvc-dispatcher</servlet-name>
		<servlet-class>
        	org.springframework.web.servlet.DispatcherServlet
		</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>mvc-dispatcher</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
	</context-param>

	<listener>
		<listener-class>
        	org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>
</web-app>

Try accessing the web page using http://localhost:8080/spring-mvc/first-app and here is the demo output:

 Next Program >>

Spring MVC Framework Examples

  1. Spring MVC hello world example
  2. Spring MVC REST hello world example
  3. How to map URLs to beans in Spring MVC?
  4. How to map URLs using annotations in Spring MVC?
  5. Spring MVC URI Template Patterns using Regular Expressions
Knowledge Centre
Can interface be final?
No. We can not instantiate interfaces, so in order to make interfaces useful we must create subclasses. The final keyword makes a class unable to be extended.
Famous Quotations
It is easier to fight for one’s principles than to live up to them.
-- Alfred Adler

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.