23 August 2017

Sample Spring MVC application

To build a Spring MVC application,We need to setup the following software

1.JDK
2.Apache Maven
3.Intellij/Eclipse(Editor tool)
4.Apache Tomcat

Project structure looks like
SpringMVC_project_structure


Define web.xml file.Declare spring DispatcherServlet as the servlet in web.xml. Mention the URL pattern,which can be accepted by servlet.
Declare ContextLoaderListener and provide context-param as spring context xml configuration file (java9pro-springmvc-servlet.xml).
web.xml 
<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">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/java9pro-springmvc-servlet.xml</param-value>
    </context-param>
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>java9pro-springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>java9pro-springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
java9pro-springmvc-servlet.xml:
This configuration file contains component scan configuration and view resolver configuration.
<mvc:annotation-driven/> statement is to support MVC annotations.

<beans xmlns="http://www.springframework.org/schema/beans"       
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       
xmlns:context="http://www.springframework.org/schema/context"       
xmlns:mvc="http://www.springframework.org/schema/mvc"       
xsi:schemaLocation="http://www.springframework.org/schema/mvc 
       http://www.springframework.org/schema/mvc/spring-mvc.xsd       
       http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans.xsd       
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context.xsd ">

    <context:component-scan base-package="com.becomeJavaPro.springmvc"/>
    <mvc:annotation-driven/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>
 Define the views
        1. login.jsp has the input fields for entering username and greeting.

       <form action="submit.html" method="post">
       Name:<input type="text" name="name"/><br/>
       Greeting:<input type="text" name="greeting"/><br/>
       <input type="submit" value="login"/>
        </form>

        2. hello.jsp has the greeting information for the user.

       <H1>${user.greeting} Dear ${user.name}</H1>
 Define Model class User.
   package com.becomeJavaPro.springmvc;

    public class User {


      String name;
      String greeting;

      public String getGreeting() {
        return greeting;
      }

      public void setGreeting(String greeting) {
        this.greeting = greeting;
      }
      public String getName() {
        return name;
      }

      public void setName(String name) {
        this.name = name;
     }
   }
  Define a Controller class by adding @Controller annotation to Java9ProController.
  It has two request mappings.
  1./login which redirect to login page
  2./submit which gets the @RequestParam from login page and set to User(Model) object.
  Then it redirect to hello page
Java9ProController 
package com.becomeJavaPro.springmvc;

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;

@Controllerpublic class Java9ProController {
    @RequestMapping("/login")
    public String loginPage(){
        return "login";
    }

    @RequestMapping("/submit")
    public ModelAndView loginPage(@RequestParam String name,@RequestParam String greeting){
 
        ModelAndView mv = new ModelAndView("hello");
        User user = new User();
        user.setName(name);
        user.setGreeting(greeting);      
mv.addObject(user); return mv; } }
pom.xml look like below.We need spring webmvc jar. 
<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>java9pro.springmvc</groupId>
    <artifactId>java9pro.springmvc.arti</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <build>
        <finalName>springJava9ProMVC</finalName>
    </build>

    <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.3.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.3.2.RELEASE</version>
    </dependency>
    </dependencies>
</project>
Build the application with mvn clean install.War file will be generated in target folder.
Install the war file in tomcat container by placing war file in webapps folder.start the tomcat server.We can see the output with the following URL like
http://localhost:8080/springJava9ProMVC/login
SpringMVC_output1
when we click on login,Controller method with request mapping as submit, will be called.Page will be redirected to hello.jsp and output is
SpringMVC_output2
To know about MVC click