HelloKoding

Practical coding guides

Spring MVC JSP Example of XML Configuration

This tutorial walks you through the process of creating a Hello World example web site with JSP and Spring MVC XML Configurations

What you’ll build

Spring MVC XML Hello World

What you’ll need

  • Your favorite IDE
  • JDK 1.8+
  • Maven 3+

Stack

  • Spring MVC 5
  • JSP View Template

Init project structure and dependencies

Project structure

├── src
│   └── main
│       ├── java
│       │   └── com
│       │       └── hellokoding
│       │           └── springmvc
│       │               └── HelloController.java
│       ├── resources
│       │   ├── application.properties
│       │   └── logback.xml
│       └── webapp
│           ├── WEB-INF
│           │   ├── views
│           │   │   └── hello.jsp
│           │   ├── appconfig-mvc.xml
│           │   ├── appconfig-root.xml
│           │   └── web.xml
│           └── resources
│               ├── css
│               │   └── main.css
│               └── js
│                   └── main.js
└── pom.xml

Project dependencies

pom.xml

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         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.hellokoding.springmvc</groupId>
    <artifactId>springmvc-xml-jsp</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <jdk.version>1.8</jdk.version>
        <spring.version>5.1.5.RELEASE</spring.version>
        <servlet-api.version>2.5</servlet-api.version>
        <junit.version>4.12</junit.version>
        <logback.version>1.2.3</logback.version>
        <jetty-maven-plugin.version>9.4.15.v20190215</jetty-maven-plugin.version>
    </properties>

    <dependencies>
        <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>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>${servlet-api.version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>${logback.version}</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <source>${jdk.version}</source>
                <target>${jdk.version}</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>${jetty-maven-plugin.version}</version>
            <configuration>
                <scanIntervalSeconds>10</scanIntervalSeconds>
                <webApp>
                    <contextPath>/</contextPath>
                </webApp>
            </configuration>
        </plugin>
        </plugins>
    </build>
</project>

Define Controller and View Template

Hello Controller

@Controller maps HTTP requests with JSP view template files.

HelloController.java

package com.hellokoding.springmvc;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class HelloController {
    @GetMapping({"/", "/hello"})
    public String hello(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) {
        model.addAttribute("name", name);
        return "hello";
    }
}

@RequestMapping maps /hello request to hello() method.

name is a query string parameter of /hello request.

Model object passes value to hello view (hello.jsp).

JSP/JSTL View Template

JSP performs server side rendering for the HTML content. It parses the hello.jsp template below and evaluate the EL (Expression Language) to render the value of ${name} parameter that was set in the controller.

hello.jsp

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="contextPath" value="${pageContext.request.contextPath}"/>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello ${name}!</title>
    <link href="${contextPath}/resources/css/main.css" rel="stylesheet">
</head>
<body>
    <h2 class="hello-title">Hello ${name}!</h2>
    <script src="${contextPath}/resources/js/main.js"></script>
</body>
</html>

Static files

main.css

.hello-title{
    color: darkgreen;
}

main.js

(function(){
    console.log("Hello World!");
})();

Config and Run

XML Configurations

A Spring MVC XML application usually has several XML configuration files, but there should have one bootstrap file (appconfig-root.xml). This bootstrap file should use the <import resource="" /> to include other config files.

appconfig-root.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="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">
    <import resource="appconfig-mvc.xml"/>

    <context:component-scan base-package="com.hellokoding.*"/>
    <context:property-placeholder location="classpath:application.properties"/>
</beans>

appconfig-mvc.xml

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns="http://www.springframework.org/schema/beans"
       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">
    <mvc:annotation-driven/>
    <mvc:resources mapping="/resources/**" location="/resources/"/>

    <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>

web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
    <display-name>Hello Spring MVC</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/appconfig-root.xml</param-value>
    </context-param>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value></param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

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

Run with Maven and Jetty

Type the below command at the project root directory and visit to localhost:8080

mvn clean jetty:run

Source code

https://github.com/hellokoding/hellokoding-courses/tree/master/spring-mvc-examples/springmvc-xml-jsp

Follow HelloKoding