jfinalQ开发教程05-qiao-util.jar:常量和配置文件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/uikoo9/article/details/49022251

99miles-wordie-book-icon5-800x600_1x.png

常量

数据字典

相信每个系统不可避免的都会使用常量,而对应的在数据库中则以数据字典的方式记录。


常量文件

jfinalQ中src下contants.properties为常量对应的文件,内容如下:

1.png

这里可以采用key-value的形式记录常量,

key可以用前两位为功能,中两位为模块,后两位为实际值的方式记录。


QContants

jfinalQ中com.uikoo9.z下的QContants是于常量文件对应的代码:

public class QContants {
	
	// 是,否
	public static final String YESNO_YES		= "000101";
	public static final String YESNO_NO			= "000102";
	public static final String[] YESNO			= new String[]{YESNO_YES,YESNO_NO};
}


com.uikoo9.util.plugin.contants

QContantsModel.java:

public class QContantsModel {
	private String value;
	private String text;
}

一个简单的bean,getter和setter省略了。

QContantsUtil.java:

public class QContantsUtil {
	
	private static final Properties CONTANTS = QPropertiesUtil.readProperties("/contants.properties");
	
	public static List<QContantsModel> list(String[] values){
		List<QContantsModel> list = new ArrayList<QContantsModel>();
		if(values != null){
			for(String value : values){
				list.add(new QContantsModel(value, get(value)));
			}
		}
		
		return list;
	}
	
	public static String get(String key){
		return CONTANTS.getProperty(key);
	}
}

读取常量文件中的某一类型列表,在freemarker的页面中需要使用到。


使用

list-后台:后台将常量中的某一系列代码设置进去:

        /**
	 * 跳转到搜索页 
	 */
	public void search(){
		setAttr("yesnos", QContantsUtil.list(QContants.YESNO));
		setAttr("usertypes", QContantsUtil.list(QContants.USER_TYPE));
		
		render("/WEB-INF/view/manage/ucenter/ucenter-user-search.html");
	}


list-前台:对应的前台就可以获取到这个list:

<@bsradios name='row.ucenter_user_mail_confirm' ck='000102' list=yesnos/>


get-前台:还有一种情况是从数据库中取出数据字典,例如000101,要显示对应的常量“是”,这个时候页面可以这样写:

<td>${static.get(row.ucenter_user_type)}</td>


get-static:上面的static是从后台注入到freemarker中的一个工具类,

controller.setAttr("static", QFreemarkerUtil.getStaticClass("com.uikoo9.util.external.QStaticUtil"));

也就是利用之前的全局拦截器QIntercepter,将static变量注入,注入的是上面讲到的QContantsUtil的get方法。


配置文件

config.properties:jfinalQ中的配置文件都保存在config.properties中

com.uikoo9.util.core.file.QPropertiesUtil对操作配置文件做了封装,如下:

/**
 * 资源工具类<br>
 * 1.读取jar包路径<br>
 * 2.读取配置文件<br>
 * 3.获取配置文件中的属性<br>
 * 4.获取配置文件中的属性,返回boolean值<br>
 * @author qiaowenbin
 * @version 0.0.2.20141220
 * @history
 * 	0.0.2.20141220<br>
 * 	0.0.1.20141117<br>
 */
public class QPropertiesUtil {}

默认加载了config.properties文件,使用示例:

String url = QPropertiesUtil.get("sms.url");


jfinalQ2.0

官网:http://uikoo9.com/jfinalQ

源码:https://github.com/uikoo9/jfinalQ


更多精彩内容:http://uikoo9.com/


求打赏(长按图片即可识别)~
微信 捐助列表:http://uikoo9.com/donate/

猜你喜欢

转载自blog.csdn.net/uikoo9/article/details/49022251