spring中从xml配置文件中取值

xml 代码
  1. <bean id="webSys" class="com.njusc.sys.WebSys">  
  2.         <property name="bottomhd">  
  3.             <value>${bottomhd}</value>  
  4.         property>  
  5.    </bean>  

      在项目中有时需要配置一些可变的内容(如可能链接的头尾地址了,或是IP什么的),这时自然想到用配置文件达到灵活多用的效果,这里主要讲一下spring中配置文件的使用及配置过程。

webpro.xml 配置文件
  1. <xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> 
  3. <properties>  
  4. <entry key="typt.title.index">平台管理员</entry>  
  5. <entry key="bottomhd">hdpt/jsp/main/bottom.htm</entry>  
  6. </properties>  
applicationContext-properties   spring配置文件
  1. <xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE properties SYSTEM
  3.     "http://www.springframework.org/dtd/spring-beans.dtd">  
  4. <beans>  
  5.     <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  6.         <property name="locations">  
  7.             <list>  
  8.                 <value>/WEB-INF/sys.properties</value>  
  9.                 <value>/WEB-INF/webpro.xml</value>  
  10.             </list>  
  11.         </property>  
  12.     </bean>  
  13. </beans>  

建一个关系映射类

java 代码
  1. package com.njusc.sys;   
  2.   
  3. public class WebSys   
  4. {   
  5.     private String title;   
  6.        
  7.     private String bottomhd;   
  8.   
  9.     public String getTitle()   
  10.     {   
  11.         return title;   
  12.     }   
  13.   
  14.     public void setTitle(String title)   
  15.     {   
  16.         this.title = title;   
  17.     }   
  18.   
  19.     public String getBottomhd()   
  20.     {   
  21.         return bottomhd;   
  22.     }   
  23.   
  24.     public void setBottomhd(String bottomhd)   
  25.     {   
  26.         this.bottomhd = bottomhd;   
  27.     }   
  28. }   

在service配置文件中将改类依赖注入

applicationContext-services-hdpt.xml
  1. <bean id="webSys" class="com.njusc.sys.WebSys"  
  2.       singleton="true" autowire="byName">
  3.     <property name="bottomhd">  
  4.         <value>${bottomhd}</value>  
  5.     </property>  
  6.     <property name="title">    
  7.         <value>${typt.title.index}</value>  
  8.     </property>
  9. </bean> 

好配置完成了,这时如果需要在jsp页面使用的话,这里采用structs2.0标签显示

  1. 在跳转到这个页面的action中将 WebSys通过spring依赖注入,加入
  2. private WebSys webSys;

    public WebSys getWebSys()
    {
    return webSys;
    }

    public void setWebSys(WebSys webSys)
    {
    this.webSys = webSys;
  1. 页面使用<iframe scrolling="no" src="<ww:property value="webSys.bottomhd"/>frameborder="0" width="780" height="76">iframe>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值