1

我的问题很简单。我想使用另一个带有 freemarker 的 taglib。我在 freemarker 文档中读到这是可能的。我想使用 kendoui taglib

<%@taglib prefix="kendo" uri="http://www.kendoui.com/jsp/tags"%>

api doc说我们必须通过添加一行来做到这一点

<#assign html=JspTaglibs["/WEB-INF/struts-html.tld"]>.

但是当我这样做时,我有错误

The following has evaluated to null or missing: ==> JspTaglibs 

亲切地。


谢谢你的回答。当我使用 freemarkerServlet 时,它可以工作。
但我想使用一个 servlet,它允许我配置 freemarker(在 init 方法中)并解析对响应编写器的 html 响应。

req.setCharacterEncoding(cfg.getOutputEncoding());
resp.setContentType("text/html; charset=" + cfg.getOutputEncoding());
resp.setHeader("Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
resp.setHeader("Pragma", "no-cache");
resp.setHeader("Expires", "Thu, 01 Dec 1994 00:00:00 GMT");
Writer out = resp.getWriter();
template.process(page.getRoot(), out);

事实上,我想扩展 freemarkerServlet 类,如此处提到的:http://schakrap.wordpress.com/2009/09/05/using-freemarkerservlet-in-google-guice-to-inject-configuration/

但我仍然有同样的错误。

对于此事,配置参数 TemplatePath 是什么意思?

4

2 回答 2

0
    package presentation;
import java.io.IOException;
import java.io.StringReader;
import java.io.Writer;
import java.text.DateFormat;
import java.util.Locale;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import freemarker.core.Macro;
import freemarker.template.Configuration;
import freemarker.template.ObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;

public class ControllerServlet extends freemarker.ext.servlet.FreemarkerServlet
{
    //
    private Configuration   cfg;
    private Handler handler;

    public ControllerServlet()
    {
        super();
    }


    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {

        String userAgentString = "";

        StringBuffer url = req.getRequestURL();

        String sUrl = url.toString();

        // on instancie les classes/structures pour Freemarker
        FreemarkerParameterMap page = new FreemarkerParameterMap();

        handler = new Handler();

        try
        {
            userAgentString = req.getHeader("User-Agent");

            System.out.println("User-Agent : " + userAgentString);

            handler.handle(req, resp, page);

            if (page.getDataType() == FreemarkerParameterMap.AJAX_JSON)
            {
                Gson gson = new GsonBuilder().setDateFormat(DateFormat.FULL).setPrettyPrinting().create();
                Map<String, Object> map = page.getRoot();
                resp.setContentType("application/json;charset=UTF-8");
                resp.getWriter().println(gson.toJson(map));
                resp.getWriter().flush();
            }
            else if (page.getDataType() == FreemarkerParameterMap.HTML_REDIRECT && page.getRedirect() != null)
            {
                resp.sendRedirect(page.getRedirect());
            }
            else if (page.getDataType() == FreemarkerParameterMap.HTML_FORWARD && page.getForward() != null)
            {
                RequestDispatcher rd = req.getRequestDispatcher(page.getForward());
                rd.forward(req, resp);
            } 

            else
            {
                if (page.getTemplate() != null)
                {
                    Template t = cfg.getTemplate("theme/" + page.getTemplate());

                    if (page.getDataType() == FreemarkerParameterMap.AJAX_HTML)
                    {
                        Map mapMacro = t.getMacros();
                        Macro macro = (Macro) mapMacro.get(page.getMacro());
                        t = new Template("name", new StringReader(macro.getSource()+"<@"+page.getMacro()+"/>"), cfg);
                    }
                    else if (page.getDataType() == FreemarkerParameterMap.HTML)
                    {
                    }

                    req.setCharacterEncoding(cfg.getOutputEncoding());
                    resp.setContentType("text/html; charset=" + cfg.getOutputEncoding());
                    resp.setHeader("Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
                    resp.setHeader("Pragma", "no-cache");
                    resp.setHeader("Expires", "Thu, 01 Dec 1994 00:00:00 GMT");
                    Writer out = resp.getWriter();

                    t.process(page.getRoot(), out);
                }
                else
                {
                    throw new ServletException("aucune action  specifiée");
                }
            } 

        }

        /*catch (EcoliaException e)
        {
        } */
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }


    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {

        doGet(req, resp);
    }


    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException
    {
        // Initialize the FreeMarker configuration;
        // - Create a configuration instance
        cfg = new Configuration();

        // - Templates are stored in the WEB-INF/templates directory of the Web app.
        cfg.setServletContextForTemplateLoading(getServletContext(), "");

        // - Set update dealy to 0 for now, to ease debugging and testing.
        //   Higher value should be used in production environment.
        cfg.setTemplateUpdateDelay(0);

        // - Set an error handler that prints errors so they are readable with
        //   a HTML browser.

        cfg.setTemplateExceptionHandler(TemplateExceptionHandler.HTML_DEBUG_HANDLER);

        // - Use beans wrapper (recommmended for most applications) 
        cfg.setObjectWrapper(ObjectWrapper.BEANS_WRAPPER);

        // - Set the default charset of the template files
        cfg.setDefaultEncoding("UTF-8");

        // - Set the charset of the output. This is actually just a hint, that
        //   templates may require for URL encoding and for generating META element
        //   that uses http-equiv="Content-type".
        cfg.setOutputEncoding("UTF-8");

        // - Set the default locale
        cfg.setLocale(Locale.US);

        System.out.println("init controleur");
    }

}

在 web.xml 我有这个:

<servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>ControllerServlet</servlet-name>
    <servlet-class>presentation.ControllerServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>ControllerServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
于 2013-08-16T15:27:04.240 回答
0

JspTaglibs变量以及自定义 JSP 标记感到宾至如归所需的整个环境是由freemarker.ext.servlet.FreemarkerServlet. 所以目前使用 taglibs 的唯一方法是 with FreemarkerServlet,这是为了让您可以在旧版 JSP Model-2 框架中使用 FTL 文件而不是 JSP 文件。

于 2013-08-13T16:47:08.873 回答