1. Spring实现web模块; 简化web开发

    POJO: Plain Old Java Object


    1. 导包
      Spring核心模块:
    1. <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    2. <dependency>
    3. <groupId>org.springframework</groupId>
    4. <artifactId>spring-context</artifactId>
    5. <version>4.0.0.RELEASE</version>
    6. </dependency>
    7. <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
    8. <dependency>
    9. <groupId>org.springframework</groupId>
    10. <artifactId>spring-core</artifactId>
    11. <version>4.0.0.RELEASE</version>
    12. </dependency>
    13. <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
    14. <dependency>
    15. <groupId>org.springframework</groupId>
    16. <artifactId>spring-beans</artifactId>
    17. <version>4.0.0.RELEASE</version>
    18. </dependency>
    19. <!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
    20. <dependency>
    21. <groupId>org.springframework</groupId>
    22. <artifactId>spring-expression</artifactId>
    23. <version>4.0.0.RELEASE</version>
    24. </dependency>
    25. <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
    26. <dependency>
    27. <groupId>commons-logging</groupId>
    28. <artifactId>commons-logging</artifactId>
    29. <version>1.1.1</version>
    30. </dependency>

    支持注解的aop包:

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>4.0.0.RELEASE</version>
    </dependency>
    

    导入web模块的包: spring-web, spring-webmvc

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.0.0.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.0.0.RELEASE</version>
    </dependency>
    
    1. web.xml配置

    SpringMVC有一个前端控制器能拦截所有请求并智能派发, 实际上是一个servlet, 所以需要在web.xml中配置

    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <!--servlet启动加载-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <!--
            /*和/都是拦截所有请求
            /会拦截所有请求, 但是不会拦截*.jsp
            /*的范围更大, 还会拦截到*.jsp这些请求
        -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>