1.导入jar包。
    下载网址:https://repo.spring.io/release/org/springframework/spring/

    2.配置web.xml

    ```jsx springmvc org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:springmvc-servlet.xml

    springmvc /

    1. 3.添加springmvc配置文件,默认在WEB-INF下。文件名默认为 [servlet名(步骤二servlet-name)]-servlet.xml。例如:springmvc-servlet.xml
    2. 4.编写springmvc配置文件
    3. ```xml
    4. <?xml version="1.0" encoding="UTF-8"?>
    5. <beans xmlns="http://www.springframework.org/schema/beans"
    6. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    7. xmlns:context="http://www.springframework.org/schema/context"
    8. xmlns:mvc="http://www.springframework.org/schema/mvc"
    9. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    10. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
    11. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
    12. <!-- scan the package and the sub package -->
    13. <context:component-scan base-package="test.SpringMVC"/>
    14. <!-- don't handle the static resource -->
    15. <mvc:default-servlet-handler />
    16. <!-- if you use annotation you must configure following setting -->
    17. <mvc:annotation-driven />
    18. <!-- configure the InternalResourceViewResolver -->
    19. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    20. id="internalResourceViewResolver">
    21. <!-- 前缀 -->
    22. <property name="prefix" value="/WEB-INF/jsp/" />
    23. <!-- 后缀 -->
    24. <property name="suffix" value=".jsp" />
    25. </bean>
    26. </beans>

    5.在WEB-INF文件夹下创建名为jsp的文件夹,用来存放jsp视图。创建一个hello.jsp,在body中添加“Hello World”。

    6.建立包以及mvcController.java文件。整体布局如下。

    image.png

    6.mvcController.java文件编写代码。

    1. @Controller
    2. @RequestMapping("/mvc")
    3. public class mvcController {
    4. @RequestMapping("/hello")
    5. public String hello(){
    6. return "hello";
    7. }
    8. }

    7.运行网址:http://localhost:8080/项目名/mvc/hello