【问题标题】:Adding conditions in web.xml在 web.xml 中添加条件
【发布时间】:2013-06-07 06:05:51
【问题描述】:

这是我的 web.xml 的一部分

    <error-page>
    <error-code>500</error-code>
    <location>/index</location>
    </error-page>

有没有办法告诉 web.xml 文件处于开发模式:

&lt;location&gt;/displayException&lt;/location&gt;

以上location property?

或通过代码添加条件的任何方式?

尝试这样做的目的是:在开发模式下我想在页面中看到异常,在实时模式下我想在发生异常时将他重定向到默认页面。

【问题讨论】:

    标签: java jakarta-ee servlets web-applications web.xml


    【解决方案1】:

    您不能在web.xml 级别执行此操作。如果web.xml 尊重系统属性,这可能是一个很酷的功能,但事实并非如此。

    因此,您唯一的方法就是在代码中执行此操作。幸运的是,您有各种可能性。您可以将错误 URL 映射到 JSP,您可以在 JSP 中根据存储在属性文件、数据库或您想要的位置的系统属性或其他参数来实现逻辑。您也可以在 HTTP 过滤器中执行此操作。

    您还可以创建多个版本的 web.xml:一个用于生产,另一个用于测试。如果您想避免重复使用从模板自动生成来创建这些版本。

    【讨论】:

    • 你的 web.xml 的几个版本会这样做。因为有一些像这样的变化。谢谢。
    【解决方案2】:

    在 Wildfly 中,有一种非便携式方法可以在 web.xml(和其他,如 persistence.xml)中启用系统属性评估。为此,请在您的服务器配置(standalone.xml 或其他)的ee 子系统中指定以下内容:

    <subsystem xmlns="urn:jboss:domain:ee:2.0">
        <spec-descriptor-property-replacement>true</spec-descriptor-property-replacement>
        <!-- ... -->
    </subsystem>
    

    然后你可以这样做:

    <location>${myapp.errorpage.location}</location>
    

    【讨论】:

      【解决方案3】:

      您可以在错误或位置页面中使用 if 语句。 web.xml 不支持 if 语句。

      //error page
      if("blog".equals(url))
          blog content ......
      else if("profile".equals(url))
          profile content ......
      

      【讨论】:

      • // 错误页面 if("blog".equals(url)) blog content ...... else if("profile".equals(url)) profile content ...... .
      猜你喜欢
      • 2011-06-24
      • 2014-03-07
      • 2014-04-25
      • 1970-01-01
      • 2023-03-06
      • 2012-12-12
      • 2012-06-16
      • 1970-01-01
      • 2012-11-19
      相关资源
      最近更新 更多