相信用过魔板的伙伴很难再使用其他的渲染方式了, 比如JSP等...
但是身在江湖, 老客户提的新需求还得在老项目中做不是... 而且由于历史遗留问题... 还不能简单的全部切换到新技术中去...
那么就简单的写个插件用下吧... 分享一下
利用jsp的自定义标签Tag
package com.momathink.common.tag; import java.io.IOException; import java.io.StringWriter; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.jsp.JspException; import javax.servlet.jsp.PageContext; import javax.servlet.jsp.tagext.SimpleTagSupport; import com.jfinal.kit.PathKit; import com.jfinal.template.Engine; import com.jfinal.template.Template; /*** * 让JSP也用上jfinal的模版引擎 * * @author dufuzhong */ public class TemplateTag extends SimpleTagSupport { private static Engine engine = null; private String render = null; public String getRender() { return render; } public void setRender(String render) { this.render = render; } public static Engine getEngine() { if (engine != null) { return engine; } engine = Engine.create(TemplateTag.class.getName()) .setDevMode(true) .setBaseTemplatePath(PathKit.getWebRootPath()); return engine; } /*** * 自己定义 Engine * @param engine */ public static void setEngine(Engine engine) { if (engine == null) { throw new IllegalArgumentException("engine can not be null"); } TemplateTag.engine = engine; } @Override public void doTag() throws JspException, IOException { Template template = null; if(getRender() != null) { // 使用文件模版 template = getEngine().getTemplate(getRender()); }else { // 使用JSP标签体内的字符串模版 StringWriter sw = new StringWriter(); getJspBody().invoke(sw); template = getEngine().getTemplateByString(sw.toString()); } template.render(getData(), getJspContext().getOut()); } private Map<Object, Object> getData() { HttpServletRequest request = getRequest(); Map<Object, Object> data = new HashMap<Object, Object>(16); for (Enumeration<String> attrs = request.getAttributeNames(); attrs.hasMoreElements();) { String attrName = attrs.nextElement(); data.put(attrName, request.getAttribute(attrName)); } return data; } protected HttpServletRequest getRequest() { return (HttpServletRequest) ((PageContext)this.getJspContext()).getRequest(); } }
2.注册 Tag , 根目录/common/建一个文件: jfinal.tld (可以建其他地方和下面地址对上就可以了)
<?xml version="1.0" encoding="UTF-8"?> <taglib> <tlib-version>1.0</tlib-version> <jsp-version>1.2</jsp-version> <short-name>challen</short-name> <tag> <name>template</name> <tag-class>com.momathink.common.tag.TemplateTag</tag-class> <body-content>scriptless</body-content> <attribute> <name>render</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>
3 . web.xml 中加入taglib
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <jsp-config> <taglib> <taglib-uri>/common/jfinal.tld</taglib-uri> <taglib-location>/common/jfinal.tld</taglib-location> </taglib> </jsp-config> <filter> <filter-name>jfinal</filter-name> <filter-class>com.jfinal.core.JFinalFilter</filter-class> <init-param> <param-name>configClass</param-name> <param-value>写你自己的Config路径</param-value> </init-param> </filter> <filter-mapping> <filter-name>jfinal</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
4 . 例子使用 : JSP页面顶部加入
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@taglib prefix="jfinal" uri="/common/jfinal.tld" %> <html> <head> ... 省略一万字的html标签 <div> <!-- 模版文件渲染 --> <jfinal:template render="/workbench/你自已的Template.html"/> <!-- 字符串模版渲染 --> <jfinal:template> #("你好 jfinal template") ${xxx}//混用也可以的 </jfinal:template> </div> ... 省略一万字的html标签 </body> </html>
过渡期的项目也可以用下
23333 就算老项目我也要嗨起来~~~ JFinal使用技巧~点个赞呗~