简体   繁体   English

将JSP标记转换为JSF组件

[英]Converting A JSP Tag To A JSF Component

I've got an JSP tag that I would like to use in my JSF/Seam application. 我有一个JSP标记,我想在我的JSF / Seam应用程序中使用。 Could someone please give me some guidance in getting my environment to accept the tag. 有人可以给我一些指导,让我的环境接受标签。 Can I just point a faclets *.taglib.xml file at my old tag or do I need to write a component extending the previous tag perhaps? 我可以在我的旧标签上指定一个faclets * .taglib.xml文件,或者我是否需要编写一个扩展前一个标签的组件?

Cheers for any info, Lee 为任何信息干杯,李

I would be very reluctant to try and directly invoke a JSP tag outside a JSP context. 我非常不愿意尝试直接调用JSP上下文之外的JSP标记。 As the documentation points out , the similarities between JSP and Facelets are pretty superficial. 正如文档所指出的那样 ,JSP和Facelets之间的相似之处非常肤浅。

One hack (I suspect that any solution is going to be a hack) might be to include the JSP by casting down to the servlet API. 一个hack(我怀疑任何解决方案都将成为一个hack)可能是通过向下转换到servlet API来包含JSP。

This function includes a given resource using a RequestDispatcher : 此函数包含使用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);
    }
  }

}

This function is defined in a Facelet taglib file WEB-INF/facelets/include.taglib.xml : 此函数在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>

This is specified as a library in the WEB-INF/web.xml using a context parameter : 使用context参数将其指定为WEB-INF / web.xml中的库:

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

Example usage 用法示例

JSP to be included, includeme.jsp : 要包含的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>

Facelet that includes the JSP: 包含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>

Note the use of ${demo:include('/includeme.jsp')} to invoke the request dispatcher (the function returns an empty String). 注意使用$ {demo:include('/ includeme.jsp')}来调用请求调度程序(该函数返回一个空字符串)。 The function is included by the attribute xmlns:demo="http://demo" . 该函数包含在属性xmlns:demo =“http:// demo”中 The request scope variable foo is bound to the text field and picked up by the JSP. 请求范围变量foo绑定到文本字段并由JSP拾取。


All I can say about this is that it worked for me and there are probably a dozen reasons why it wouldn't work with a certain combination of tags, or with certain combinations of JSF libraries. 我所能说的就是它对我有用,并且可能有十几个原因导致它不适用于某些标签组合或JSF库的某些组合。 Caveat emptor. 买者自负。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM