A.12. web.xml

web.xml 是 Java EE web 应用程序的标准描述文件。需要为 Middleware、web 客户端以及 Web Portal 客户端 block 创建此文件。

在一个应用程序项目中,web.xml 文件在相应模块web/WEB-INF 目录。

  • Middleware block(core 项目模块)的 web.xml 文件有如下内容:
  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <web-app xmlns="http://java.sun.com/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  5. version="3.0">
  6. <!-- Application properties config files -->
  7. <context-param>
  8. <param-name>appPropertiesConfig</param-name>
  9. <param-value>
  10. classpath:com/company/sample/app.properties
  11. /WEB-INF/local.app.properties
  12. "file:${catalina.base}/conf/app-core/local.app.properties"
  13. </param-value>
  14. </context-param>
  15. <!--Application components-->
  16. <context-param>
  17. <param-name>appComponents</param-name>
  18. <param-value>com.haulmont.cuba com.haulmont.reports</param-value>
  19. </context-param>
  20. <listener>
  21. <listener-class>com.haulmont.cuba.core.sys.AppContextLoader</listener-class>
  22. </listener>
  23. <servlet>
  24. <servlet-name>remoting</servlet-name>
  25. <servlet-class>com.haulmont.cuba.core.sys.remoting.RemotingServlet</servlet-class>
  26. <load-on-startup>1</load-on-startup>
  27. </servlet>
  28. <servlet-mapping>
  29. <servlet-name>remoting</servlet-name>
  30. <url-pattern>/remoting/*</url-pattern>
  31. </servlet-mapping>
  32. </web-app>

context-param 元素定义了当前 web 应用程序 ServletContext 对象的初始化参数。应用程序组件列表定义在 appComponents 参数,应用程序属性文件列表定义在 appPropertiesConfig 参数。

listener 元素定义了实现 ServletContextListener 接口的监听类。Middleware block 使用 AppContextLoader 类作为监听器。这个类初始化了 AppContext

然后是 Servlet 描述,包括 RemotingServlet 类,对于 Middleware block 来说,这是必须的。这个 servlet 可以通过 /remoting/* URL 来访问,跟远程访问容器相关联,参考 remoting-spring.xml

  • Web 客户端 block(web 项目模块)的 web.xml 文件有如下内容:
  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <web-app xmlns="http://java.sun.com/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  5. version="3.0">
  6. <context-param>
  7. <description>Web resources version for correct caching in browser</description>
  8. <param-name>webResourcesTs</param-name>
  9. <param-value>${webResourcesTs}</param-value>
  10. </context-param>
  11. <!-- Application properties config files -->
  12. <context-param>
  13. <param-name>appPropertiesConfig</param-name>
  14. <param-value>
  15. classpath:com/company/sample/web-app.properties
  16. /WEB-INF/local.app.properties
  17. "file:${catalina.base}/conf/app/local.app.properties"
  18. </param-value>
  19. </context-param>
  20. <!--Application components-->
  21. <context-param>
  22. <param-name>appComponents</param-name>
  23. <param-value>com.haulmont.cuba com.haulmont.reports</param-value>
  24. </context-param>
  25. <listener>
  26. <listener-class>com.vaadin.server.communication.JSR356WebsocketInitializer</listener-class>
  27. </listener>
  28. <listener>
  29. <listener-class>com.haulmont.cuba.web.sys.WebAppContextLoader</listener-class>
  30. </listener>
  31. <servlet>
  32. <servlet-name>app_servlet</servlet-name>
  33. <servlet-class>com.haulmont.cuba.web.sys.CubaApplicationServlet</servlet-class>
  34. <async-supported>true</async-supported>
  35. </servlet>
  36. <servlet>
  37. <servlet-name>dispatcher</servlet-name>
  38. <servlet-class>com.haulmont.cuba.web.sys.CubaDispatcherServlet</servlet-class>
  39. <load-on-startup>1</load-on-startup>
  40. </servlet>
  41. <servlet>
  42. <servlet-name>rest_api</servlet-name>
  43. <servlet-class>com.haulmont.restapi.sys.CubaRestApiServlet</servlet-class>
  44. <load-on-startup>2</load-on-startup>
  45. </servlet>
  46. <servlet-mapping>
  47. <servlet-name>dispatcher</servlet-name>
  48. <url-pattern>/dispatch/*</url-pattern>
  49. </servlet-mapping>
  50. <servlet-mapping>
  51. <servlet-name>app_servlet</servlet-name>
  52. <url-pattern>/*</url-pattern>
  53. </servlet-mapping>
  54. <servlet-mapping>
  55. <servlet-name>rest_api</servlet-name>
  56. <url-pattern>/rest/*</url-pattern>
  57. </servlet-mapping>
  58. <filter>
  59. <filter-name>cuba_filter</filter-name>
  60. <filter-class>com.haulmont.cuba.web.sys.CubaHttpFilter</filter-class>
  61. <async-supported>true</async-supported>
  62. </filter>
  63. <filter-mapping>
  64. <filter-name>cuba_filter</filter-name>
  65. <url-pattern>/*</url-pattern>
  66. </filter-mapping>
  67. <filter>
  68. <filter-name>restSpringSecurityFilterChain</filter-name>
  69. <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  70. <init-param>
  71. <param-name>contextAttribute</param-name>
  72. <param-value>org.springframework.web.servlet.FrameworkServlet.CONTEXT.rest_api</param-value>
  73. </init-param>
  74. <init-param>
  75. <param-name>targetBeanName</param-name>
  76. <param-value>springSecurityFilterChain</param-value>
  77. </init-param>
  78. </filter>
  79. <filter-mapping>
  80. <filter-name>restSpringSecurityFilterChain</filter-name>
  81. <url-pattern>/rest/*</url-pattern>
  82. </filter-mapping>
  83. </web-app>

context-param 元素中,定义了应用程序组件列表和应用程序属性文件列表。webResourcesTs 参数以及在构建时替换的这个参数的值,保证了在网页浏览器中能正确的缓存静态资源。

Web 客户端 block 使用 WebAppContextLoader 类作为 ServletContextListener

JSR356WebsocketInitializer 是支持 WebSockets 协议需要的监听器。

CubaApplicationServlet 提供了基于 Vaadin 框架实现的通用用户界面

CubaDispatcherServlet 为 Spring MCV 控制器初始化了一个额外的 Spring context。这个 context 通过 dispatcher-spring.xml 文件来配置。

CubaRestApiServlet 提供全局的 REST API