我想使用ui:include在facelet中包含servlet生成的内容。到目前为止我只有错误。
请注意,servlet工作正常,浏览器的GET请求正常。
请注意,我可以成功地包含来自另一个facelet文件的内容,但是当内容是servlet生成时则不能。
我用谷歌搜索并堆栈溢出了我的问题,但找不到匹配的回复。
myfacelet.xhtml(位于webapp的根目录,即“http:// localhost:8080 / mywebapp / myfacelet.xhtml”):
<?xml version="1.0" encoding="UTF-8"?>
<!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:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<title>JSF Dynamic Content</title>
</h:head><h:body>
<h3>Dynamic content:</h3>
<ui:include src="dynamic-content"/>
</h:body>
</html>
MyServlet.java(可通过“http:// localhost:8080 / mywebapp / dynamic-content”访问):
@WebServlet(name = "DynamicContentServlet", urlPatterns = {"/dynamic-content"})
public class DynamicContentServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter writer = response.getWriter();
writer.println("<p>Dynamic stuff</p>");
}
}
web.xml(仅限JSF相关内容):
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<servlet>
<servlet-name>FacesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FacesServlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
<url-pattern>*.jsf</url-pattern>
<url-pattern>*.faces</url-pattern>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
对于ui:include =“/ dynamic-content”和urlPatterns =“/ dynamic-content” (或ui:include =“/ mywebapp / dynamic-content”和urlPatterns =“/ dynamic-content”) 错误是:
javax.faces.view.facelets.TagAttributeException: /myfacelet.xhtml @12,48 <ui:include src="/dynamic-content"> Invalid path : /dynamic-content
at com.sun.faces.facelets.tag.ui.IncludeHandler.apply(IncludeHandler.java:152)
at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:98)
at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:195)
at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:98)
at com.sun.faces.facelets.compiler.NamespaceHandler.apply(NamespaceHandler.java:93)
at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:98)
at com.sun.faces.facelets.compiler.EncodingHandler.apply(EncodingHandler.java:86)
at com.sun.faces.facelets.impl.DefaultFacelet.apply(DefaultFacelet.java:152)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.buildView(FaceletViewHandlingStrategy.java:774)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:100)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
(...)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930)
at java.lang.Thread.run(Thread.java:662)
对于ui:include =“dynamic-content”和urlPatterns =“/ dynamic-content” 或者ui:include =“dynamic-content.bla”和urlPatterns =“*。bla”, 错误是:
java.io.FileNotFoundException
at org.apache.naming.resources.DirContextURLConnection.getInputStream(DirContextURLConnection.java:369)
at com.sun.faces.facelets.impl.DefaultFaceletCache._getLastModified(DefaultFaceletCache.java:172)
at com.sun.faces.facelets.impl.DefaultFaceletCache.access$000(DefaultFaceletCache.java:62)
at com.sun.faces.facelets.impl.DefaultFaceletCache$1.newInstance(DefaultFaceletCache.java:82)
at com.sun.faces.facelets.impl.DefaultFaceletCache$1.newInstance(DefaultFaceletCache.java:78)
at com.sun.faces.util.ExpiringConcurrentCache$1.call(ExpiringConcurrentCache.java:99)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at com.sun.faces.util.ExpiringConcurrentCache.get(ExpiringConcurrentCache.java:114)
at com.sun.faces.facelets.impl.DefaultFaceletCache.getFacelet(DefaultFaceletCache.java:121)
at com.sun.faces.facelets.impl.DefaultFaceletCache.getFacelet(DefaultFaceletCache.java:62)
at com.sun.faces.facelets.impl.DefaultFaceletFactory.getFacelet(DefaultFaceletFactory.java:256)
at com.sun.faces.facelets.impl.DefaultFacelet.include(DefaultFacelet.java:366)
at com.sun.faces.facelets.impl.DefaultFacelet.include(DefaultFacelet.java:346)
at com.sun.faces.facelets.impl.DefaultFaceletContext.includeFacelet(DefaultFaceletContext.java:199)
at com.sun.faces.facelets.tag.ui.IncludeHandler.apply(IncludeHandler.java:147)
at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:98)
at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:195)
at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:98)
at com.sun.faces.facelets.compiler.NamespaceHandler.apply(NamespaceHandler.java:93)
at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:98)
at com.sun.faces.facelets.compiler.EncodingHandler.apply(EncodingHandler.java:86)
at com.sun.faces.facelets.impl.DefaultFacelet.apply(DefaultFacelet.java:152)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.buildView(FaceletViewHandlingStrategy.java:774)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:100)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
(...)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930)
at java.lang.Thread.run(Thread.java:662)
另请注意,servlet永远不会被执行。
我的代码出了什么问题?
包含是否可能?
我怎样才能实现它?
祝你好运
答案 0 :(得分:2)
<ui:include>
只能包含另一个Facelets文件,而不能包含JSP / Servlet的输出。
您需要通过一个值得一提的Facelets文件替换Servlet,或使用Facelets标记文件或Facelets复合组件,其中HTML也可以使用JSTL标记或rendered
属性动态生成,如果需要,可以借助支持豆或背衬组件。
如果您出于某种原因绝对需要使用Servlet,例如,因为您正处于从旧版JSP / Servlet到JSF / Facelets的迁移过程中,并且没有太多时间可以将Servlet正确转换为理智的Facelets方法,然后您还可以考虑使用<o:resourceInclude>
的OmniFaces组件:
<o:resourceInclude path="/dynamic-content" />
但如上所述,这不是正确的设计方法。对于正确的长期解决方案,您应该考虑用Facelets文件/标签/复合替换Servlet。
答案 1 :(得分:0)
我也正在开发一个必须执行动态页面生成的应用,包括嵌入式图片,<map>
和<area>
标记以及相关的<div>
标记,这些标记在翻转时突出显示。
我天真的第一个测试方法是从StringBuffer中写出整个html:response.getWriter().println(sb.toString());
。这有效,但它是一个黑客,并没有利用我的JSF模板。即使我写出整个<ui:composition>
元素,它也不会使用模板。
在Java EE 6教程中,有关于如何创建自定义组件的详细信息。见这里:http://docs.oracle.com/javaee/6/tutorial/doc/glpcb.html
这似乎支持BalusC上面的评论, “...使用Facelets标记文件或Facelets复合组件,其中HTML也可以使用JSTL标记或渲染属性动态生成,必要时可以借助辅助bean或支持组件。”
您的里程可能会有所不同!
答案 2 :(得分:0)
自问题发布以来已经有一段时间了,但是有人可能会发现它很有用,如果我做了一些愚蠢的事情,我欢迎您的指正! 我遇到了同样的问题,我通过javascript进行了锻炼来解决了这个问题,我使用的线索来自此链接https://www.w3schools.com/howto/howto_html_include.asp,我的servlet是动态生成地图标记,并且它是在(web.xml)文件中设置的使用servlet过滤器(轮廓),因此我使用上述链接中的javascript在xhtml中应用了链接(注意:rtutil.appUrl返回我的JSF应用程序的上下文路径),我将其应用如下:
<div w3-include-html="#{rtutil.appUrl}/contours/map"></div>
<script type="text/javascript" src="resources/js/rtscript.js"></script>
<script type="text/javascript">
includeHTML();
$(document).ready(function () {
cityplanmap();
});
</script>