SpringMVC入门

你的名字 2022-01-25 18:29 390阅读 0赞

一、引入依赖

引入依赖
jsp的依赖

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.william</groupId>
  6. <artifactId>springmvc_day01_1_helloworld</artifactId>
  7. <version>1.0-SNAPSHOT</version>
  8. <packaging>war</packaging>
  9. <name>springmvc_day01_1_helloworld Maven Webapp</name>
  10. <!-- FIXME change it to the project's website -->
  11. <url>http://www.example.com</url>
  12. <properties>
  13. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  14. <maven.compiler.source>1.7</maven.compiler.source>
  15. <maven.compiler.target>1.7</maven.compiler.target>
  16. </properties>
  17. <dependencies>
  18. <!--springMVC的依赖-->
  19. <dependency>
  20. <groupId>org.springframework</groupId>
  21. <artifactId>spring-webmvc</artifactId>
  22. <version>5.0.2.RELEASE</version>
  23. </dependency>
  24. <!--有关servlet的依赖-->
  25. <dependency>
  26. <groupId>javax.servlet</groupId>
  27. <artifactId>servlet-api</artifactId>
  28. <version>2.5</version>
  29. <!--
  30. 依赖范围:jar的生存范围
  31. provided: 在运行期是无效的,在编译器和测试器有效
  32. -->
  33. <scope>provided</scope>
  34. </dependency>
  35. <!--有关jsp页面的依赖-->
  36. <dependency>
  37. <groupId>javax.servlet</groupId>
  38. <artifactId>jsp-api</artifactId>
  39. <version>2.0</version>
  40. <scope>provided</scope>
  41. </dependency>
  42. </dependencies>
  43. <build>
  44. <finalName>springmvc_day01_1_helloworld</finalName>
  45. <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
  46. <plugins>
  47. <plugin>
  48. <artifactId>maven-clean-plugin</artifactId>
  49. <version>3.1.0</version>
  50. </plugin>
  51. <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
  52. <plugin>
  53. <artifactId>maven-resources-plugin</artifactId>
  54. <version>3.0.2</version>
  55. </plugin>
  56. <plugin>
  57. <artifactId>maven-compiler-plugin</artifactId>
  58. <version>3.8.0</version>
  59. </plugin>
  60. <plugin>
  61. <artifactId>maven-surefire-plugin</artifactId>
  62. <version>2.22.1</version>
  63. </plugin>
  64. <plugin>
  65. <artifactId>maven-war-plugin</artifactId>
  66. <version>3.2.2</version>
  67. </plugin>
  68. <plugin>
  69. <artifactId>maven-install-plugin</artifactId>
  70. <version>2.5.2</version>
  71. </plugin>
  72. <plugin>
  73. <artifactId>maven-deploy-plugin</artifactId>
  74. <version>2.8.2</version>
  75. </plugin>
  76. </plugins>
  77. </pluginManagement>
  78. </build>
  79. </project>

二、目录结构

目录结构

三、配置web.xml

配置web.xml
拦截地址栏以.do结尾的请求

  1. <!DOCTYPE web-app PUBLIC
  2. "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  3. "http://java.sun.com/dtd/web-app_2_3.dtd" >
  4. <web-app>
  5. <display-name>Archetype Created Web Application</display-name>
  6. <!--前端控制器: springMVC的入口(servlet)-->
  7. <servlet>
  8. <servlet-name>DispatcherServlet</servlet-name>
  9. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  10. <!--指定springmvc配置文件-->
  11. <init-param>
  12. <param-name>contextConfigLocation</param-name>
  13. <param-value>classpath:spring-mvc.xml</param-value>
  14. </init-param>
  15. <!--在tomcat启动时创建servlet, 如果没有配置,在第一次访问时创建servlet-->
  16. <load-on-startup>1</load-on-startup>
  17. </servlet>
  18. <servlet-mapping>
  19. <servlet-name>DispatcherServlet</servlet-name>
  20. <!--
  21. / :拦截所有的请求和资源, 后期需要对象资源进行放行
  22. /* : 拦截所有的请求和html(springMVC中不推荐是用 /*)
  23. *.do *.action *.go: 只会拦截.do ,.action, .go 请求( 由struts2框架借鉴的)
  24. -->
  25. <url-pattern>*.do</url-pattern>
  26. </servlet-mapping>
  27. </web-app>

四、配置spring-mvc.xml

配置spring-mvc.xml
在这里插入图片描述

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context.xsd
  10. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  11. <!--开启注解扫描-->
  12. <context:component-scan base-package="com.william"></context:component-scan>
  13. <!--内部资源视图解析器-->
  14. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  15. <!--前缀-->
  16. <property name="prefix" value="/WEB-INF/"></property>
  17. <!--后缀-->
  18. <property name="suffix" value=".jsp"></property>
  19. </bean>
  20. <!--引入 处理器映射器,处理器适配器, 引入springMVC丰富的功能-->
  21. <mvc:annotation-driven></mvc:annotation-driven>
  22. </beans>

在浏览器端验证

启动tomcat发布,输入/hello.do

启动tomcat发布,输入/hello.do

发表评论

表情:
评论列表 (有 0 条评论,390人围观)

还没有评论,来说两句吧...

相关阅读