@WebInitParam 的用例 [英] Use cases for @WebInitParam

查看:52
本文介绍了@WebInitParam 的用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从 Servlet 3.0 规范开始,就有可能将 servlet 映射元数据声明为 servlet 类上的注释:

Since the Servlet 3.0 specification there is the possibility of declaring servlet mapping metadata as annotation on the servlet class:

@WebServlet(name="appInfoServlet", urlPatterns ="/appInfo", initParams = @WebInitParam(name="ocwd.deployer.email", value="admin@example.com"))
public class AppInfoServlet extends HttpServlet {

}

我不明白的是将 init 参数保持在与 servlet 相同的类中的用例.据我了解,这些参数将与类分开并放入部署描述符中.

What I do not understand though is the use case for keeping init parameters in the same class as the servlet. As far as I understand these parameters are to be kept separate from the class and placed into the deployment descriptor.

@WebServlet 注释中指定初始化参数有哪些用例?

What use cases are there for specifying init parameters within the @WebServlet annotation?

推荐答案

注解用于给出默认值.

在 JavaEE 中,也可以使用注解提供部署属性.给定注释的值,部署描述符(即 web.xml)仍可用于覆盖注释提供的默认值.

In JavaEE the deployment properties can also be provided using annotations. Given the values for annotations, the deployment descriptor i.e, web.xml can still be used to override the default values provided by the annotations.

在上面的示例中,可以通过在 web.xml 中配置一个具有匹配名称的 servlet 来覆盖 init-param:

In the example above, the init-param can be overriden by configuring a servlet with a matching name in web.xml:

  <servlet>
    <servlet-name>appInfoServlet</servlet-name>
    <init-param>
        <param-name>ocwd.deployer.email</param-name>
        <param-value>noreply@example.com</param-value>
    </init-param>
  </servlet>

这篇关于@WebInitParam 的用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆