Add code in XML file “WEB-INF/web.xml” for defining and configuring Spring MVC dispatcher servlet(s).
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>spring-mvc-demo</display-name>
<!-- Spring MVC Configs -->
<!-- Step 1: Configure Spring MVC Dispatcher Servlet -->
<servlet>
<servlet-name> jbkDispatcher </servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc-demo-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Step 2: Set up URL mapping of Spring MVC Dispatcher Servlet -->
<servlet-mapping>
<servlet-name>jbkDispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Then, we setup URL mapping of dispatcher servlet(s) we’ve just defined. This means all requests coming in (since the url-pattern is “/” below) should be handled by dispatcher called “JbkDispatcher”.
Now, when you make a request, this will be mapped to a specific dispatcher. And so, Spring will load the configuration of that dispatcher from “WEB-INF” folder. In this case, our file is “WEB-INF/spring-mvc-demo-servlet.xml”.
We can add support for features like Spring component scanning, conversions, formatting, validation, configure Spring MVC View Resolver.
<?xml version="1.0" encoding="UTF-8"?>
<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/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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- Step 3: Add support for component scanning -->
<context:component-scan base-package="com.javabykiran" />
<!-- Step 4: Add support for conversion, formatting and validation support -->
<mvc:annotation-driven />
<!-- Step 5: Define Spring MVC view resolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- A handler for serving static resources -->
<mvc:resources mapping="/assets/**" location="/assets/" />
</beans>
The “prefix” and “suffix” will be used to render the view page.
We need to create “view” folder inside the “WEB-INF” folder.
I’m assuming you’ve created a package called “com.javabykiran” where all of your Java classes live.
Any static resource is processed as a URL mapping in Spring MVC.
You can configure references to static resources by adding the following entry to the configuration file of the Dispatcher Servlet.
<!-- A handler for serving static resources -->
<mvc:resources mapping="/assets/**" location="/assets/" />
The double asterisk “**” means to look also for the subdirectory.
In your view pages, we can access the static files using one of the following:
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/assets/css/main.css">
OR
<!-- Include the core JSTL tags (will be discussed later) -->
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<!-- ... -->
<link rel="stylesheet" type="text/css" href="< c:url value="/assets/css/main.css" />">