Spring MVC官方文档学习笔记(一)之Web入门

打印 上一主题 下一主题

主题 876|帖子 876|积分 2638

注: 该章节主要为原创内容,为后续的Spring MVC内容做一个先行铺垫
1.Servlet的构建使用
(1) 选择Maven -> webapp来构建一个web应用
[img=30%,30%]https://img2023.cnblogs.com/blog/3062734/202305/3062734-20230509222021414-1725326899.png[/img](2) 构建好后,打开pom.xml文件,一要注意打包方式为war包,二导入servlet依赖,如下
  1. <packaging>war</packaging>
  2. <dependencies>
  3. <?xml version="1.0" encoding="UTF-8"?>
  4. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  5.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  7.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  8.          version="3.1">
  9.   
  10. </web-app><dependency>
  11. <?xml version="1.0" encoding="UTF-8"?>
  12. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  13.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  14.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  15.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  16.          version="3.1">
  17.   
  18. </web-app><?xml version="1.0" encoding="UTF-8"?>
  19. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  20.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  21.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  22.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  23.          version="3.1">
  24.   
  25. </web-app><groupId>javax.servlet</groupId>
  26. <?xml version="1.0" encoding="UTF-8"?>
  27. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  28.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  29.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  30.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  31.          version="3.1">
  32.   
  33. </web-app><?xml version="1.0" encoding="UTF-8"?>
  34. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  35.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  36.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  37.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  38.          version="3.1">
  39.   
  40. </web-app><artifactId>javax.servlet-api</artifactId>
  41. <?xml version="1.0" encoding="UTF-8"?>
  42. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  43.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  44.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  45.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  46.          version="3.1">
  47.   
  48. </web-app><?xml version="1.0" encoding="UTF-8"?>
  49. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  50.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  51.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  52.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  53.          version="3.1">
  54.   
  55. </web-app><version>3.1.0</version>
  56. <?xml version="1.0" encoding="UTF-8"?>
  57. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  58.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  59.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  60.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  61.          version="3.1">
  62.   
  63. </web-app><?xml version="1.0" encoding="UTF-8"?>
  64. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  65.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  66.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  67.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  68.          version="3.1">
  69.   
  70. </web-app><scope>provided</scope>
  71. <?xml version="1.0" encoding="UTF-8"?>
  72. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  73.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  74.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  75.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  76.          version="3.1">
  77.   
  78. </web-app></dependency>
  79. </dependencies>
复制代码
(3) 替换webapp/WEB-INF/web.xml文件为如下内容,采用Servlet 3.1版本
  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
  5.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  6.          version="3.1">
  7.   
  8. </web-app>
复制代码
(4) 在main目录下,新建java目录和resources目录,并在java目录下新建包,最终项目目录结构如下
[img=30%,30%]https://img2023.cnblogs.com/blog/3062734/202305/3062734-20230509223138766-401565015.png[/img](5) 编写一个简单的servlet如下
  1. @WebServlet("/example")public class ExampleServlet extends HttpServlet {<?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
  5.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  6.          version="3.1">
  7.   
  8. </web-app><?xml version="1.0" encoding="UTF-8"?>
  9. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  10.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  11.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  12.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  13.          version="3.1">
  14.   
  15. </web-app>@Override<?xml version="1.0" encoding="UTF-8"?>
  16. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  17.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  18.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  19.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  20.          version="3.1">
  21.   
  22. </web-app><?xml version="1.0" encoding="UTF-8"?>
  23. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  24.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  25.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  26.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  27.          version="3.1">
  28.   
  29. </web-app>protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {<?xml version="1.0" encoding="UTF-8"?>
  30. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  31.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  32.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  33.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  34.          version="3.1">
  35.   
  36. </web-app><?xml version="1.0" encoding="UTF-8"?>
  37. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  38.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  39.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  40.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  41.          version="3.1">
  42.   
  43. </web-app><?xml version="1.0" encoding="UTF-8"?>
  44. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  45.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  46.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  47.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  48.          version="3.1">
  49.   
  50. </web-app><?xml version="1.0" encoding="UTF-8"?>
  51. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  52.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  53.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  54.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  55.          version="3.1">
  56.   
  57. </web-app>resp.getWriter().write("example...");<?xml version="1.0" encoding="UTF-8"?>
  58. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  59.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  60.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  61.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  62.          version="3.1">
  63.   
  64. </web-app><?xml version="1.0" encoding="UTF-8"?>
  65. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  66.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  67.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  68.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  69.          version="3.1">
  70.   
  71. </web-app>}}
复制代码
(6) 有了servlet后,我们得让服务器知道哪个请求要交给哪个servlet处理,因此还需要配置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
  5.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  6.          version="3.1">
  7.   
  8. </web-app><?xml version="1.0" encoding="UTF-8"?>
  9. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  10.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  11.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  12.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  13.          version="3.1">
  14.   
  15. </web-app><?xml version="1.0" encoding="UTF-8"?>
  16. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  17.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  18.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  19.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  20.          version="3.1">
  21.   
  22. </web-app><?xml version="1.0" encoding="UTF-8"?>
  23. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  24.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  25.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  26.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  27.          version="3.1">
  28.   
  29. </web-app><?xml version="1.0" encoding="UTF-8"?>
  30. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  31.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  32.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  33.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  34.          version="3.1">
  35.   
  36. </web-app><?xml version="1.0" encoding="UTF-8"?>
  37. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  38.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  39.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  40.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  41.          version="3.1">
  42.   
  43. </web-app><?xml version="1.0" encoding="UTF-8"?>
  44. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  45.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  46.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  47.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  48.          version="3.1">
  49.   
  50. </web-app><?xml version="1.0" encoding="UTF-8"?>
  51. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  52.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  53.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  54.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  55.          version="3.1">
  56.   
  57. </web-app>exampleServlet<?xml version="1.0" encoding="UTF-8"?>
  58. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  59.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  60.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  61.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  62.          version="3.1">
  63.   
  64. </web-app><?xml version="1.0" encoding="UTF-8"?>
  65. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  66.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  67.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  68.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  69.          version="3.1">
  70.   
  71. </web-app><?xml version="1.0" encoding="UTF-8"?>
  72. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  73.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  74.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  75.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  76.          version="3.1">
  77.   
  78. </web-app><?xml version="1.0" encoding="UTF-8"?>
  79. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  80.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  81.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  82.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  83.          version="3.1">
  84.   
  85. </web-app>cn.example.springmvc.boke.servlet.ExampleServlet<?xml version="1.0" encoding="UTF-8"?>
  86. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  87.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  88.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  89.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  90.          version="3.1">
  91.   
  92. </web-app><?xml version="1.0" encoding="UTF-8"?>
  93. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  94.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  95.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  96.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  97.          version="3.1">
  98.   
  99. </web-app><?xml version="1.0" encoding="UTF-8"?>
  100. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  101.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  102.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  103.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  104.          version="3.1">
  105.   
  106. </web-app><?xml version="1.0" encoding="UTF-8"?>
  107. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  108.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  109.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  110.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  111.          version="3.1">
  112.   
  113. </web-app><?xml version="1.0" encoding="UTF-8"?>
  114. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  115.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  116.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  117.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  118.          version="3.1">
  119.   
  120. </web-app><?xml version="1.0" encoding="UTF-8"?>
  121. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  122.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  123.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  124.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  125.          version="3.1">
  126.   
  127. </web-app><?xml version="1.0" encoding="UTF-8"?>
  128. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  129.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  130.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  131.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  132.          version="3.1">
  133.   
  134. </web-app><?xml version="1.0" encoding="UTF-8"?>
  135. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  136.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  137.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  138.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  139.          version="3.1">
  140.   
  141. </web-app><?xml version="1.0" encoding="UTF-8"?>
  142. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  143.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  144.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  145.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  146.          version="3.1">
  147.   
  148. </web-app><?xml version="1.0" encoding="UTF-8"?>
  149. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  150.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  151.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  152.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  153.          version="3.1">
  154.   
  155. </web-app><?xml version="1.0" encoding="UTF-8"?>
  156. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  157.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  158.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  159.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  160.          version="3.1">
  161.   
  162. </web-app><?xml version="1.0" encoding="UTF-8"?>
  163. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  164.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  165.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  166.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  167.          version="3.1">
  168.   
  169. </web-app>exampleServlet<?xml version="1.0" encoding="UTF-8"?>
  170. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  171.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  172.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  173.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  174.          version="3.1">
  175.   
  176. </web-app><?xml version="1.0" encoding="UTF-8"?>
  177. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  178.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  179.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  180.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  181.          version="3.1">
  182.   
  183. </web-app><?xml version="1.0" encoding="UTF-8"?>
  184. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  185.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  186.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  187.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  188.          version="3.1">
  189.   
  190. </web-app><?xml version="1.0" encoding="UTF-8"?>
  191. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  192.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  193.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  194.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  195.          version="3.1">
  196.   
  197. </web-app>/<?xml version="1.0" encoding="UTF-8"?>
  198. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  199.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  200.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  201.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  202.          version="3.1">
  203.   
  204. </web-app><?xml version="1.0" encoding="UTF-8"?>
  205. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  206.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  207.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  208.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  209.          version="3.1">
  210.   
  211. </web-app>
复制代码
(7) 然后,为了能够在网页上访问,我们得把这个项目部署到tomcat服务器中
首先,在URL栏中,添加上项目名称,此处为springmvc
[img=30%,30%]https://img2023.cnblogs.com/blog/3062734/202305/3062734-20230509224914750-1271790275.png[/img]然后,在Deployment中添加我们的项目
[img=30%,30%]https://img2023.cnblogs.com/blog/3062734/202305/3062734-20230509225123489-498372496.png[/img]最后,注意 Application Context 中的值,应与前面在URL栏中添加的项目名称相同,此处均为springmvc
[img=30%,30%]https://img2023.cnblogs.com/blog/3062734/202305/3062734-20230509225327945-37398283.png[/img](8) 最后,启动tomcat服务器,在浏览器上输入 http://localhost:8080/springmvc/example ,如果能看到 example... 字符串,则说明项目配置成功
2.基于web.xml,整合Spring与Servlet
(1) 现在,web应用已经搭建好了,但是我们希望能够在该应用中使用Spring容器,该怎么办呢? 在之前的非web环境中,我们都是在main方法中创建ioc容器(如 new ClassPathXmlApplicationContext()),然后直接使用的,但是现在没有了main方法,该由谁来创建ioc容器呢? 答案就是由我们的web容器,可以在web应用初始化的时候来帮助我们创建,但创建好之后,我们该怎么获取到ioc容器呢? Servlet规定了4大作用域,分别为page域(PageContext),当前页面有效; request域(HttpServletContext),一次请求内有效; session域(HttpSession),一次会话内有效; application域(ServletContext),在当前整个web应用内有效,因此我们可以将创建好的ioc容器直接放到application域中,这样在任何位置,我们都能拿到ioc容器进行使用,具体示例如下
首先导入相关的Spring依赖包
  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
  5.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  6.          version="3.1">
  7.   
  8. </web-app><?xml version="1.0" encoding="UTF-8"?>
  9. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  10.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  11.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  12.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  13.          version="3.1">
  14.   
  15. </web-app><?xml version="1.0" encoding="UTF-8"?>
  16. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  17.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  18.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  19.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  20.          version="3.1">
  21.   
  22. </web-app>org.springframework<?xml version="1.0" encoding="UTF-8"?>
  23. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  24.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  25.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  26.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  27.          version="3.1">
  28.   
  29. </web-app><?xml version="1.0" encoding="UTF-8"?>
  30. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  31.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  32.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  33.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  34.          version="3.1">
  35.   
  36. </web-app>spring-context<?xml version="1.0" encoding="UTF-8"?>
  37. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  38.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  39.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  40.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  41.          version="3.1">
  42.   
  43. </web-app><?xml version="1.0" encoding="UTF-8"?>
  44. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  45.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  46.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  47.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  48.          version="3.1">
  49.   
  50. </web-app>5.2.22.RELEASE<?xml version="1.0" encoding="UTF-8"?>
  51. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  52.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  53.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  54.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  55.          version="3.1">
  56.   
  57. </web-app><?xml version="1.0" encoding="UTF-8"?>
  58. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  59.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  60.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  61.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  62.          version="3.1">
  63.   
  64. </web-app><?xml version="1.0" encoding="UTF-8"?>
  65. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  66.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  67.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  68.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  69.          version="3.1">
  70.   
  71. </web-app><?xml version="1.0" encoding="UTF-8"?>
  72. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  73.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  74.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  75.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  76.          version="3.1">
  77.   
  78. </web-app>org.springframework<?xml version="1.0" encoding="UTF-8"?>
  79. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  80.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  81.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  82.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  83.          version="3.1">
  84.   
  85. </web-app><?xml version="1.0" encoding="UTF-8"?>
  86. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  87.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  88.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  89.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  90.          version="3.1">
  91.   
  92. </web-app>spring-web<?xml version="1.0" encoding="UTF-8"?>
  93. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  94.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  95.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  96.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  97.          version="3.1">
  98.   
  99. </web-app><?xml version="1.0" encoding="UTF-8"?>
  100. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  101.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  102.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  103.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  104.          version="3.1">
  105.   
  106. </web-app>5.2.22.RELEASE<?xml version="1.0" encoding="UTF-8"?>
  107. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  108.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  109.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  110.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  111.          version="3.1">
  112.   
  113. </web-app>
复制代码
接着,修改我们的代码,配置一个普通的bean
  1. //创建一个普通的java类public class ExampleService {<?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
  5.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  6.          version="3.1">
  7.   
  8. </web-app><?xml version="1.0" encoding="UTF-8"?>
  9. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  10.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  11.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  12.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  13.          version="3.1">
  14.   
  15. </web-app>public String get() {<?xml version="1.0" encoding="UTF-8"?>
  16. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  17.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  18.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  19.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  20.          version="3.1">
  21.   
  22. </web-app><?xml version="1.0" encoding="UTF-8"?>
  23. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  24.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  25.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  26.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  27.          version="3.1">
  28.   
  29. </web-app><?xml version="1.0" encoding="UTF-8"?>
  30. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  31.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  32.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  33.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  34.          version="3.1">
  35.   
  36. </web-app><?xml version="1.0" encoding="UTF-8"?>
  37. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  38.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  39.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  40.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  41.          version="3.1">
  42.   
  43. </web-app>return "user";<?xml version="1.0" encoding="UTF-8"?>
  44. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  45.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  46.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  47.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  48.          version="3.1">
  49.   
  50. </web-app><?xml version="1.0" encoding="UTF-8"?>
  51. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  52.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  53.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  54.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  55.          version="3.1">
  56.   
  57. </web-app>}}//然后,在resources目录下新建一个springmvc.xml,并将上面的ExampleService注册为一个bean<?xml version="1.0" encoding="UTF-8"?>
  58. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  59.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  60.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  61.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  62.          version="3.1">
  63.   
  64. </web-app><?xml version="1.0" encoding="UTF-8"?>
  65. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  66.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  67.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  68.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  69.          version="3.1">
  70.   
  71. </web-app>
复制代码
接下来,我们就得让web容器来为我们创建ioc容器了,具体由谁来创建呢? Servlet有三大核心组件,即Servlet,用于处理请求;Filter,过滤器,用来拦截或修改请求;Listener,监听器,用于监听某个事件。显然,这里使用Listener最合适,那就由Listener来为我们创建ioc容器
  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
  5.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  6.          version="3.1">
  7.   
  8. </web-app><?xml version="1.0" encoding="UTF-8"?>
  9. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  10.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  11.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  12.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  13.          version="3.1">
  14.   
  15. </web-app><?xml version="1.0" encoding="UTF-8"?>
  16. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  17.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  18.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  19.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  20.          version="3.1">
  21.   
  22. </web-app><?xml version="1.0" encoding="UTF-8"?>
  23. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  24.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  25.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  26.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  27.          version="3.1">
  28.   
  29. </web-app><?xml version="1.0" encoding="UTF-8"?>
  30. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  31.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  32.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  33.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  34.          version="3.1">
  35.   
  36. </web-app><?xml version="1.0" encoding="UTF-8"?>
  37. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  38.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  39.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  40.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  41.          version="3.1">
  42.   
  43. </web-app><?xml version="1.0" encoding="UTF-8"?>
  44. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  45.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  46.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  47.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  48.          version="3.1">
  49.   
  50. </web-app> org.springframework.web.context.ContextLoaderListener<?xml version="1.0" encoding="UTF-8"?>
  51. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  52.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  53.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  54.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  55.          version="3.1">
  56.   
  57. </web-app><?xml version="1.0" encoding="UTF-8"?>
  58. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  59.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  60.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  61.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  62.          version="3.1">
  63.   
  64. </web-app><?xml version="1.0" encoding="UTF-8"?>
  65. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  66.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  67.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  68.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  69.          version="3.1">
  70.   
  71. </web-app><?xml version="1.0" encoding="UTF-8"?>
  72. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  73.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  74.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  75.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  76.          version="3.1">
  77.   
  78. </web-app><?xml version="1.0" encoding="UTF-8"?>
  79. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  80.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  81.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  82.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  83.          version="3.1">
  84.   
  85. </web-app><?xml version="1.0" encoding="UTF-8"?>
  86. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  87.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  88.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  89.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  90.          version="3.1">
  91.   
  92. </web-app><?xml version="1.0" encoding="UTF-8"?>
  93. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  94.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  95.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  96.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  97.          version="3.1">
  98.   
  99. </web-app><?xml version="1.0" encoding="UTF-8"?>
  100. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  101.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  102.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  103.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  104.          version="3.1">
  105.   
  106. </web-app><?xml version="1.0" encoding="UTF-8"?>
  107. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  108.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  109.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  110.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  111.          version="3.1">
  112.   
  113. </web-app><?xml version="1.0" encoding="UTF-8"?>
  114. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  115.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  116.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  117.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  118.          version="3.1">
  119.   
  120. </web-app>contextConfigLocation<?xml version="1.0" encoding="UTF-8"?>
  121. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  122.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  123.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  124.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  125.          version="3.1">
  126.   
  127. </web-app><?xml version="1.0" encoding="UTF-8"?>
  128. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  129.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  130.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  131.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  132.          version="3.1">
  133.   
  134. </web-app><?xml version="1.0" encoding="UTF-8"?>
  135. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  136.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  137.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  138.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  139.          version="3.1">
  140.   
  141. </web-app><?xml version="1.0" encoding="UTF-8"?>
  142. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  143.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  144.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  145.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  146.          version="3.1">
  147.   
  148. </web-app>classpath:springmvc.xml<?xml version="1.0" encoding="UTF-8"?>
  149. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  150.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  151.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  152.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  153.          version="3.1">
  154.   
  155. </web-app><?xml version="1.0" encoding="UTF-8"?>
  156. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  157.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  158.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  159.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  160.          version="3.1">
  161.   
  162. </web-app><?xml version="1.0" encoding="UTF-8"?>
  163. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  164.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  165.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  166.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  167.          version="3.1">
  168.   
  169. </web-app><?xml version="1.0" encoding="UTF-8"?>
  170. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  171.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  172.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  173.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  174.          version="3.1">
  175.   
  176. </web-app><?xml version="1.0" encoding="UTF-8"?>
  177. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  178.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  179.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  180.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  181.          version="3.1">
  182.   
  183. </web-app><?xml version="1.0" encoding="UTF-8"?>
  184. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  185.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  186.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  187.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  188.          version="3.1">
  189.   
  190. </web-app><?xml version="1.0" encoding="UTF-8"?>
  191. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  192.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  193.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  194.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  195.          version="3.1">
  196.   
  197. </web-app><?xml version="1.0" encoding="UTF-8"?>
  198. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  199.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  200.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  201.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  202.          version="3.1">
  203.   
  204. </web-app>exampleServlet<?xml version="1.0" encoding="UTF-8"?>
  205. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  206.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  207.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  208.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  209.          version="3.1">
  210.   
  211. </web-app><?xml version="1.0" encoding="UTF-8"?>
  212. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  213.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  214.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  215.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  216.          version="3.1">
  217.   
  218. </web-app><?xml version="1.0" encoding="UTF-8"?>
  219. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  220.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  221.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  222.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  223.          version="3.1">
  224.   
  225. </web-app><?xml version="1.0" encoding="UTF-8"?>
  226. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  227.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  228.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  229.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  230.          version="3.1">
  231.   
  232. </web-app>cn.example.springmvc.boke.servlet.ExampleServlet<?xml version="1.0" encoding="UTF-8"?>
  233. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  234.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  235.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  236.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  237.          version="3.1">
  238.   
  239. </web-app><?xml version="1.0" encoding="UTF-8"?>
  240. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  241.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  242.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  243.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  244.          version="3.1">
  245.   
  246. </web-app><?xml version="1.0" encoding="UTF-8"?>
  247. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  248.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  249.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  250.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  251.          version="3.1">
  252.   
  253. </web-app><?xml version="1.0" encoding="UTF-8"?>
  254. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  255.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  256.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  257.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  258.          version="3.1">
  259.   
  260. </web-app><?xml version="1.0" encoding="UTF-8"?>
  261. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  262.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  263.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  264.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  265.          version="3.1">
  266.   
  267. </web-app><?xml version="1.0" encoding="UTF-8"?>
  268. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  269.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  270.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  271.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  272.          version="3.1">
  273.   
  274. </web-app><?xml version="1.0" encoding="UTF-8"?>
  275. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  276.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  277.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  278.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  279.          version="3.1">
  280.   
  281. </web-app><?xml version="1.0" encoding="UTF-8"?>
  282. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  283.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  284.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  285.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  286.          version="3.1">
  287.   
  288. </web-app><?xml version="1.0" encoding="UTF-8"?>
  289. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  290.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  291.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  292.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  293.          version="3.1">
  294.   
  295. </web-app><?xml version="1.0" encoding="UTF-8"?>
  296. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  297.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  298.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  299.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  300.          version="3.1">
  301.   
  302. </web-app>exampleServlet<?xml version="1.0" encoding="UTF-8"?>
  303. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  304.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  305.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  306.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  307.          version="3.1">
  308.   
  309. </web-app><?xml version="1.0" encoding="UTF-8"?>
  310. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  311.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  312.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  313.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  314.          version="3.1">
  315.   
  316. </web-app><?xml version="1.0" encoding="UTF-8"?>
  317. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  318.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  319.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  320.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  321.          version="3.1">
  322.   
  323. </web-app><?xml version="1.0" encoding="UTF-8"?>
  324. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  325.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  326.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  327.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  328.          version="3.1">
  329.   
  330. </web-app>/<?xml version="1.0" encoding="UTF-8"?>
  331. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  332.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  333.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  334.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  335.          version="3.1">
  336.   
  337. </web-app><?xml version="1.0" encoding="UTF-8"?>
  338. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  339.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  340.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  341.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  342.          version="3.1">
  343.   
  344. </web-app>
复制代码
现在ioc容器有了,而且被Spring以WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE为key放到了application域中,现在我们可以在任何地方被获取到它,如下所示
  1. @WebServlet("/example")public class ExampleServlet extends HttpServlet {<?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
  5.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  6.          version="3.1">
  7.   
  8. </web-app><?xml version="1.0" encoding="UTF-8"?>
  9. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  10.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  11.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  12.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  13.          version="3.1">
  14.   
  15. </web-app>@Override<?xml version="1.0" encoding="UTF-8"?>
  16. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  17.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  18.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  19.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  20.          version="3.1">
  21.   
  22. </web-app><?xml version="1.0" encoding="UTF-8"?>
  23. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  24.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  25.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  26.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  27.          version="3.1">
  28.   
  29. </web-app>protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {<?xml version="1.0" encoding="UTF-8"?>
  30. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  31.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  32.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  33.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  34.          version="3.1">
  35.   
  36. </web-app><?xml version="1.0" encoding="UTF-8"?>
  37. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  38.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  39.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  40.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  41.          version="3.1">
  42.   
  43. </web-app><?xml version="1.0" encoding="UTF-8"?>
  44. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  45.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  46.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  47.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  48.          version="3.1">
  49.   
  50. </web-app><?xml version="1.0" encoding="UTF-8"?>
  51. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  52.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  53.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  54.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  55.          version="3.1">
  56.   
  57. </web-app>//获取application域中的ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE属性值,即我们的ioc容器<?xml version="1.0" encoding="UTF-8"?>
  58. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  59.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  60.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  61.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  62.          version="3.1">
  63.   
  64. </web-app><?xml version="1.0" encoding="UTF-8"?>
  65. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  66.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  67.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  68.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  69.          version="3.1">
  70.   
  71. </web-app><?xml version="1.0" encoding="UTF-8"?>
  72. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  73.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  74.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  75.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  76.          version="3.1">
  77.   
  78. </web-app><?xml version="1.0" encoding="UTF-8"?>
  79. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  80.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  81.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  82.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  83.          version="3.1">
  84.   
  85. </web-app>XmlWebApplicationContext ctx = (XmlWebApplicationContext) this.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);<?xml version="1.0" encoding="UTF-8"?>
  86. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  87.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  88.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  89.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  90.          version="3.1">
  91.   
  92. </web-app><?xml version="1.0" encoding="UTF-8"?>
  93. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  94.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  95.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  96.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  97.          version="3.1">
  98.   
  99. </web-app><?xml version="1.0" encoding="UTF-8"?>
  100. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  101.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  102.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  103.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  104.          version="3.1">
  105.   
  106. </web-app><?xml version="1.0" encoding="UTF-8"?>
  107. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  108.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  109.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  110.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  111.          version="3.1">
  112.   
  113. </web-app>//或者也可以使用Spring提供的工具类WebApplicationContextUtils来获取ioc容器,如下<?xml version="1.0" encoding="UTF-8"?>
  114. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  115.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  116.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  117.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  118.          version="3.1">
  119.   
  120. </web-app><?xml version="1.0" encoding="UTF-8"?>
  121. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  122.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  123.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  124.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  125.          version="3.1">
  126.   
  127. </web-app><?xml version="1.0" encoding="UTF-8"?>
  128. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  129.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  130.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  131.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  132.          version="3.1">
  133.   
  134. </web-app><?xml version="1.0" encoding="UTF-8"?>
  135. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  136.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  137.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  138.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  139.          version="3.1">
  140.   
  141. </web-app>//XmlWebApplicationContext ctx = (XmlWebApplicationContext) WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());<?xml version="1.0" encoding="UTF-8"?>
  142. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  143.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  144.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  145.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  146.          version="3.1">
  147.   
  148. </web-app><?xml version="1.0" encoding="UTF-8"?>
  149. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  150.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  151.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  152.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  153.          version="3.1">
  154.   
  155. </web-app><?xml version="1.0" encoding="UTF-8"?>
  156. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  157.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  158.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  159.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  160.          version="3.1">
  161.   
  162. </web-app><?xml version="1.0" encoding="UTF-8"?>
  163. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  164.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  165.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  166.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  167.          version="3.1">
  168.   
  169. </web-app>//使用ioc容器,获取其中的bean<?xml version="1.0" encoding="UTF-8"?>
  170. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  171.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  172.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  173.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  174.          version="3.1">
  175.   
  176. </web-app><?xml version="1.0" encoding="UTF-8"?>
  177. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  178.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  179.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  180.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  181.          version="3.1">
  182.   
  183. </web-app><?xml version="1.0" encoding="UTF-8"?>
  184. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  185.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  186.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  187.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  188.          version="3.1">
  189.   
  190. </web-app><?xml version="1.0" encoding="UTF-8"?>
  191. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  192.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  193.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  194.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  195.          version="3.1">
  196.   
  197. </web-app>ExampleService exampleService = ctx.getBean(ExampleService.class);<?xml version="1.0" encoding="UTF-8"?>
  198. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  199.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  200.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  201.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  202.          version="3.1">
  203.   
  204. </web-app><?xml version="1.0" encoding="UTF-8"?>
  205. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  206.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  207.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  208.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  209.          version="3.1">
  210.   
  211. </web-app><?xml version="1.0" encoding="UTF-8"?>
  212. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  213.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  214.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  215.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  216.          version="3.1">
  217.   
  218. </web-app><?xml version="1.0" encoding="UTF-8"?>
  219. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  220.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  221.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  222.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  223.          version="3.1">
  224.   
  225. </web-app>resp.getWriter().write(exampleService.get());<?xml version="1.0" encoding="UTF-8"?>
  226. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  227.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  228.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  229.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  230.          version="3.1">
  231.   
  232. </web-app><?xml version="1.0" encoding="UTF-8"?>
  233. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  234.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  235.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  236.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  237.          version="3.1">
  238.   
  239. </web-app>}}//最后,重新启动容器,访问 http://localhost:8080/springmvc/example,会发现页面上出现 user 字符串
复制代码
当然,向上面这样每次都通过get方法获取,很麻烦,我们可以借助Spring提供的工具类,在Servlet初始化的时候对Servlet进行依赖注入,如下
  1. @WebServlet(urlPatterns = "/example")public class ExampleServlet extends HttpServlet {<?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
  5.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  6.          version="3.1">
  7.   
  8. </web-app><?xml version="1.0" encoding="UTF-8"?>
  9. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  10.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  11.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  12.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  13.          version="3.1">
  14.   
  15. </web-app><?xml version="1.0" encoding="UTF-8"?>
  16. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  17.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  18.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  19.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  20.          version="3.1">
  21.   
  22. </web-app><?xml version="1.0" encoding="UTF-8"?>
  23. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  24.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  25.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  26.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  27.          version="3.1">
  28.   
  29. </web-app>//使用@Autowired注解标注需要进行依赖注入的bean<?xml version="1.0" encoding="UTF-8"?>
  30. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  31.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  32.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  33.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  34.          version="3.1">
  35.   
  36. </web-app><?xml version="1.0" encoding="UTF-8"?>
  37. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  38.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  39.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  40.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  41.          version="3.1">
  42.   
  43. </web-app>@Autowired<?xml version="1.0" encoding="UTF-8"?>
  44. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  45.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  46.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  47.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  48.          version="3.1">
  49.   
  50. </web-app><?xml version="1.0" encoding="UTF-8"?>
  51. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  52.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  53.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  54.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  55.          version="3.1">
  56.   
  57. </web-app>private ExampleService exampleService;<?xml version="1.0" encoding="UTF-8"?>
  58. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  59.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  60.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  61.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  62.          version="3.1">
  63.   
  64. </web-app><?xml version="1.0" encoding="UTF-8"?>
  65. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  66.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  67.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  68.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  69.          version="3.1">
  70.   
  71. </web-app><?xml version="1.0" encoding="UTF-8"?>
  72. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  73.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  74.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  75.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  76.          version="3.1">
  77.   
  78. </web-app><?xml version="1.0" encoding="UTF-8"?>
  79. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  80.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  81.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  82.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  83.          version="3.1">
  84.   
  85. </web-app>//Servlet初始化方法<?xml version="1.0" encoding="UTF-8"?>
  86. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  87.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  88.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  89.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  90.          version="3.1">
  91.   
  92. </web-app><?xml version="1.0" encoding="UTF-8"?>
  93. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  94.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  95.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  96.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  97.          version="3.1">
  98.   
  99. </web-app>@Override<?xml version="1.0" encoding="UTF-8"?>
  100. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  101.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  102.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  103.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  104.          version="3.1">
  105.   
  106. </web-app><?xml version="1.0" encoding="UTF-8"?>
  107. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  108.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  109.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  110.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  111.          version="3.1">
  112.   
  113. </web-app>public void init(ServletConfig config) throws ServletException {<?xml version="1.0" encoding="UTF-8"?>
  114. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  115.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  116.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  117.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  118.          version="3.1">
  119.   
  120. </web-app><?xml version="1.0" encoding="UTF-8"?>
  121. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  122.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  123.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  124.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  125.          version="3.1">
  126.   
  127. </web-app><?xml version="1.0" encoding="UTF-8"?>
  128. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  129.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  130.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  131.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  132.          version="3.1">
  133.   
  134. </web-app><?xml version="1.0" encoding="UTF-8"?>
  135. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  136.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  137.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  138.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  139.          version="3.1">
  140.   
  141. </web-app>super.init(config);<?xml version="1.0" encoding="UTF-8"?>
  142. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  143.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  144.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  145.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  146.          version="3.1">
  147.   
  148. </web-app><?xml version="1.0" encoding="UTF-8"?>
  149. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  150.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  151.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  152.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  153.          version="3.1">
  154.   
  155. </web-app><?xml version="1.0" encoding="UTF-8"?>
  156. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  157.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  158.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  159.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  160.          version="3.1">
  161.   
  162. </web-app><?xml version="1.0" encoding="UTF-8"?>
  163. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  164.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  165.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  166.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  167.          version="3.1">
  168.   
  169. </web-app>//获取application域<?xml version="1.0" encoding="UTF-8"?>
  170. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  171.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  172.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  173.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  174.          version="3.1">
  175.   
  176. </web-app><?xml version="1.0" encoding="UTF-8"?>
  177. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  178.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  179.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  180.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  181.          version="3.1">
  182.   
  183. </web-app><?xml version="1.0" encoding="UTF-8"?>
  184. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  185.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  186.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  187.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  188.          version="3.1">
  189.   
  190. </web-app><?xml version="1.0" encoding="UTF-8"?>
  191. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  192.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  193.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  194.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  195.          version="3.1">
  196.   
  197. </web-app>ServletContext servletContext = config.getServletContext();<?xml version="1.0" encoding="UTF-8"?>
  198. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  199.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  200.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  201.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  202.          version="3.1">
  203.   
  204. </web-app><?xml version="1.0" encoding="UTF-8"?>
  205. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  206.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  207.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  208.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  209.          version="3.1">
  210.   
  211. </web-app><?xml version="1.0" encoding="UTF-8"?>
  212. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  213.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  214.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  215.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  216.          version="3.1">
  217.   
  218. </web-app><?xml version="1.0" encoding="UTF-8"?>
  219. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  220.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  221.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  222.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  223.          version="3.1">
  224.   
  225. </web-app>//使用Spring提供的自动注入工具类SpringBeanAutowiringSupport,直接进行依赖注入<?xml version="1.0" encoding="UTF-8"?>
  226. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  227.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  228.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  229.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  230.          version="3.1">
  231.   
  232. </web-app><?xml version="1.0" encoding="UTF-8"?>
  233. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  234.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  235.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  236.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  237.          version="3.1">
  238.   
  239. </web-app><?xml version="1.0" encoding="UTF-8"?>
  240. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  241.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  242.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  243.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  244.          version="3.1">
  245.   
  246. </web-app><?xml version="1.0" encoding="UTF-8"?>
  247. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  248.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  249.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  250.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  251.          version="3.1">
  252.   
  253. </web-app>SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, servletContext);<?xml version="1.0" encoding="UTF-8"?>
  254. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  255.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  256.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  257.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  258.          version="3.1">
  259.   
  260. </web-app><?xml version="1.0" encoding="UTF-8"?>
  261. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  262.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  263.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  264.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  265.          version="3.1">
  266.   
  267. </web-app>}<?xml version="1.0" encoding="UTF-8"?>
  268. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  269.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  270.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  271.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  272.          version="3.1">
  273.   
  274. </web-app><?xml version="1.0" encoding="UTF-8"?>
  275. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  276.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  277.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  278.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  279.          version="3.1">
  280.   
  281. </web-app>@Override<?xml version="1.0" encoding="UTF-8"?>
  282. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  283.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  284.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  285.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  286.          version="3.1">
  287.   
  288. </web-app><?xml version="1.0" encoding="UTF-8"?>
  289. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  290.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  291.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  292.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  293.          version="3.1">
  294.   
  295. </web-app>protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {<?xml version="1.0" encoding="UTF-8"?>
  296. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  297.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  298.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  299.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  300.          version="3.1">
  301.   
  302. </web-app><?xml version="1.0" encoding="UTF-8"?>
  303. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  304.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  305.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  306.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  307.          version="3.1">
  308.   
  309. </web-app><?xml version="1.0" encoding="UTF-8"?>
  310. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  311.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  312.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  313.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  314.          version="3.1">
  315.   
  316. </web-app><?xml version="1.0" encoding="UTF-8"?>
  317. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  318.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  319.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  320.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  321.          version="3.1">
  322.   
  323. </web-app>resp.getWriter().println(exampleService.get());<?xml version="1.0" encoding="UTF-8"?>
  324. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  325.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  326.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  327.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  328.          version="3.1">
  329.   
  330. </web-app><?xml version="1.0" encoding="UTF-8"?>
  331. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  332.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  333.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  334.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  335.          version="3.1">
  336.   
  337. </web-app>}}
复制代码
3.基于Servlet扩展接口,整合Spring与Servlet
(1) 在上一节中,我们将ioc的创建配置于web.xml中,但此外我们还可以利用java代码的方式来创建ioc容器,可通过Servlet 3.0提供的ServletContainerInitializer接口,来在web容器启动的时候为第三方组件提供初始化的机会(例如注册Servlet等),如果要使用ServletContainerInitializer接口,那么就必须要在项目或所其依赖的jar包中的/META-INF/services目录下创建一个名称为javax.servlet.ServletContainerInitializer 的文件,而这个文件的具体内容,就是ServletContainerInitializer实现类的全限定类名称,然后,借助java的SPI技术,web容器便会加载这些实现类,通常情况下,ServletContainerInitializer这个接口通常会配合@HandlesTypes注解一起使用,而这个@HandlesTypes注解的作用就是让web容器收集我们项目中所有所指定的类,然后将这些类作为ServletContainerInitializer的onStartup方法参数传入,这样,在web容器启动的时候,我们就可以拿到这些我们所需的类然后创建它们
当然,同上面web.xml中的ContextLoaderListener,Spring也提供了一个ServletContainerInitializer接口的实现类SpringServletContainerInitializer,来创建帮助我们简化ioc容器的创建,首先在spring-mvc jar包中,就定义了一个/META-INF/services/javax.servlet.ServletContainerInitializer文件,然后,在启动时,web容器便会加载这个文件,读取里面的内容,为SpringServletContainerInitializer这个类
[img=30%,30%]https://img2023.cnblogs.com/blog/3062734/202305/3062734-20230510213533730-1995015549.jpg[/img]由于在SpringServletContainerInitializer上有注解@HandlesTypes标注,而这个注解的值为WebApplicationInitializer,因此,在创建SpringServletContainerInitializer对象前,web容器会收集应用内所有WebApplicationInitializer接口的实现类,并将它们作为参数传递给onStartup方法中的webAppInitializerClasses,这样,在web容器启动时,我们就能初始化我们所指定的对象
[img=30%,30%]https://img2023.cnblogs.com/blog/3062734/202305/3062734-20230510213749691-2046699001.jpg[/img]总而言之,在应用启动时,web容器会调用ServletContainerInitializer实现类(这里为SpringServletContainerInitializer)中的onStartup方法,而这个onStartup方法中又调用了@HandlesTypes注解所指定的类或接口(此处为WebApplicationInitializer)的实现类中的onStartup方法,因此,我们可以编写一个WebApplicationInitializer的实现类,来创建ioc容器,不过,Spring已经为我们提供了一个实现了WebApplicationInitializer接口的抽象类AbstractContextLoaderInitializer,它里面已经封装好了大部分的逻辑(比如将ioc容器置于application域中等),而我们所需要做的仅仅就是创建一下ioc容器而已,如下
  1. public class IocInit extends AbstractContextLoaderInitializer {<?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
  5.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  6.          version="3.1">
  7.   
  8. </web-app><?xml version="1.0" encoding="UTF-8"?>
  9. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  10.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  11.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  12.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  13.          version="3.1">
  14.   
  15. </web-app>@Override<?xml version="1.0" encoding="UTF-8"?>
  16. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  17.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  18.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  19.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  20.          version="3.1">
  21.   
  22. </web-app><?xml version="1.0" encoding="UTF-8"?>
  23. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  24.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  25.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  26.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  27.          version="3.1">
  28.   
  29. </web-app>protected WebApplicationContext createRootApplicationContext() {<?xml version="1.0" encoding="UTF-8"?>
  30. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  31.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  32.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  33.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  34.          version="3.1">
  35.   
  36. </web-app><?xml version="1.0" encoding="UTF-8"?>
  37. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  38.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  39.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  40.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  41.          version="3.1">
  42.   
  43. </web-app><?xml version="1.0" encoding="UTF-8"?>
  44. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  45.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  46.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  47.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  48.          version="3.1">
  49.   
  50. </web-app><?xml version="1.0" encoding="UTF-8"?>
  51. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  52.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  53.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  54.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  55.          version="3.1">
  56.   
  57. </web-app>XmlWebApplicationContext ctx = new XmlWebApplicationContext();<?xml version="1.0" encoding="UTF-8"?>
  58. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  59.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  60.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  61.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  62.          version="3.1">
  63.   
  64. </web-app><?xml version="1.0" encoding="UTF-8"?>
  65. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  66.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  67.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  68.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  69.          version="3.1">
  70.   
  71. </web-app><?xml version="1.0" encoding="UTF-8"?>
  72. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  73.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  74.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  75.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  76.          version="3.1">
  77.   
  78. </web-app><?xml version="1.0" encoding="UTF-8"?>
  79. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  80.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  81.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  82.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  83.          version="3.1">
  84.   
  85. </web-app>ctx.setConfigLocation("classpath:springmvc.xml");<?xml version="1.0" encoding="UTF-8"?>
  86. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  87.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  88.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  89.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  90.          version="3.1">
  91.   
  92. </web-app><?xml version="1.0" encoding="UTF-8"?>
  93. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  94.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  95.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  96.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  97.          version="3.1">
  98.   
  99. </web-app><?xml version="1.0" encoding="UTF-8"?>
  100. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  101.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  102.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  103.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  104.          version="3.1">
  105.   
  106. </web-app><?xml version="1.0" encoding="UTF-8"?>
  107. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  108.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  109.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  110.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  111.          version="3.1">
  112.   
  113. </web-app>return ctx;<?xml version="1.0" encoding="UTF-8"?>
  114. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  115.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  116.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  117.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  118.          version="3.1">
  119.   
  120. </web-app><?xml version="1.0" encoding="UTF-8"?>
  121. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  122.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  123.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  124.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  125.          version="3.1">
  126.   
  127. </web-app>}}
复制代码
此外,不要忘了注释掉web.xml中关于Spring的相关内容,否则会产生产生两个ioc容器
  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
  5.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  6.          version="3.1">
  7.   
  8. </web-app><?xml version="1.0" encoding="UTF-8"?>
  9. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  10.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  11.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  12.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  13.          version="3.1">
  14.   
  15. </web-app><?xml version="1.0" encoding="UTF-8"?>
  16. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  17.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  18.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  19.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  20.          version="3.1">
  21.   
  22. </web-app><?xml version="1.0" encoding="UTF-8"?>
  23. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  24.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  25.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  26.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  27.          version="3.1">
  28.   
  29. </web-app><?xml version="1.0" encoding="UTF-8"?>
  30. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  31.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  32.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  33.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  34.          version="3.1">
  35.   
  36. </web-app><?xml version="1.0" encoding="UTF-8"?>
  37. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  38.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  39.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  40.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  41.          version="3.1">
  42.   
  43. </web-app>exampleServlet<?xml version="1.0" encoding="UTF-8"?>
  44. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  45.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  46.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  47.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  48.          version="3.1">
  49.   
  50. </web-app><?xml version="1.0" encoding="UTF-8"?>
  51. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  52.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  53.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  54.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  55.          version="3.1">
  56.   
  57. </web-app><?xml version="1.0" encoding="UTF-8"?>
  58. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  59.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  60.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  61.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  62.          version="3.1">
  63.   
  64. </web-app><?xml version="1.0" encoding="UTF-8"?>
  65. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  66.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  67.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  68.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  69.          version="3.1">
  70.   
  71. </web-app>cn.example.springmvc.boke.servlet.ExampleServlet<?xml version="1.0" encoding="UTF-8"?>
  72. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  73.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  74.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  75.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  76.          version="3.1">
  77.   
  78. </web-app><?xml version="1.0" encoding="UTF-8"?>
  79. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  80.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  81.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  82.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  83.          version="3.1">
  84.   
  85. </web-app><?xml version="1.0" encoding="UTF-8"?>
  86. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  87.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  88.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  89.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  90.          version="3.1">
  91.   
  92. </web-app><?xml version="1.0" encoding="UTF-8"?>
  93. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  94.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  95.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  96.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  97.          version="3.1">
  98.   
  99. </web-app><?xml version="1.0" encoding="UTF-8"?>
  100. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  101.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  102.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  103.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  104.          version="3.1">
  105.   
  106. </web-app><?xml version="1.0" encoding="UTF-8"?>
  107. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  108.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  109.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  110.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  111.          version="3.1">
  112.   
  113. </web-app><?xml version="1.0" encoding="UTF-8"?>
  114. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  115.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  116.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  117.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  118.          version="3.1">
  119.   
  120. </web-app><?xml version="1.0" encoding="UTF-8"?>
  121. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  122.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  123.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  124.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  125.          version="3.1">
  126.   
  127. </web-app>exampleServlet<?xml version="1.0" encoding="UTF-8"?>
  128. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  129.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  130.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  131.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  132.          version="3.1">
  133.   
  134. </web-app><?xml version="1.0" encoding="UTF-8"?>
  135. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  136.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  137.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  138.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  139.          version="3.1">
  140.   
  141. </web-app><?xml version="1.0" encoding="UTF-8"?>
  142. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  143.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  144.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  145.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  146.          version="3.1">
  147.   
  148. </web-app><?xml version="1.0" encoding="UTF-8"?>
  149. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  150.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  151.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  152.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  153.          version="3.1">
  154.   
  155. </web-app>/<?xml version="1.0" encoding="UTF-8"?>
  156. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  157.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  158.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  159.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  160.          version="3.1">
  161.   
  162. </web-app><?xml version="1.0" encoding="UTF-8"?>
  163. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  164.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  165.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  166.                              http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  167.          version="3.1">
  168.   
  169. </web-app>
复制代码
最后,重启项目,输入http://localhost:8080/springmvc/example,看见user字符串则说明成功
4.Spring MVC
现在,我们将Servlet与Spring ioc容器整合到了一起,但如果我们需要处理新的请求的话,我们还得继承HttpServlet来编写新的Servlet,并将其配置到web.xml中,非常麻烦,因此,Spring变为我们提供了一个全新的框架 - Spring MVC来帮助我们进行开发

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

来自云龙湖轮廓分明的月亮

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表