xml 代码
- <bean id="webSys" class="com.njusc.sys.WebSys">
- <property name="bottomhd">
- <value>${bottomhd}</value>
- property>
- </bean>
在项目中有时需要配置一些可变的内容(如可能链接的头尾地址了,或是IP什么的),这时自然想到用配置文件达到灵活多用的效果,这里主要讲一下spring中配置文件的使用及配置过程。
webpro.xml 配置文件
- <xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
- <properties>
- <entry key="typt.title.index">平台管理员</entry>
- <entry key="bottomhd">hdpt/jsp/main/bottom.htm</entry>
- </properties>
applicationContext-properties spring配置文件
- <xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE properties SYSTEM
- "http://www.springframework.org/dtd/spring-beans.dtd">
- <beans>
- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <list>
- <value>/WEB-INF/sys.properties</value>
- <value>/WEB-INF/webpro.xml</value>
- </list>
- </property>
- </bean>
- </beans>
建一个关系映射类
java 代码
- package com.njusc.sys;
- public class WebSys
- {
- private String title;
- private String bottomhd;
- public String getTitle()
- {
- return title;
- }
- public void setTitle(String title)
- {
- this.title = title;
- }
- public String getBottomhd()
- {
- return bottomhd;
- }
- public void setBottomhd(String bottomhd)
- {
- this.bottomhd = bottomhd;
- }
- }
在service配置文件中将改类依赖注入
applicationContext-services-hdpt.xml
- <bean id="webSys" class="com.njusc.sys.WebSys"
- singleton="true" autowire="byName">
- <property name="bottomhd">
- <value>${bottomhd}</value>
- </property>
- <property name="title">
- <value>${typt.title.index}</value>
- </property>
- </bean>
好配置完成了,这时如果需要在jsp页面使用的话,这里采用structs2.0标签显示
- 在跳转到这个页面的action中将 WebSys通过spring依赖注入,加入
- private WebSys webSys;
public WebSys getWebSys()
{
return webSys;
}
public void setWebSys(WebSys webSys)
{
this.webSys = webSys;
}
- 页面使用<iframe scrolling="no" src="<ww:property value="webSys.bottomhd"/>" frameborder="0" width="780" height="76">iframe>