作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 Facelets 上使用 Myfaces 2。
我必须在 Facelet 页面中包含一个 JSP 页面。
我尝试使用 <ui:include>
但它只需要 XHTML 页面。
我也尝试使用 <c:import>
和 <f:subview>
但没有任何效果。
谷歌搜索了很多,但没有得到确切的答案。
我怎样才能达到要求?
最佳答案
这可以通过自定义 UIComponent
实现.我的同事在一年前写了一篇关于此的博客文章:Facelets and legacy JSP .
是一些代码,但是原理很简单,组件做了一个 RequestDispatcher#include()
带定制 HttpServletResponseWrapper
它捕获写入的输出,然后将其写入 JSF 组件的主体。以下是相关摘录:
创建类(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();
}
}
}
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>
my.taglib.xml
(Facelet taglib) 在 WEB-INF 文件夹中
<!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>
<ui:component
xmlns:my="http://example.com/jsf"
xmlns:ui="http://java.sun.com/jsf/facelets"
>
<my:include page="page.jsp" />
</ui:composition>
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.
关于jsp - 如何在 Facelets 页面中包含 JSP 页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6106703/
我是一名优秀的程序员,十分优秀!