借助它可以快速完成一个web项目。
    maven初始化一个web项目,调整web.xml .

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    5. version="4.0">
    6. <context-param>
    7. <param-name>contextConfigLocation</param-name>
    8. <param-value>classpath:applicationContext.xml</param-value>
    9. </context-param>
    10. <listener>
    11. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    12. </listener>
    13. <servlet>
    14. <servlet-name>dispatcherServlet</servlet-name>
    15. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    16. <init-param>
    17. <param-name>contextConfigLocation</param-name>
    18. <param-value>classpath:applicationContext.xml</param-value>
    19. </init-param>
    20. <load-on-startup>1</load-on-startup>
    21. <async-supported>true</async-supported>
    22. </servlet>
    23. <servlet-mapping>
    24. <servlet-name>dispatcherServlet</servlet-name>
    25. <url-pattern>/</url-pattern>
    26. </servlet-mapping>
    27. </web-app>

    容器的配置文件 applicationContext.xml .

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xmlns:mvc="http://www.springframework.org/schema/mvc"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. xmlns="http://www.springframework.org/schema/beans"
    6. xsi:schemaLocation="http://www.springframework.org/schema/mvc
    7. http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
    8. http://www.springframework.org/schema/context
    9. http://www.springframework.org/schema/context/spring-context-4.2.xsd
    10. http://www.springframework.org/schema/beans
    11. http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
    12. <context:component-scan base-package="cn.lichenghao"/>
    13. <mvc:annotation-driven/>
    14. <!--视图解析器-->
    15. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    16. <!-- 需要在WEB-INF下创建jsp文件夹存放视图 -->
    17. <property name="prefix" value="/WEB-INF/views/"/>
    18. <property name="suffix" value=".jsp"/>
    19. </bean>
    20. <mvc:default-servlet-handler/>
    21. </beans>

    新建个测试页面

    1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    2. <html>
    3. <head>
    4. <title>Hello world!</title>
    5. </head>
    6. <body>
    7. Hello world!
    8. </body>
    9. </html>

    新建个页面请求

    1. package cn.lichenghao.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. @Controller
    5. public class HomeController {
    6. @RequestMapping("/")
    7. public String home() {
    8. return "index";
    9. }
    10. }

    启动访问即可。 演示项目地址:spring-framework-5.2.22.RELEASE/spring-web-lich-test .

    基于Servlet简化了复杂的操作,还记得Servlet怎么操作吗?(新建请求处理,添加请求映射,然后建很多个……) .