# jsp-jstl-engine
**Repository Path**: anyline/jsp-jstl-engine
## Basic Information
- **Project Name**: jsp-jstl-engine
- **Description**: java jsp&jstl template engine
- **Primary Language**: Unknown
- **License**: GPL-2.0
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2016-01-17
- **Last Updated**: 2022-06-27
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
java-template
===================
java template jstl
Ayada is a "template engine"; a generic tool to generate text output (anything from HTML to autogenerated source code) based on templates. It's a Java package, a class library for Java programmers. It's not an application for end-users in itself, but something that programmers can embed into their products.
更详细的文档请参考: docs/html/index.html
项目目录结构:
src - 源码
release - 最终发布包
docs/html - 相关文档
Tag Example
===================
c:if
1 == 1
out:
1 == 1
c:forEach & c:each
${status.index}: ${myvar}
out:
0: 1
1: 2
2: 3
c:forEach & c:each
${status.index}: ${myvar}
out:
0: 1
1: 2
2: 3
c:forEach & c:each
var varList = [1, 2, 3];
${status.index}: ${myvar}
out:
0: 1
1: 2
2: 3
c:set
c:out
out:
1,2,3
c:choose & c:when & c:otherwise
test1
test2
test3
c:comment
...
fmt:formatDate
sql:connect
...
...
...
sql:execute
...
...
...
sql:query
...
c:include
c:macro
import a tag, scope: current page
t:include
include a page
配置说明:
ayada-default.properties
ayada.compile.source-factory=com.skin.ayada.template.DefaultSourceFactory # 脚本加载factory, 从文件系统加载
ayada.compile.source-pattern=jsp,jspf,jspx,tpl # 脚本扩展名, 其他类型的文件会被认为是静态文件
ayada.compile.ignore-jsptag=true # 忽略jsp标签
ayada.compile.fast-jstl=true # 是否使用fast-jstl
ayada-default.properties
set util=com.skin.ayada.jstl.util.BeanUtil # 定义一个全局工具类
el表达式转义
在ayada-default.properties文件中添加一行
set ELEncoder=com.skin.ayada.jstl.util.HtmlEncoder # 对所有的el表达式的输出使用html编码之后再输出
在脚本中输出脚本
有时候我们会希望在脚本中输出脚本, 例如页面中输出一段模板, 这段模板在js中再使用js模板引擎进行渲染, 这中情况下可以使用t:text指令
t:text标签中的任何内容都会作为静态内容原样输出, 如果escape="xml", 那么其中的内容还会被做html编码
t:text是编译指令,因此转码是在编译期进行的, 以后每次执行都直接输出
API Example
===================
// demo1
String sourcePattern = "jsp,jspx";
String home = "E:\\WorkSpace\\test\\webapp\\template";
SourceFactory sourceFactory = new DefaultSourceFactory();
TemplateFactory templateFactory = new TemplateFactory();
ExpressionFactory expressionFactory = new DefaultExpressionFactory();
sourceFactory.setHome(home);
sourceFactory.setSourcePattern(sourcePattern);
TemplateContext templateContext = new DefaultTemplateContext(home, "UTF-8");
templateContext.setSourceFactory(sourceFactory);
templateContext.setTemplateFactory(templateFactory);
templateContext.setExpressionFactory(expressionFactory);
StringWriter writer = new StringWriter();
Template template = templateContext.getTemplate("/test.jsp");
templateContext.execute(template, new HashMap(), writer);
System.out.println(writer.toString());
Source source = new Source("", "", 0);
SourceFactory sourceFactory = new MemorySourceFactory(source);
TagLibrary tagLibrary = TagLibraryFactory.getStandardTagLibrary();
TemplateCompiler compiler = new TemplateCompiler(sourceFactory);
compiler.setTagLibrary(tagLibrary);
Template template = compiler.compile("", "UTF-8");
StringWriter writer = new StringWriter();
TemplateContext templateContext = new TemplateContext("");
PageContext pageContext = JspFactory.getPageContext(templateContext, writer);
List userList = UserHandler.getUserList(16);
pageContext.setAttribute("userList", userList);
DefaultExecutor.execute(template, pageContext);
System.out.println(writer.toString());
// demo2
TemplateContext templateContext = new TemplateContext(home);
Template template = templateContext.getTemplate(file);
StringWriter writer = new StringWriter();
PageContext pageContext = getPageContext(writer);
DefaultExecutor.execute(template, pageContext);
System.out.println("-------------- source --------------");
System.out.println(TemplateUtil.toString(template));
System.out.println("-------------- result --------------");
System.out.println(writer.toString());
// demo3
SourceFactory sourceFactory = new DefaultSourceFactory("webapp");
TagLibrary tagLibrary = TagLibraryFactory.getStandardTagLibrary();
TemplateCompiler compiler = new TemplateCompiler(sourceFactory);
compiler.setTagLibrary(tagLibrary);
Template template = compiler.compile("/large.html", "UTF-8");
StringWriter writer = new StringWriter();
PageContext pageContext = getPageContext(writer);
DefaultExecutor.execute(template, pageContext);
System.out.println("------------- result -------------");
System.out.println(writer.toString());
// demo4
String home = "com/skin/ayada/demo";
SourceFactory sourceFactory = new ClassPathSourceFactory(home);
TemplateContext templateContext = new TemplateContext(home);
templateContext.setSourceFactory(sourceFactory);
Template template = templateContext.getTemplate("/hello.jsp");
StringWriter writer = new StringWriter();
PageContext pageContext = JspFactory.getPageContext(writer);
System.out.println("-------------- source --------------");
System.out.println(TemplateUtil.toString(template));
System.out.println("-------------- System.out.print --------------");
DefaultExecutor.execute(template, pageContext);
System.out.println("-------------- result --------------");
System.out.println(writer.toString());
TemplateFilter
com.skin.ayada.filter.TemplateFilter
home
/template
source-factory
com.skin.ayada.source.ClassPathSourceFactory
template-factory
com.skin.ayada.template.JspTemplateFactory
jsp-work
context: /WEB-INF/ayada
TemplateFilter
/template/*
FORWARD
java: https://github.com/xuesong123/ayada
javascript: https://github.com/xuesong123/javascript-template