无法获得Freemarker工作的基本实现

时间:2015-08-18 13:09:14

标签: java servlets freemarker

我正在尝试从Servlet配置Freemarker模板,如下所示:

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

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
@WebServlet("/Controller")
public class Controller extends HttpServlet {

    // class fields
    private static final long serialVersionUID = 1L;
    private static final String views = "/WEB-INF/views/";

    public Controller(){

    }

    // used to handle any get requests i.e. webpage navigation
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Configuration conf = new Configuration();
        conf.setClassForTemplateLoading(Controller.class, views);
        conf.setDefaultEncoding("UTF-8");

        Template indexTemplate = conf.getTemplate("index.ftl");
        StringWriter writer = new StringWriter();
        Map<String, Object> indexMap = new HashMap<String, Object>();
        indexMap.put("name", "Chris");

        try {
            indexTemplate.process(indexMap, writer);
        } catch (TemplateException e) {
            e.printStackTrace();
        }

        System.out.println(writer);

    }
}

但是当我启动Tomcat服务器时出现以下错误:

java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/webapp]]
    at java.util.concurrent.FutureTask.report(Unknown Source)
    at java.util.concurrent.FutureTask.get(Unknown Source)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1122)
    at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:819)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1574)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1564)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/webapp]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
    ... 6 more

这是文件结构:

enter image description here

我在这里做错了什么?

0 个答案:

没有答案