如何使用servlet运行Freemarker模板?

时间:2015-12-18 13:09:12

标签: java jsp tomcat servlets freemarker

我正在使用Freemarker模板编写应用程序。我通过Java类实现了如何配置和运行.ftl模板机制的知识,并使控制台或文件输出在其上打印模板结果。

但是如何在servlet上做呢?它是如何工作的?我试图在Tomcat上运行.ftl文件,如.jsp文件。但它只给我一个错误("描述所请求的资源不可用。")。

有人可以解释一下如何在servlet上运行.ftl文件吗?

这是我的web.xml:

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
    <servlet-name>freemarker</servlet-name>
    <servlet-class>freemarker.ext.servlet.FreemarkerServlet</servlet-class>

    <!-- FreemarkerServlet settings: -->
    <init-param>
        <param-name>TemplatePath</param-name>
        <param-value>/</param-value>
    </init-param>
    <init-param>
        <param-name>NoCache</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>ContentType</param-name>
        <param-value>text/html; charset=UTF-8</param-value> <!-- Forces UTF-8 output encoding! -->
    </init-param>

    <!-- FreeMarker settings: -->
    <init-param>
        <param-name>incompatible_improvements</param-name>
        <param-value>2.3.22</param-value>
        <!-- Recommended to set to a high value. For the details, see the Java 
            API docs of freemarker.template.Configuration#Configuration(Version). -->
    </init-param>
    <init-param>
        <param-name>template_exception_handler</param-name>
        <!-- Use "html_debug" instead during development! -->
        <param-value>rethrow</param-value>
    </init-param>
    <init-param>
        <param-name>template_update_delay</param-name>
        <!-- ATTENTION, 0 is for development only! Use higher value otherwise. -->
        <param-value>0</param-value>
    </init-param>
    <init-param>
        <param-name>default_encoding</param-name>
        <!-- The encoding of the template files. -->
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>locale</param-name>
        <!-- Influences number and date/time formatting, etc. -->
        <param-value>en_US</param-value>
    </init-param>
    <init-param>
        <param-name>number_format</param-name>
        <param-value>0.##########</param-value>
    </init-param>

    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>freemarker</servlet-name>
    <url-pattern>*.ftl</url-pattern>
</servlet-mapping>

<servlet>
    <servlet-name>tomcat</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>tomcat</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

这是我的Java代码可以工作,并在控制台上打印输出:

public static void main(String[] args) {
    ApplicationContext context = 
            new ClassPathXmlApplicationContext("context.xml");

    FreeMarkerConfiguration configurationWrapper = 
            context.getBean("freeMarkerConf", FreeMarkerConfiguration.class);

    Configuration cfg = configurationWrapper.getConfiguration();

    try {
        cfg.setSharedVariable("title", "SharedTitle");
    } catch (TemplateModelException e1) {
        e1.printStackTrace();
    }

    Map<String, Object> data = new HashMap<String, Object>();
    //data.put("title", "Hello");
    data.put("name", "Andrew");

    List<String> list = new ArrayList<>();
    list.add("Poland");
    list.add("Austria");
    list.add("Germany");
    list.add("Hungary");

    data.put("countries", list);

    Writer out = new OutputStreamWriter(System.out);

    try {
        Template template = cfg.getTemplate("index.ftl");
        template.process(data, out);

        Template template2 = cfg.getTemplate("list.ftl");
        template2.process(data, out);
    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:0)

要将FreeMarker与Java Servlet一起使用,请按照以下简单步骤操作:

  1. 添加Maven依赖项。

    <dependency>
        <groupId>org.freemarker</groupId>
        <artifactId>freemarker</artifactId>
        <version>${freemarker-version}</version>
    </dependency>   
    
  2. web.xml文件中配置FreeMarker。 复制并粘贴FreeMarker website中的设置。

  3. 将处理从Servlet发送到FreeMarker 模板文件。

    request.getRequestDispatcher("WEB-INF/views/show.ftl").forward(request, response);
    
  4. 清洁&amp;建立。

  5. 查看我的FreeMarker tutorial,其中显示了如何在Java控制台,Servlet和Spring Boot应用程序中设置FreeMarker。