读取properties文件中属性值并显示到前端页面

本文介绍了一种通过properties配置文件存储并展示系统版本信息的方法。利用Java的ResourceBundle类读取配置文件,通过Action处理前端请求返回版本详情。前端使用AJAX异步请求获取并显示这些信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文将实现把系统版本信息存储在properties配置文件中,然后在前端页面可点击版本图标获取版本信息.

version.properties:

#SVN版本
SVNVersion = 211937
#版本日期
VersionDate = 2016-08
#平台版本号
PlatformVersion = V2.3.5

VersionAction.java:

@Controller(value = "versionAction")
@Scope("prototype")
public class VersionAction extends BaseAction{
	
	private static final long serialVersionUID = 3656174215921050826L;
	private String SVNVersion;//svn版本
	private String VersionDate;//版本日期
	private String PlatformVersion;//平台版本
	
	public String getVersionInfo() throws UnsupportedEncodingException {
		
		SimpleJsonResult result = getSuccessresult();
		Map<Object, Object> map = new HashMap<Object, Object>();
		
		ResourceBundle resourceBundle = VersionConfigUtil.getResources("version");
		if (resourceBundle != null) {
			for (String set : resourceBundle.keySet()) {
				String value = resourceBundle.getString(set);
				map.put(set, value);
			}
		}	
		result.setResult(map);
		root = result;
		return JSON;
	}
	
	
	public String getSVNVersion() {
		return SVNVersion;
	}

	public void setSVNVersion(String SVNVersion) {
		this.SVNVersion = SVNVersion;
	}
	
	public String getVersionDate() {
		return VersionDate;
	}

	public void setVersionDate(String VersionDate) {
		this.VersionDate = VersionDate;
	}
	
	public String getPlatformVersion() {
		return PlatformVersion;
	}

	public void setPlatformVersion(String PlatformVersion) {
		this.PlatformVersion = PlatformVersion;
	}
}

VersionConfigUtil.java:从properties文件取属性值的工具类,本功能核心类.

public class VersionConfigUtil {
	
	private static Map<String,ResourceBundle> configMap = new HashMap<String, ResourceBundle>();
	
	private  static synchronized void loadResource(String propertyFileName){
		ResourceBundle resource = ResourceBundle.getBundle(propertyFileName);
		configMap.put(propertyFileName, resource);
	}
	
	/**
	 * 在指定文件中获取指定的配置属性
	 * @param key
	 * @param propertyFileName
	 * @return
	 */
	public static String getValue(String key,String propertyFileName) {
		ResourceBundle resource = configMap.get(propertyFileName);
		if(resource == null){
			loadResource(propertyFileName);
			resource = configMap.get(propertyFileName);
		}
		return resource.getString(key);
	}
	
	/**
	 * 在指定文件中获取所有属性集
	 * @param propertyFileName
	 * @return
	 */
	public static ResourceBundle getResources(String propertyFileName){
		ResourceBundle resource = configMap.get(propertyFileName);
		if(resource == null){
			loadResource(propertyFileName);
			resource = configMap.get(propertyFileName);
		}
		return resource;
	}
}


version.jsp:因为AJAX是异步请求值,所以注意要把AJAX请求写在点击事件里面.

<div href="javascript:void(0);" class="func-item version">
	<i class="icon"></i>
</div>
</pre><pre name="code" class="html">$('.version').on('click',function(){
<span style="white-space:pre">	</span>Common.versionHtml = '';
	$.ajax({
		url: contextPath+"/base/version!getVersionInfo.action", 
		type: "POST",
		success : function(resp){
			if(!resp.success) return ;
			var SVNVersion = resp.result.SVNVersion,VersionDate = resp.result.VersionDate,PlatformVersion = resp.result.PlatformVersion;
			Common.versionHtml = '<div class="version_panel">'
				+'<div class="logo-info"></div>'	
				+'<div class="version_body">'	
				+'<h5 class="title_left"></h5>'
				+'<span class="title_right"></span>'
				+'<div class="company"> SVN版本号:'+SVNVersion+'</div>'
				+'<div class="content"><p>版本日期:'+VersionDate+'<br/>平台版本号:'+PlatformVersion+'</p></div>'
				+'</div>'
				+'<div class="cf"></div>'
				+'</div>';
				
			art.dialog({
				lock :true,
				title:'XXX系统',
				opacity:0.2,
				content: Common.versionHtml
			});
			$('.aui_content').css('padding','0');
		}
	});	
		
});


struts-base.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
	<package name="base" namespace="/base" extends="platform-default">		
		<action name="version" class="versionAction"></action>		
	</package>
</struts>

注:鉴于查看版本信息功能用户点击次数很少,所以信息放在Action中,每次点击都重新获取一次,如果查询次数过多的情况下,要在系统初始化时候就将配置文件中属性值存储在缓存中,以提高性能.


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值