Solon

solon.view.enjoy [国产]

v2.7.3 </> markdown
<dependency>
    <groupId>org.noear</groupId>
    <artifactId>solon.view.enjoy</artifactId>
</dependency>

1、描述

视图扩展插件,为 Solon View 提供基于 jfinal enjoy(代码仓库)的框架适配。

2、应用示例

DemoController.java

//顺便,加个国际化在模板里的演法
@I18n
@Controller
public class DemoController{
    @Mapping("/test")
    public ModelAndView test() {
        ModelAndView model = new ModelAndView("enjoy.shtm");
        model.put("title", "dock");
        model.put("msg", "你好 world!");

        return model;
    }
}

resources/WEB-INF/templates/enjoy.shtm

<!DOCTYPE html>
<html>
<head>
    <title>#(title)</title>
</head>
<body>
enjoy::#(msg);i18n::#(i18n.get("login.title"))
</body>
</html>

3、自定义标签示例

以插件自带的权限验证标签为例:

#authPermissions("user:del")
我有user:del权限
#end

自定义标签和共享对象代码实现:

@Component("view:authPermissions")
public class AuthPermissionsTag extends Directive {
    @Override
    public void exec(Env env, Scope scope, Writer writer) {
        String[] attrs = getAttrArray(scope);

        if(attrs.length == 0){
            return;
        }

        String nameStr = attrs[0];
        String logicalStr = null;
        if(attrs.length > 1){
            logicalStr = attrs[1];
        }

        if (Utils.isEmpty(nameStr)) {
            return;
        }

        String[] names = nameStr.split(",");

        if (names.length == 0) {
            return;
        }

        if (AuthUtil.verifyPermissions(names, Logical.of(logicalStr))) {
            stat.exec(env, scope, writer);
        }
    }

    @Override
    public boolean hasEnd() {
        return true;
    }
    
    private String[] getAttrArray(Scope scope) {
        Object[] values = exprList.evalExprList(scope);
        String[] ret = new String[values.length];
        for (int i = 0; i < values.length; i++) {
            if (values[i] instanceof String) {
                ret[i] = (String) values[i];
            } else {
                throw new IllegalArgumentException("Name can only be strings");
            }
        }
        return ret;
    }
}


@Component("share:demo1")
public class Demo {
    public String hello(){
        return "hello";
    }
}