最近在学习jboot项目,那最好的学习资料就是其官方基于jboot写的jpress。由于jpress有些代码注释不足,我就边学习,边把注释记录上,同时补充一下java的知识(我常年开发基于C#的cs客户端)ps.不知道分享这样的内容会不会有些不妥,如有不妥,詹总说一下,我删帖。
1、io.jpress.commons.utils.CommonsUtils :
/**
* @author Michael Yang 杨福海 (fuhai999@gmail.com)
* @version V1.0
* @Package io.jpress.commons.utils
*/
public class CommonsUtils {
/**
* 生成1000以上的随机码
* @return
*/
public static String generateCode() {
Random random = new Random();
return String.valueOf(random.nextInt(9999 - 1000 + 1) + 1000);
}
/**
* 安静的关闭 参数:实现了自动关闭资源接口的类
* java类型bai后面跟三个点是代表可du以接受多个实际参数zhi,这里的多个指的是dao不限个数,可以zhuan是一个、两个、三个甚至更多。
* java中类型后面加三个点是java1.5之后出现的新的内容,使用在函数的形参上,相当于一个数组,调用函数时传递多少了实际参数,都可以存储到这个形参上,需要注意的是,使用这个形参必须放在最后一位形参位置上,否则会报错!
* @param autoCloseables
*/
public static void quietlyClose(AutoCloseable... autoCloseables) {
for (AutoCloseable closeable : autoCloseables) {
if (closeable != null) {
try {
closeable.close();
} catch (Exception e) {
// do nothing
}
}
}
}
/**
* 最大文本长度
* @param content
* @param maxLength
* @return
*/
public static String maxLength(String content, int maxLength) {
return CommonsUtils.maxLength(content, maxLength, null);
}
/**
* 取文本最大长度
* 若文本长度小于@maxLength,则直接返回文本。否则截取长度为@maxLength的文本并追加@suffix(if not null or blank)
* @param content 文本内容
* @param maxLength 最大长度
* @param suffix 后缀
* @return 文本
*/
public static String maxLength(String content, int maxLength, String suffix) {
if (StrUtil.isBlank(content)) {
return content;
}
if (maxLength <= 0) {
throw new IllegalArgumentException("maxLength 必须大于 0 ");
}
if (StrUtil.isNotBlank(suffix)) {
return content.length() <= maxLength
? content :
content.substring(0, maxLength) + suffix;
} else {
return content.length() <= maxLength
? content :
content.substring(0, maxLength);
}
}
/**
* 移除后缀
* @param url 链接
* @return 移除后缀的链接
*/
public static String removeSuffix(String url) {
int indexOf = url.indexOf(".");
if (indexOf == -1) {
return url;
}
return url.substring(0, indexOf);
}
/**
* 防止 model 存储关于 xss 相关代码
* @param model activerecord类
* @param ignoreAttrs 需要忽略的属性
*/
public static void escapeModel(Model model, String... ignoreAttrs) {
String[] attrNames = model._getAttrNames();
for (String attr : attrNames) {
if (ArrayUtils.contains(ignoreAttrs, attr)) {
continue;
}
Object value = model.get(attr);
if (value != null && value instanceof String) {
// 通过StrUtil的escapeHtml转义可能的html代码(将< > & " '转成字符实体)
model.set(attr, StrUtil.escapeHtml(value.toString()));
}
}
}
/**
* 防止 Map 存储关于 xss 相关代码
* @param map Map类
* @param ignoreKeys 需要忽略的键
*/
public static void escapeMap(Map map, Object... ignoreKeys) {
if (map == null || map.isEmpty()) {
return;
}
Set keys = map.keySet();
for (Object key : keys) {
if (ArrayUtils.contains(ignoreKeys, key)) {
continue;
}
Object value = map.get(key);
if (value != null && value instanceof String) {
map.put(key, StrUtil.escapeHtml(value.toString()));
}
}
}
/**
* 测试
* @param args
*/
public static void main(String[] args) {
System.out.println(generateCode());
}
}2、jpress的权限设计
数据表:
![]()

此外,你的内容在复制的时候,要先点击一下那个贴源码的下拉,否则你现在的内容全是乱掉的