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

查看:288
本文介绍了@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 {

}

我不明白的是保留的用例与servlet在同一个类中的init参数。据我所知,这些参数将与类分开并放入部署描述符中。

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.

在<$>中指定init参数有哪些用例c $ c> @WebServlet 注释?

推荐答案

注释用于给出默认值。

The annotations are used to give the default values.

在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.

在上面的示例中,可以通过使用匹配的名称配置 servlet 来覆盖 init-param web.xml

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天全站免登陆