News Leaflets
A leading news portal.

Spring – Change DispatcherServlet Context Configuration File Name

0 54

DispatcherServlet acts as the Front Controller for Spring-based web applications. So now what is Front Controller? So it is pretty simple. Any request is going to come into our website the front controller is going to stand in front and is going to accept all the requests and once the front controller accepts that request then this is the job of the front controller that it will make a decision that who is the right controller to handle that request. For example, refer to the below image. Suppose we have a website called student.com and the client is make a request to save student data by hitting the following URL “student.com/save” and its first come to the front controller and once the front controller accepts that request it is going to assign to the Cointroller_1 as this controller handle the request for /save operation. Then it is going to return back the response to the Client. 

In this article What is Dispatcher Servlet in Spring? we have seen that we have to create a DispatcherServlet Context Configuration File inside the src > main > webapp > WEB-INF. And the name of the file must be in this format

YourServeltName-servelt.xml  

But what happens if we change this format name to another one. Can we change the DispatcherServlet context configuration file name? So let’s see by an example. 

Implementation: Project

Setting Up the Project

Note: We are going to use Spring Tool Suite 4 IDE for this project. Please refer to this article to install STS in your local machine How to Download and Install Spring Tool Suite (Spring Tools 4 for Eclipse) IDE? 

  • Step 1: Create a Dynamic Web Project in your STS IDE. You may refer to this article to create a Dynamic Web Project in STS: How to Create a Dynamic Web Project in Spring Tool Suite?
  • Step 2: Download the spring JARs file from this link and go to the src > main > webapp > WEB-INF > lib folder and past these JAR files.
  • Step 3: Refer to this article Configuration of Apache Tomcat Server and configure the tomcat server with your application. Now we are ready to go.

Configuring Dispatcher Servlet

Step 4: Now go to the src > main > webapp > WEB-INF > web.xml file and here we have to configure our front controller inside a <servlet>…</servlet> tag something like this. 

<servlet>

      <!-- Provide a Servlet Name -->
    <servlet-name>frontcontroller-dispatcher</servlet-name>
    
    <!-- Provide a fully qualified path to the DispatcherServelt class -->
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    

</servlet>

Now let’s tell this servlet, hey servlet you have to handle all the requests coming to our website called student.com (for this example). So the way we are going to tell the above servlet is we can write something like this

<servlet-mapping>

      <!-- Provide a Servlet Name that you want to map -->
    <servlet-name>frontcontroller-dispatcher</servlet-name>
    
    <!-- Provide a url pattern -->
    <url-pattern>/student.com/*</url-pattern>
    
</servlet-mapping>

So this does mean that the servlet “frontcontroller-dispatcher” is going to handle all the requests starting from student.com/anything, that may be student.com/save or student.com/get, anything. But it must start with student.com. So we are done with creating a Dispatcher Servlet. 

File: web.xml

XML

<?xml version="1.0" encoding="UTF-8"?>

    

  <display-name>myfirst-mvc-project</display-name>

  <welcome-file-list>

    <welcome-file>index.html</welcome-file>

    <welcome-file>index.jsp</welcome-file>

    <welcome-file>index.htm</welcome-file>

    <welcome-file>default.html</welcome-file>

    <welcome-file>default.jsp</welcome-file>

    <welcome-file>default.htm</welcome-file>

  </welcome-file-list>

    

  <absolute-ordering/>

    

  <servlet>

      

    <servlet-name>frontcontroller-dispatcher</servlet-name>

    

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

  </servlet>

    

  <servlet-mapping>

      

    <servlet-name>frontcontroller-dispatcher</servlet-name>

    

    <url-pattern>/student.com/*</url-pattern>

  </servlet-mapping>

    

</web-app>

Now, let’s run our application. To run your Spring MVC Application right-click on your project > Run As > Run on Server and run your application as shown in the below image. 

And in this scenario, you are going to get the following exception

IOException parsing XML document from ServletContext resource [/WEB-INF/frontcontroller-dispatcher-servlet.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/frontcontroller-dispatcher-servlet.xml]
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:342)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:310)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:224)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:195)
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125)
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130)

So from the first line, you can see it’s a java.io.FileNotFoundException which means Spring is looking for this specific file frontcontroller-dispatcher-servlet.xml inside the WEB-INF directory. So now let’s create this file. 

Step 5: Now go to the  src > main > webapp > WEB-INF and create an XML file. Actually, this is a Spring Configuration file like beans.xml file. And the name of the file must be in this format.

YourServeltName-servlet.xml  

For example, for this project, the name of the file must be 

frontcontroller-dispatcher-servlet.xml

So either you can create a Spring Configuration File or you can just create a simple XML file add the below lines of code inside that file. 

File: frontcontroller-dispatcher-servlet.xml

The project file structure will appear as follows: 

project structure

Step 6: Now again run your application. Now in the Console, you can see our DispatcherServlet has successfully initialized and also initialization completed without any exceptions or errors. 

But can we provide another name? Yes, you can, but you have to write the following lines of code if you are changing the DispatcherServlet Context Configuration File Name. Suppose we have changed the name from frontcontroller-dispatcher-servlet.xml to dispatcher-servlet.xml then we have to write something like this 

<init-param>
     <param-name>contextConfigLocation</param-name>
       <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</init-param>

Output explanation:

<init-param> is a static parameter that is stored in the web.xml file. If you have any data which doesn’t change frequently you can store it in one of them. If you want to store particular data which is confined to a particular servlet scope, then you can use <init-param>. Anything you declare inside <init-param> is only accessible only for that particular servlet. The init-param is declared inside the <servlet> tag. 

For example, for this project, we can write something like as follows: 

<servlet>
      <!-- Provide a Servlet Name -->
    <servlet-name>frontcontroller-dispatcher</servlet-name>
    <!-- Provide a fully qualified path to the DispatcherServelt class -->
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </init-param>
  </servlet>

File: web.xml 

XML

<?xml version="1.0" encoding="UTF-8"?>

    

  <display-name>myfirst-mvc-project</display-name>

  <welcome-file-list>

    <welcome-file>index.html</welcome-file>

    <welcome-file>index.jsp</welcome-file>

    <welcome-file>index.htm</welcome-file>

    <welcome-file>default.html</welcome-file>

    <welcome-file>default.jsp</welcome-file>

    <welcome-file>default.htm</welcome-file>

  </welcome-file-list>

    

  <absolute-ordering/>

    

  <servlet>

      

    <servlet-name>frontcontroller-dispatcher</servlet-name>

    

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

    <init-param>

        <param-name>contextConfigLocation</param-name>

        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>

    </init-param>

  </servlet>

    

  <servlet-mapping>

      

    <servlet-name>frontcontroller-dispatcher</servlet-name>

    

    <url-pattern>/student.com/*</url-pattern>

  </servlet-mapping>

    

</web-app>

The modified project file structure is given below. 

Step 7: Now again run your application. Now in the Console, you can see our DispatcherServlet has successfully initialized and also initialization completed without any exceptions or errors. 

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! News Leaflets is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.
Leave a comment