官术网_书友最值得收藏!

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:

  1. Create web.xml under the src/main/webapp/WEB-INF/ directory in your project and enter the following content inside web.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>
  2. Now create one more xml file called DefaultServlet-servlet.xml under the same src/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.

主站蜘蛛池模板: 井冈山市| 茶陵县| 临武县| 青冈县| 万载县| 毕节市| 邢台县| 阳曲县| 手游| 卢龙县| 盘山县| 赤城县| 清徐县| 且末县| 阿瓦提县| 陆良县| 凤庆县| 海口市| 大悟县| 通辽市| 乌兰县| 同德县| 任丘市| 山丹县| 玉林市| 盱眙县| 马公市| 和平县| 高碑店市| 岑巩县| 亚东县| 双辽市| 天全县| 通州区| 泗阳县| 贞丰县| 康平县| 海阳市| 黑水县| 彰武县| 广丰县|