如何在 Facelets 页面中包含 JSP 页面? [英] How to include a JSP page in a Facelets page?
问题描述
我在 Facelets 上使用 Myfaces 2.我必须在 Facelet 页面中包含一个 JSP 页面.我尝试使用 <ui:include>
但它只需要 XHTML 页面.我也尝试使用 <c:import>
和 <f:subview>
但没有任何效果.谷歌了很多,但没有得到确切的答案.
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.
我怎样才能达到要求?
推荐答案
这可以通过自定义 UIComponent
实现.我的同事在一年前写了一篇关于这个的博客文章:Facelets and legacy JSP.
That's possible with a custom UIComponent
. My fellow colleague has written a blog article about this a year ago: Facelets and legacy JSP.
是一些代码,但原理很简单,组件做了一个RequestDispatcher#include()
与自定义 HttpServletResponseWrapper
捕获写入的输出,然后将其写入JSF 组件.以下是相关性摘录:
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:
创建类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();
}
}
}
<小时>
创建类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);
}
}
<小时>
在faces-config.xml中添加
<component>
<component-type>com.example.component.JSPIncludeComponent</component-type>
<component-class>com.example.component.JSPIncludeComponent</component-class>
</component>
<小时>
在WEB-INF文件夹中创建文件my.taglib.xml
(Facelet taglib)
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>
<小时>
添加到 web.xml
(如 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
我不建议将其用作持久的解决方案,但它可能会简化从带有臭名昭著的 scriptlet 的旧 JSP 迁移到更健全和现代的 Facelets 应用程序.
这篇关于如何在 Facelets 页面中包含 JSP 页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!