- Spring MVC Beginner’s Guide
- Amuthan G
- 356字
- 2021-07-16 11:25:42
Time for action – configuring the dispatcher servlet
The dispatcher servlet is what examines the incoming request URL and invokes the right corresponding controller method. In our case, the welcome
method from the HomeController
class needs to be invoked if we enter the http://localhost:8080/webstore/
URL on the browser. So let's configure the dispatcher servlet for our project:
- Create
web.xml
under thesrc/main/webapp/WEB-INF/
directory in your project and enter the following content insideweb.xml
and save it:<web-app version="3.0" 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_3_0.xsd"> <servlet> <servlet-name>DefaultServlet</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>DefaultServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
- Now create one more xml file called
DefaultServlet-servlet.xml
under the samesrc/main/webapp/WEB-INF/
directory and enter the following content into it and save it:<?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-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <mvc:annotation-driven /> <context:component-scan base-package="com.packt.webstore" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
What just happened?
If you know about servlet programming, you might be quite familiar with the servlet configuration and web.xml
. In web.xml
, we configured a servlet named DefaultServlet
, which is more or less similar to any other normal servlet configuration. The only difference is that we have not created any servlet class for that configuration. Instead, the servlet class (org.springframework.web.servlet.DispatcherServlet
) is provided by the Spring MVC framework, and we make use of it in web.xml
. After this step, our configured DispatcherServlet
(DefaultServlet
) will be ready to handle any requests that come to our application on runtime and will dispatch the request to the correct controller's method.
However, DispatcherServlet
should know where our controllers and view files are located in our project, and only then can it properly dispatch the request to the correct controllers. So we have to give some hint to DispatcherServlet
to locate the controllers and view files. This is what we configured in step 2 through the DispatcherServlet-servlet.xml
file.
Don't worry if you are not able to understand each and every configuration in web.xml
and DispatcherServlet-servlet.xml
; we will take a look at these configuration files in next chapter. As of now, just remember that this is a one-time configuration that is needed to run our project successfully.
Deploying our project
We successfully created the project in the last section, so you might be curious to know what would happen if we run our project now. As our project is a web project, we need a web server to run it.
- PyTorch深度學習實戰:從新手小白到數據科學家
- 虛擬化與云計算
- Libgdx Cross/platform Game Development Cookbook
- INSTANT Cytoscape Complex Network Analysis How-to
- Learning Proxmox VE
- 金融商業算法建模:基于Python和SAS
- 淘寶、天貓電商數據分析與挖掘實戰(第2版)
- Hadoop集群與安全
- 計算機視覺
- 智能與數據重構世界
- Visual Studio 2012 and .NET 4.5 Expert Development Cookbook
- 大數據技術體系詳解:原理、架構與實踐
- 一本書讀懂大數據
- 數據分析方法及應用:基于SPSS和EXCEL環境
- Managing Software Requirements the Agile Way