Open In App

Spring MVC using Java based configuration

Prerequisites: MVC Design Pattern, Spring MVC with JSP View
Spring MVC framework enables separation of modules namely Model, View and Controller and seamlessly handles the application integration. This enables the developer to create complex applications also using plain java classes. The model object can be passed between view and controller using maps. In this article, we will see how to set up a Spring MVC application in the Eclipse IDE and understand how to make applications. 
The Spring MVC framework is comprised of the following components: 
 

Requirements : 



Steps to set up the Spring MVC application in eclipse IDE:
 






<project
  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.geeksforgeeks</groupId>
  <artifactId>SpringMVCjava</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>SpringMVCjava 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>
     
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.3.12.RELEASE</version>
    </dependency>
     
  </dependencies>
  <build>
    <finalName>SpringMVCjava</finalName>
  </build>
</project>




package com.geeksforgeeks.web;
 
import org.springframework.web
    .servlet.support
    .AbstractAnnotationConfigDispatcherServletInitializer;
 
public class WebInitializer
    extends AbstractAnnotationConfigDispatcherServletInitializer {
 
    @Override
    protected Class<?>[] getRootConfigClasses()
    {
        // TODO Auto-generated method stub
        return null;
    }
 
    @Override
    protected Class<?>[] getServletConfigClasses()
    {
        // TODO Auto-generated method stub
        return new Class[] { MVCconfig.class };
    }
 
    @Override
    protected String[] getServletMappings()
    {
        // TODO Auto-generated method stub
        return new String[] { "/" };
    }
}


package com.geeksforgeeks.web;

import org.springframework.context
.annotation.ComponentScan;
import org.springframework.context
.annotation.Configuration;
import org.springframework.web.servlet
.config.annotation
.WebMvcConfigurerAdapter;

@Configuration
@ComponentScan({ "com.geeksforgeeks.web" })

public class MVCconfig
extends WebMvcConfigurerAdapter {
}




package com.geeksforgeeks.web;
import org.springframework
    .stereotype.Controller;
import org.springframework.web
    .bind.annotation.RequestMapping;
import org.springframework.web
    .servlet.ModelAndView;
 
@Controller
public class GreetController {
 
    @RequestMapping("/greet")
    public ModelAndView showview()
    {
 
        ModelAndView mv = new ModelAndView();
        mv.setViewName("result.jsp");
        mv.addObject("result",
                     "GeeksForGeeks Welcomes "
                         + "you to Spring!");
        return mv;
    }
}




<html>
<body>
<h2>Hello World!</h2>
    <form action="greet">
        <input type="submit" value="Press to greet">
    </form>
</body>
</html>




<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" isELIgnored="false"%>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="ISO-8859-1">
        <title>Insert title here</title>
    </head>
    <body>
     
        <h1>${result}</h1>
         
    </body>
</html>


Article Tags :