最近为了改一个问题,想加一个控制开关,就在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. 双接口(回调)promise cb

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. CSS(五)

    定位 关于定位 我们可以使用css的position属性来设置元素的定位类型,postion的设置项如下: relative 生成相对定位元素,元素所占据的文档流的位置不变,元素本身相对文档流的位置进 ...

  3. __x__(47)0910第六天__IE6到IE11对于包含中文路径的png显示问题

    问题:IE6额外地除了中文路径外,对于png24的支持度不高,以致于无法透明. 解决方法1,png8 替换: png8 比 png24 小,质量较低,但是在这里可以替代,以解决问题. 使用 ps 打开 ...

  4. sql的基础用法

    # sql 对大小写不敏感 # 查询表中的所有信息 select * from `Customers`; # 查询指定字段 CustomerName,Country select CustomerNa ...

  5. PHP使用 strpos() 注意事项

    返回字符出现的第一个位置, 如果字符在被搜索字符串的开头, 则会返回 ‘0’ 因此, 在使用此函数判断 字符串是否包含 某一个字符时  使用: if(strpos('string','str') != ...

  6. python多进程使用及线程池的使用方法

    多进程:主要运行multiprocessing模块 import os,time import sys from multiprocessing import Process class MyProc ...

  7. Centos 7 上使用nginx为Node.js配置反向代理时错误:(13: Permission denied) while connecting to upstream

    错误来源:Centos 7 上使用nginx为Node.js配置反向代理时产生(13: Permission denied) while connecting to upstream的错误 nginx ...

  8. thinkphp框架,数据动态缓存后,或数据已读取出来,想分页怎么办

    //读取缓存后赋值到数组,通过array_slice函数处理,如: $blog = S('blogname'); //赋值 $count = count($blog); //条数统计 $page = ...

  9. 滑动viewpage

    Adapter: package com.example.fashionyuan.Adatader; import android.support.v4.app.Fragment;import and ...

  10. 关于Mysql6.0+的时区错乱问题

    如果使用mysql6.0+的JDBC驱动版本的时候,有时候会出现程序时间与数据库时间相差很多个小时; 1.如果以北京时间为例,相差8个小时的情况一般是你在连接jdbc的url中没有标明system_t ...