A.12. web.xml

web.xml 是 Java 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:${app.home}/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"?>
    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/demo/web-app.properties
    11. /WEB-INF/local.app.properties
    12. "file:${app.home}/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.vaadin.server.communication.JSR356WebsocketInitializer</listener-class>
    22. </listener>
    23. <listener>
    24. <listener-class>com.haulmont.cuba.web.sys.WebAppContextLoader</listener-class>
    25. </listener>
    26. <servlet>
    27. <servlet-name>app_servlet</servlet-name>
    28. <servlet-class>com.haulmont.cuba.web.sys.CubaApplicationServlet</servlet-class>
    29. <async-supported>true</async-supported>
    30. </servlet>
    31. <servlet>
    32. <servlet-name>dispatcher</servlet-name>
    33. <servlet-class>com.haulmont.cuba.web.sys.CubaDispatcherServlet</servlet-class>
    34. <load-on-startup>1</load-on-startup>
    35. </servlet>
    36. <servlet-mapping>
    37. <servlet-name>dispatcher</servlet-name>
    38. <url-pattern>/dispatch/*</url-pattern>
    39. </servlet-mapping>
    40. <servlet-mapping>
    41. <servlet-name>app_servlet</servlet-name>
    42. <url-pattern>/*</url-pattern>
    43. </servlet-mapping>
    44. <filter>
    45. <filter-name>cuba_filter</filter-name>
    46. <filter-class>com.haulmont.cuba.web.sys.CubaHttpFilter</filter-class>
    47. <async-supported>true</async-supported>
    48. </filter>
    49. <filter-mapping>
    50. <filter-name>cuba_filter</filter-name>
    51. <url-pattern>/*</url-pattern>
    52. </filter-mapping>
    53. </web-app>

    context-param 元素中,定义了应用程序组件列表和应用程序属性文件列表。

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

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

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

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