webapp1-freemarker-2.3.16 官方 examples

1、HelloServlet.java

 

package example;

import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import freemarker.template.*;

/**
 * This Servlet does not do anything useful, just prints "Hello World!". The
 * intent is to help you to get started if you want to build your own Controller
 * servlet that uses FreeMarker for the View. For more advanced example, see the
 * 2nd Web application example.
 */
public class HelloServlet extends HttpServlet {
    private Configuration cfg;
   
    public void init() {
        // Initialize the FreeMarker configuration;
        // - Create a configuration instance
        cfg = new Configuration();
        // - Templates are stoted in the WEB-INF/templates directory of the Web app.
        cfg.setServletContextForTemplateLoading(
                getServletContext(), "WEB-INF/templates");
        // In a real-world application various other settings should be explicitly
        // set here, but for the sake of brevity we leave it out now. See the
        // "webapp2" example for them.
    }
   
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
       
        // Build the data-model
        Map root = new HashMap();
        root.put("message", "Hello World!");
       
        // Get the templat object
        Template t = cfg.getTemplate("test.ftl");
       
        // Prepare the HTTP response:
        // - Use the charset of template for the output
        // - Use text/html MIME-type
        resp.setContentType("text/html; charset=" + t.getEncoding());
        Writer out = resp.getWriter();
       
        // Merge the data-model and the template
        try {
            t.process(root, out);
        } catch (TemplateException e) {
            throw new ServletException(
                    "Error while processing FreeMarker template", e);
        }
    }
}

 

2、help.html

<html>
<head>
  <title>FreeMarker Example Web Application 1 - Help</title>
  <meta http-equiv="Content-type" content="text/html; charset=ISO-8859-1">
</head>
<body>

<h1>FreeMarker Example Web Application 1</h1>

<p>To try this example you should visit
<a href="ftl"><code>http://<i>127.0.0.1:8080</i>/<i>yourapplicationname</i>/ftl</code></a>

<h2>What's this example about?</h2>

<p>This Servlet does not do anything useful, just prints "Hello World!". The
intent is to help you to get started if you want to build your own Controller
servlet that uses FreeMarker for the View. For more advanced example, see the
2nd Web application example.
 
</body>
</html>

 

3、在WEB-INF下放置文件templates/test.ftl

<html>
<head>
  <title>FreeMarker Example Web Application 1</title>
</head>
<body>
  ${message}
</body>
</html>

 

4、web.xml

<?xml version="1.0" encoding="utf-8"?>
<web-app version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
   
   <servlet>
    <servlet-name>helloftl</servlet-name>
    <servlet-class>example.HelloServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>helloftl</servlet-name>
    <url-pattern>/ftl</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>help.html</welcome-file>
  </welcome-file-list>
</web-app>

 

5、ok。通过访问http://localhost:8080/yourapplicationname

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值