本文将实现把系统版本信息存储在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');
}
});
});
<?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中,每次点击都重新获取一次,如果查询次数过多的情况下,要在系统初始化时候就将配置文件中属性值存储在缓存中,以提高性能.