Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
929 views
in Technique[技术] by (71.8m points)

jsf - How to include a JSP page in a Facelets page?

I am using Myfaces 2 on Facelets. I have to include a JSP page in a Facelet page. I tried using <ui:include> but it takes only XHTML pages. I also tried using <c:import> and <f:subview> but nothing worked. Googled a lot but didn't get the exact answer.

How can I achieve the requirement?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

That's possible with a custom UIComponent. My fellow colleague has written a blog article about this a year ago: Facelets and legacy JSP.

It's some code, but the principle is easy, the component does a RequestDispatcher#include() with a custom HttpServletResponseWrapper which captures the written output and then writes it to the body of the JSF component. Here are extracts of relevance:

Create class com.example.component.JspIncludeComponent

public class JSPIncludeComponent extends UIComponentBase {

    public String getFamily() {
       return "components.jsp.include";
    }

    public void encodeBegin(FacesContext context) throws IOException {
       try {
          ExternalContext externalContext = context.getExternalContext();
          HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
          HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();

          // Create dispatcher for the resource given by the componen's page attribute.
          RequestDispatcher requestDispatcher = request.getRequestDispatcher((String) getAttributes().get("page"));

          // Catch the resource's output.
          CharResponseWrapper responseWrapper = new CharResponseWrapper(response);
          requestDispatcher.include(request, responseWrapper);

          // Write the output from the resource to the JSF response writer.
          context.getResponseWriter().write(responseWrapper.toString());
       }
       catch (ServletException e) {
          throw new IOException();
       }
    }

}

Create class com.example.CharResponseWrapper

public class CharResponseWrapper extends HttpServletResponseWrapper {

   private CharArrayWriter output;

   @Override
   public String toString() {
      return output.toString();
   }

   public CharResponseWrapper(HttpServletResponse response) {
      super(response);
      output = new CharArrayWriter();
   }

   public CharArrayWriter getCharWriter() {
      return output;
   }

   @Override
   public PrintWriter getWriter() {
       return new PrintWriter(output);
  }

   @Override
   public ServletOutputStream getOutputStream() {
      return new CharOutputStream(output);
   }

   public InputStream getInputStream() {
      return new ByteArrayInputStream( toString().getBytes() );
   }
}

class CharOutputStream extends ServletOutputStream {

   private Writer output;

   public CharOutputStream( Writer writer ) {
      output = writer;
   }

   @Override
   public void write(int b) throws IOException {
      output.write(b);
   }
}

Add in faces-config.xml

<component>
    <component-type>com.example.component.JSPIncludeComponent</component-type>
    <component-class>com.example.component.JSPIncludeComponent</component-class>
</component>

Create file my.taglib.xml (Facelet taglib) in the WEB-INF folder

<!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>
    <namespace>http://example.com/jsf</namespace> 

    <tag>
      <tag-name>include</tag-name>
      <component>
        <component-type>com.example.component.JSPIncludeComponent</component-type>
      </component>
    </tag>

</facelet-taglib>

Add to web.xml (as explained in http://docs.oracle.com/javaee/6/tutorial/doc/bnawn.html)

<context-param>
    <param-name>javax.faces.FACELETS_LIBRARIES</param-name>
    <param-value>/WEB-INF/my.taglib.xml</param-value>
</context-param>

This way you can use it as

<ui:component 
    xmlns:my="http://example.com/jsf"
    xmlns:ui="http://java.sun.com/jsf/facelets"
>
    <my:include page="page.jsp" />
</ui:composition>

Last but not least, mark his last words

I wouldn’t recommend using this as a lasting solution, but it might ease a migration from legacy JSP with smelly scriptlets and all on them to a more sane and modern Facelets application.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...