gpt4 book ai didi

java - 如何使用 servlet 运行 Freemarker 模板?

转载 作者:行者123 更新时间:2023-11-28 22:27:53 29 4
gpt4 key购买 nike

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

但是在servlet上怎么办呢?它是如何工作的?我正在尝试像 .jsp 文件一样在 Tomcat 上运行 .ftl 文件。但它只给我一个错误(“描述请求的资源不可用。”)。

谁能解释一下如何在 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();
}
}

最佳答案

要将 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 分派(dispatch)到 FreeMarker模板文件。

    request.getRequestDispatcher("WEB-INF/views/show.ftl").forward(request, response);
  4. 清理和构建。

看看我的 FreeMarker tutorial ,它展示了如何在 Java 控制台、Servlet 和 Spring Boot 应用程序中设置 FreeMarker。

关于java - 如何使用 servlet 运行 Freemarker 模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34356463/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com