Solon v3.0.3

solon-view-jsp

</> markdown
<dependency>
    <groupId>org.noear</groupId>
    <artifactId>solon-view-jsp</artifactId>
</dependency>

1、描述

视图扩展插件,为 Solon View 提供基于 jsp 的框架适配。

需要配合插件:

  • solon-boot-jetty + solon-boot-jetty-add-jsp

或者

  • solon-boot-undertow + solon-boot-undertow-add-jsp

2、应用示例

DemoController.java

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

        return model;
    }
}

resources/WEB-INF/tags.tld

<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" version="2.0">

    <description>自定义标签库</description>
    <tlib-version>1.1</tlib-version>
    <short-name>ct</short-name>
    <uri>/tags</uri>

    <tag>
        <name>footer</name>
        <tag-class>webapp.widget.FooterTag</tag-class>
        <body-content>empty</body-content>
    </tag>

</taglib>

resources/WEB-INF/templates/jsp.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="ct" uri="/tags" %>
<!DOCTYPE html>
<html>
<head>
    <title>${title}</title>
</head>
<body>
jsp::${msg};i18n::${i18n.get("login.title")}

<ct:footer />
</body>
</html>

3、自定义标签示例

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

public class FooterTag extends TagSupport {
    @Override
    public int doStartTag() throws JspException {
        try {
            StringBuffer sb = new StringBuffer();
            sb.append("<div>").append("你好 world!").append("</div>");
            pageContext.getOut().write(sb.toString());
        } catch (Exception e) {
            EventBus.push(e);
        }

        return super.doStartTag();
    }

    @Override
    public int doEndTag() throws JspException {
        return super.doEndTag();
    }
}

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

public class AuthPermissionsTag extends TagSupport {
    @Override
    public int doStartTag() throws JspException {
        String nameStr = name;
        String logicalStr = logical;

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

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

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


        if (AuthUtil.verifyPermissions(names, Logical.of(logicalStr))) {
            return TagSupport.EVAL_BODY_INCLUDE;
        } else {
            return super.doStartTag();
        }
    }


    String name;
    String logical;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLogical() {
        return logical;
    }

    public void setLogical(String logical) {
        this.logical = logical;
    }
}