欢迎转发分享, 转发请附上本文地址: https://blog.csdn.net/Luoxianxun/article/details/106399780
一、实现需求
实现在html 中使用 #if(调用java类中的方法) #end;
二、使用技术
JFinal后台框架
JFinal 官方文档:https://jfinal.com/doc/6-3
三、文档讲解
需要使用到模板引擎, 官方文档写法 (截图)
如上截图中内容:
#if(com.jfinal.kit.StrKit::isBlank(title))....
#end
(com.jfinal.MyKit::me).method(paras) 是Java类中的静态方法。
四、后端引擎配置
直接上代码:
public class JfinalConfigListener extends JbootAppListenerBase {@Overridepublic void onConstantConfig(Constants constants) {}@Overridepublic void onEngineConfig(Engine engine) {//在此处配置上需要调用的类engine.addSharedMethod(new NumberUtils());}@Overridepublic void onInterceptorConfig(Interceptors interceptors) {}@Overridepublic void onHandlerConfig(JfinalHandlers handlers) {}@Overridepublic void onStart() {}
}
如上代码中: 配置上需要使用的类
@Override
public void onEngineConfig(Engine engine) {//在此处配置上需要使用的公共类 (其他代码可以忽视)engine.addSharedMethod(new NumberUtils());
}
再上 NumberUtils 类
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/*** 这是判断字符串中是否含有字母的正则表达式*/
public class NumberUtils {//此处的方法必须加上 static 修饰public static boolean isInclude(String str){String regex=".*[a-zA-Z]+.*";Matcher m = Pattern.compile(regex).matcher(str);return !m.matches();}
}
五、前端使用
#if(com.luo.util.NumberUtils::isInclude(str))<div class="layui-form-item"><label for="bank" class="layui-form-label" ><em class="require-mark">*</em>支行</label><div class="layui-input-inline"><input type="text" id="bank" name="bank" disabled value="#(m.bank??)" class="layui-input phone" autocomplete="off" style="width:539px"></div></div>
#end
如上代码所示:
#if(com.luo.util.NumberUtils::isInclude(str))
#end
在 需要判断的 html 代码外加上,其中
com.luo.util.NumberUtils 是 NumberUtils 的包名地址
isInclude 是类里面的静态方法。注意必须是 静态方法,不然会报错。
str 是传送的参数。