最近为了改一个问题,想加一个控制开关,就在web.xml 中配置了一个 <context-param> 参数,并在 Action 或 Interceptor 中获取参数值。

1、在 web.xml 中添加配置项:

<context-param>
<param-name>test</param-name>
<param-value>true</param-value>
</context-param>

2、获取配置参数值的代码:

 //方式一:
Object object = ActionContext.getContext().getApplication().get("test");
if(null != object && Boolean.valueOf(object.toString()))
{
System.out.println("logging...");
} //方式二: Spring 的listener 就是从ServletContext中获取的 getInitParameter
String parameter = ServletActionContext.getServletContext().getInitParameter("test");
if(Boolean.valueOf(parameter))
{
System.out.println("duide");
//do sth...
}

个人觉得 第二种方式 更方便些。

3、闲话

在Servlet 中也可以通过 HTTPRequest 来获取 ServletContext,进而获取 application 级别的配置项: <context-param>

struts2 在 Action 或 Interceptor 中获取 web.xml 中配置的 <context-param> 参数 (这是我的第一篇博文,哈哈。)的更多相关文章

  1. 传值:web.xml传递参数 即在Servlet中获取web.xml里的值

    传值:web.xml传递参数 在web.xml中的Servlet里配置多个init-param <servlet> ... <init-param> <param-nam ...

  2. SSM搭配中的web.xml的配置信息

    最近一段时间在自己学着搭建SSM框架的项目,其实这个项目自由自己不断尝试,不断失败,才能印象更深刻. 下面就说一下在项目中的web.xml的相关配置信息: <?xml version=" ...

  3. struts2不同版本在核心filter在web.xml中的配置

    FilterDispatcher是struts2.0.x到2.1.2版本的核心过滤器.配置如下: <filter> <filter-name>struts2</filte ...

  4. 在springboot项目中获取pom.xml中的信息

    最近做了一个新项目,用到了springboot.在搭建框架的过程中,需要读取pom.xml中version的值,本来想着是用自己用java解析xml来着.没想到maven提供了这么一个包,可以直接获取 ...

  5. SpringMVC中在web.xml中添加中文过滤器的写法

    <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>or ...

  6. Web.xml中Filter过滤器标签几个说明

    在研究liferay框架中看到Web.xml中加入了过滤器的标签,可以根据页面提交的URL地址进行过滤,发现有几个新标签没用过,下面就介绍以下几个过滤器的标签用法: <!-- 定义Filter ...

  7. web.xml中Filter过滤器标签说明

    原文:http://www.cnblogs.com/edwardlauxh/archive/2010/03/11/1918618.html 在研究liferay框架中看到Web.xml中加入了过滤器的 ...

  8. web.xml中contextConfigLocation的作用(转)

    原文地址:http://blog.csdn.net/zhangliao613/article/details/6289114 原文格式较乱,此处略作整理.内容未变. 在web.xml中使用contex ...

  9. struts2 ,web.xml中配置为/*.action,运行报错Invalid <url-pattern> /*.action in filter mapp

    首先,修改成: <filter-mapping>  <filter-name>struts2</filter-name>  <url-pattern>/ ...

随机推荐

  1. echart折线图系列一:折线图基本配置

    引入echart插件 页面上准备一个容器:<div id="box" style="height:400px;width: 800px;padding: 20px& ...

  2. Egret--添加一个精灵事件

    class Hello extends egret.DisplayObjectContainer{ //入口函数 private onAddStage(event:egret.Event){ //打开 ...

  3. shell grep

    grep "str" file > /dev/null if [ $? -eq 1]; then echo "no str" else echo &quo ...

  4. [LeetCode] Design Circular Queue 设计环形队列

    Design your implementation of the circular queue. The circular queue is a linear data structure in w ...

  5. ES6 模块机制

    ES6 实现了模块功能 将文件当作独立的模块,一个文件一个模块 每个模块可以导出自己的API成员,也可以导入其他模块或者模块中特定的API ES6 模块的设计思想,是尽量的静态化,使得编译时就能确定模 ...

  6. 解析CommandMessage

    Json 解析: void CommandMessage::ParseCmdBody() { try { Json::Reader reader; Json::Value root; if (!rea ...

  7. Python基础之元组和字典

    一.元组: 1.定义: 内存图: 2.基本操作 3.元组作用: 4.元组基础知识代码 # . 创建空元组 t01 = () t02 = tuple() # . 创建具有默认值的元组 t01 = (,, ...

  8. Java常用类库 读书笔记 一

    1.String Buffer 类 String 类所表示的字符串有一个局限就是字符串常量一旦声明则不可改变,只有内存地址的指向可以改变,如果要频繁修改字符串,需要使用String Buffer 类. ...

  9. robot 中文 乱码 问题 的处理

    第一种方式: def unic(item):  if isinstance(item, unicode):      return item  if isinstance(item, (bytes, ...

  10. Redis做LRU缓存

    当Redis用作缓存时,通常可以让它在添加新数据时自动逐出旧数据. 这种行为在开发人员社区中非常有名,因为它是流行的memcached系统的默认行为. LRU实际上只是支持的驱逐方法之一. 本页介绍了 ...