- 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.
- 從零開始學(xué)Hadoop大數(shù)據(jù)分析(視頻教學(xué)版)
- 業(yè)務(wù)數(shù)據(jù)分析:五招破解業(yè)務(wù)難題
- 大數(shù)據(jù)架構(gòu)和算法實(shí)現(xiàn)之路:電商系統(tǒng)的技術(shù)實(shí)戰(zhàn)
- 數(shù)據(jù)挖掘原理與SPSS Clementine應(yīng)用寶典
- AI時(shí)代的數(shù)據(jù)價(jià)值創(chuàng)造:從數(shù)據(jù)底座到大模型應(yīng)用落地
- Hadoop大數(shù)據(jù)開發(fā)案例教程與項(xiàng)目實(shí)戰(zhàn)(在線實(shí)驗(yàn)+在線自測(cè))
- Instant Autodesk AutoCAD 2014 Customization with .NET
- 深入理解InfluxDB:時(shí)序數(shù)據(jù)庫(kù)詳解與實(shí)踐
- Access 2016數(shù)據(jù)庫(kù)應(yīng)用基礎(chǔ)
- MySQL數(shù)據(jù)庫(kù)應(yīng)用與管理
- 云工作時(shí)代:科技進(jìn)化必將帶來(lái)的新工作方式
- 大數(shù)據(jù)隱私保護(hù)技術(shù)與治理機(jī)制研究
- 成功之路:ORACLE 11g學(xué)習(xí)筆記
- Configuration Management with Chef-Solo
- C# 7 and .NET Core 2.0 High Performance