# 8_27Jfinal 框架学习——Tomcat+Maven部署开发(一)

8_27Jfinal 框架学习——Tomcat+Maven部署开发(一)


  1. web.xml配置如下:

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <jsp-config>
        <taglib>
            <taglib-uri>/jfinal.tldtaglib-uri>
            <taglib-location>/jfinal.tldtaglib-location>
        taglib>
    jsp-config>

    <filter>
        <filter-name>jfinalfilter-name>
        <filter-class>com.jfinal.core.JFinalFilterfilter-class>
        <init-param>
            <param-name>configClassparam-name>
            <param-value>com.li.test.DemoConfigparam-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>jfinalfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>
    <welcome-file-list>
        <welcome-file>/pages/index.htmlwelcome-file>
    welcome-file-list>
web-app>
  1. Maven所要导入的依赖
 <dependencies>
        <dependency>
            <groupId>com.jfinalgroupId>
            <artifactId>jfinalartifactId>
            <version>4.2version>
        dependency>

        
        <dependency>
            <groupId>org.eclipse.jettygroupId>
            <artifactId>jetty-jspartifactId>
            <version>9.3.0.M1version>
        dependency>

        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>javax.servlet-apiartifactId>
            <version>4.0.1version>
            <scope>providedscope>
        dependency>
    dependencies>
  1. JfinalTemplate
package com.li.test.util;

import com.jfinal.kit.PathKit;
import com.jfinal.template.Engine;
import com.jfinal.template.Template;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

public class JfinalTemplate 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(JfinalTemplate.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");
        }
        JfinalTemplate.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();
    }

}
  1. jfinal.tld


    1.0
    1.2
    challen
    
        template
        com.li.test.util.JfinalTemplate
        scriptless
        
            render
            false
            true
        
    

你可能感兴趣的:(Jfinal)