`
xy_z487
  • 浏览: 271816 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

struts整合spring配置 context-param和init-param区别

阅读更多
web.xml里面可以定义两种参数:
(1)application范围内的参数,存放在servletcontext中,在web.xml中配置如下:
xml 代码

   1. <context-param> 
   2.            <param-name>context/param</param-name> 
   3.            <param-value>avalible during application</param-value> 
   4.   </context-param> 

(2)servlet范围内的参数,只能在servlet的init()方法中取得,在web.xml中配置如下:
xml 代码

   1. <servlet> 
   2.     <servlet-name>MainServlet</servlet-name> 
   3.     <servlet-class>com.wes.controller.MainServlet</servlet-class> 
   4.     <init-param> 
   5.        <param-name>param1</param-name> 
   6.        <param-value>avalible in servlet init()</param-value> 
   7.     </init-param> 
   8.     <load-on-startup>0</load-on-startup> 
   9.  </servlet> 

在servlet中可以通过代码分别取用:
java 代码

   1. package com.wes.controller;  
   2.  
   3. import javax.servlet.ServletException;  
   4. import javax.servlet.http.HttpServlet;  
   5.  
   6. public class MainServlet extends HttpServlet ...{  
   7.  
   8.     public MainServlet() ...{  
   9.         super();  
  10.      }  
  11.     public void init() throws ServletException ...{  
  12.          System.out.println("下面的两个参数param1是在servlet中存放的");  
  13.          System.out.println(this.getInitParameter("param1"));  
  14.          System.out.println("下面的参数是存放在servletcontext中的");  
  15.          System.out.println(getServletContext().getInitParameter("context/param"));  
  16.       }  
  17. }  

第一种参数在servlet里面可以通过getServletContext().getInitParameter("context/param")得到
第二种参数只能在servlet的init()方法中通过this.getInitParameter("param1")取得

init-param属于一个servlet所有,context-param属于整个应用程序所有 ,不仅是在servlet中可以得到,jsp文件中也可以得到.

在jsp中config就相当于这里的servletContext

struts整合spring配置
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
  </context-param>

  <!-- Listener contextConfigLocation -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>
spring 的ContextLoaderListener类肯定getServletContext获取Servlet上下文(aplication)
然后getInitParameter(“contextConfigLoacation”),获取配置路径,然后加载初始化所有的Bean。
分享到:
评论

相关推荐

    spring配置中<context-param> 和<init-param>的 区别

    &lt;context-param&gt; 和&lt;init-param&gt;的 区别代码 博文链接:https://xhy0422.iteye.com/blog/46319

    解析web.xml中在Servlet中获取context-param和init-param内的参数

    本篇文章是对web.xml中在Servlet中获取context-param和init-param内的参数进行了详细的分析介绍,需要的朋友参考下

    <context-param>与<init-param>的区别与作用

    NULL 博文链接:https://wy649898543.iteye.com/blog/1833263

    web.xml中&lt;/context-param&gt;等配置的作用

    了解web.xml的配置信息和加载顺序,有助于理解框架的流程,

    Myeclipse开发struts+hibernate+spring新手入门--环境配置---项目开发示例

    Myeclipse开发struts+hibernate+spring新手入门---环境配置----项目开发示例 Myeclipse开发struts+hibernate+spring小记 开发前准备工作: 1、下载eclipse3.1版本 下载地址: 2、下载Myeclipse插件 下载地址: 3...

    spring和hibernate整合

    在web.Xml中&lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt;/WEB-INF/spring/*.bean.xml&lt;/param-value&gt; &lt;/context-param&gt; &lt;listener-class&gt; org.springframework.web....

    logback-ext-spring

    &lt;context-param&gt; &lt;param-name&gt;logbackConfigLocation&lt;/param-name&gt; &lt;param-value&gt;/WEB-INF/conf/logback.xml&lt;/param-value&gt; &lt;/context-param&gt; &lt;listener-class&gt;ch.qos.logback.ext.spring.web....

    解决struts2.1.6+spring+hibernate 中文乱码

    web.xml(仅写出中文处理部分,spring的配置不用改变) &lt;filter-name&gt;encodingFilter&lt;/filter-name&gt; &lt;filter-class&gt;org.springframework.web.filter.CharacterEncodingFilter&lt;/filter-class&gt; &lt;init-param&gt; ...

    DOS命令使用方法(超全).

    -- 下边这是加入spring配置 --&gt; &lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt;classpath*:applicationContext-*.xml&lt;/param-value&gt; &lt;/context-param&gt; &lt;!-- 这是spring ...

    xfire的使用详解

    &lt;param-value&gt;classpath:spring/app.xml,classpath:org/codehaus/xfire/spring/xfire.xml&lt;/param-value&gt; &lt;/context-param&gt; &lt;!--配置xfire ws--&gt; &lt;servlet-name&gt;XFireServlet&lt;/servlet-name&gt; &lt;servlet-...

    修改后的 fckedit.jar

    web.xml 中配置 完善了对上传图片的验证 &lt;context-param&gt; &lt;param-name&gt;FCKAllowedExtensionsImage&lt;/param-name&gt; &lt;param-value&gt;jpg|gif|jpeg|png|bmp &lt;/param-value&gt; &lt;/context-param&gt; ...

    CXF WebService整合Spring示例工程代码demo

    CXF WebService整合Spring示例工程代码demo可以直接导入eclipse。参照网页http://www.cnblogs.com/hoojo/archive/2011/03/30/1999563.html 完成的webService服务提供。 大致步骤: 1.引入cxf和其他需要的jar包,(本...

    spring配置编码格式

    &lt;init-param&gt; &lt;param-name&gt;encoding&lt;/param-name&gt; &lt;param-value&gt;UTF-8&lt;/param-value&gt; &lt;/init-param&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;setCharacterEncoding&lt;/filter-name&gt; &lt;url-pattern&gt;/*&lt;/url-pattern&gt; ...

    ssh框架的搭建

    &lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt; /WEB-INF/applicationContext-*.xml,/WEB-INF/action-servlet.xml &lt;/param-value&gt; &lt;/context-param&gt; &lt;context-param&gt; &lt;param...

    网站访问记录器 0.1.1 发布_数据库

    &lt;context-param&gt; &lt;param-name&gt;WebVisitLogger-DbUrl&lt;/param-name&gt; &lt;param-value&gt;jdbc:postgresql://localhost/visit_logger?user=postgres&password=postgres&charSet=utf-8&lt;/param-value&gt; &lt;/context-param&gt; 4...

    网站访问记录器 0.1.1 发布_程序

    &lt;context-param&gt; &lt;param-name&gt;WebVisitLogger-DbUrl&lt;/param-name&gt; &lt;param-value&gt;jdbc:postgresql://localhost/visit_logger?user=postgres&password=postgres&charSet=utf-8&lt;/param-value&gt; &lt;/context-param&gt; 4...

    jaxrs-api,javax.ws.rs.Path;

    -- &lt;context-param&gt; &lt;param-name&gt;resteasy.scan&lt;/param-name&gt; &lt;param-value&gt;true&lt;/param-value&gt; &lt;/context-param&gt; --&gt; &lt;context-param&gt; &lt;param-name&gt;resteasy.servlet.mapping.prefix&lt;/param-name&gt; &lt;param...

Global site tag (gtag.js) - Google Analytics