【问题标题】:Converting A JSP Tag To A JSF Component将 JSP 标记转换为 JSF 组件
【发布时间】:2009-05-13 14:51:48
【问题描述】:

我有一个 JSP 标签,我想在我的 JSF/Seam 应用程序中使用它。有人可以给我一些指导,让我的环境接受标签。我可以将一个 faclets *.taglib.xml 文件指向我的旧标签,还是我需要编写一个扩展前一个标签的组件?

为任何信息干杯, 李

【问题讨论】:

    标签: java jsf taglib


    【解决方案1】:

    我非常不愿意尝试在 JSP 上下文之外直接调用 JSP 标记。作为documentation points out,JSP 和Facelets 之间的相似之处非常肤浅。

    一种技巧(我怀疑任何解决方案都将是一种技巧)可能是通过强制转换为 servlet API 来包含 JSP。

    此函数包含使用RequestDispatcher 的给定资源:

    public class Includer {
    
      public static String include(String resource) {
        FacesContext context = FacesContext
            .getCurrentInstance();
        ExternalContext ext = context.getExternalContext();
        include(ext.getContext(), ext.getRequest(), ext
            .getResponse(), resource);
        return "";
      }
    
      private static void include(Object context,
          Object request, Object response, String resource) {
        ServletContext servletContext = (ServletContext) context;
        ServletRequest servletRequest = (ServletRequest) request;
        ServletResponse servletResponse = (ServletResponse) response;
        RequestDispatcher dispatcher = servletContext
            .getRequestDispatcher(resource);
        try {
          dispatcher.include(servletRequest, servletResponse);
        } catch (IOException e) {
          throw new FacesException(e);
        } catch (ServletException e) {
          throw new FacesException(e);
        }
      }
    
    }
    

    这个函数定义在一个 Facelet taglib 文件中WEB-INF/facelets/include.taglib.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE facelet-taglib PUBLIC
      "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN"
      "http://java.sun.com/dtd/facelet-taglib_1_0.dtd">
    <facelet-taglib xmlns="http://java.sun.com/JSF/Facelet">
      <namespace>http://demo</namespace>
      <function>
        <function-name>include</function-name>
        <function-class>inc.Includer</function-class>
        <function-signature>
          java.lang.String include(java.lang.String)
        </function-signature>
      </function>
    </facelet-taglib>
    

    这在WEB-INF/web.xml中被指定为一个库using a context parameter:

      <context-param>
        <param-name>facelets.LIBRARIES</param-name>
        <param-value>/WEB-INF/facelets/include.taglib.xml</param-value>
      </context-param>
    

    使用示例

    要包含的JSP,includeme.jsp:

    <?xml version="1.0" encoding="UTF-8" ?>
    <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0">
      <jsp:directive.page language="java"
        contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" />
      <b>Some data: ${foo}</b>
    </jsp:root>
    

    包含 JSP 的 Facelet:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:demo="http://demo">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>JSP include hack</title>
    </head>
    <body>
    <h:form>
      <p> ${demo:include('/includeme.jsp')} </p>
      <h:inputText type="text" value="#{foo}" />
      <h:commandButton type="submit" />
    </h:form>
    </body>
    </html>
    

    注意使用 ${demo:include('/includeme.jsp')} 来调用请求调度程序(该函数返回一个空字符串)。该函数包含在属性 xmlns:demo="http://demo" 中。请求范围变量 foo 绑定到文本字段并由 JSP 拾取。


    对此我只能说它对我有用,并且可能有十几个原因导致它不能与特定的标签组合或特定的 JSF 库组合一起使用。警告购买者。

    【讨论】:

    • 感谢您的回答。看起来我只需要接受打击并创建 JSF 作为组件。
    • 当我尝试在 Icefaces 中获取 requestDispatcher 时,我得到一个 Unsupported 异常....有没有其他方法可以得到它
    猜你喜欢
    • 2016-09-12
    • 2012-09-23
    • 1970-01-01
    • 1970-01-01
    • 2013-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-14
    相关资源
    最近更新 更多