Friday, 9 November 2018

Spring web mvc: Specify context configuration location to dispatcher servlet

This is continuation to my previous post. As you see web.xml file from my previous post, I am redirecting all my requests to org.springframework.web.servlet.DispatcherServlet.

<servlet>
         <servlet-name>HelloWorld</servlet-name>
         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
         <load-on-startup>1</load-on-startup>
</servlet>

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

As you see above configuration, I do not specify any config file location there. If you do not specify any config file location, then spring will check for the file {servlet-name}-servlet.xml in WEB-INF directory.

Suppose if you want to specify the config file location manually, (or) if you have configurations split across multiple files, then you need to specify them using the init parameter contextConfigLocation.

<servlet>
         <servlet-name>HelloWorld</servlet-name>
         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

         <init-param>
                  <param-name>contextConfigLocation</param-name>
                  <param-value>/WEB-INF/myWebAppConfigurations.xml</param-value>
         </init-param>
         <load-on-startup>1</load-on-startup>
</servlet>

At the time of application startup, spring reads the context file specified by the <init-param> contextConfigLocation.

Find the below working example.

HelloWorldController.java

package com.sample.myApp.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("/hello")
public class HelloWorldController {

 @RequestMapping(method = RequestMethod.GET)
 public String printHello(ModelMap model) {
  model.addAttribute("message", "Welcome to Spring MVC framework");

  return "hello";
 }

}

Create ‘hello.jsp’ file under WEB-INF/jsp folder.

hello.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello World Spring Web MVC</title>
</head>
<body>
 <h2>${message}</h2>
</body>
</html>

Define myWebAppConfigurations.xml file under WEB-INF directory.


myWebAppConfigurations.xml

<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.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 <context:component-scan base-package="com.sample.myApp" />

 <bean
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/WEB-INF/jsp/" />
  <property name="suffix" value=".jsp" />
 </bean>

</beans>

web.xml

<web-app id="WebApp_ID" version="2.4"
 xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

 <display-name>Spring MVC Hello WorldApplication</display-name>

 <servlet>
  <servlet-name>HelloWorld</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/myWebAppConfigurations.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>

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

</web-app>

index.jsp

<html>
<body>
 <h2>Hello World!</h2>
</body>
</html>

Complete project structure looks like below.


Run the application on server and hit the url http://localhost:8080/springdemo/hello.

You can able to see the message 'Welcome to Spring MVC framework' in the browser.

Can I specify multiple configuration files?
Yes, you can specify by comma separating them.

Previous                                                 Next                                                 Home

No comments:

Post a Comment