我正在将代码从JSF 1.2迁移到JSF 2.0(部署在JBoss 6.1上)。但是,我在h:outputText
标记的href
属性中使用link
时遇到问题:
<link rel='canonical' href='<h:outputText value="#{resultPage.currentLink}" escape="false"/>' />
我不能只使用
<link rel='canonical' href="#{resultPage.currentLink}" />
因为我需要html转义属性。
我收到以下例外:
javax.servlet.ServletException: Error Parsing /shoe/shoe2.xhtml: Error Traced[line: 23] The value of attribute "href" associated with an element type "link" must not contain the '<' character.
javax.faces.webapp.FacesServlet.service(FacesServlet.java:321)
com.youandshoe.web.controller.EncoderFilter.doFilter(EncoderFilter.java:28)
com.ocpsoft.pretty.PrettyFilter.doFilter(PrettyFilter.java:126)
com.ocpsoft.pretty.PrettyFilter.doFilter(PrettyFilter.java:118)
root cause
javax.faces.view.facelets.FaceletException: Error Parsing /shoe/shoe2.xhtml: Error Traced[line: 23] The value of attribute "href" associated with an element type "link" must not contain the '<' character.
com.sun.faces.facelets.compiler.SAXCompiler.doCompile(SAXCompiler.java:390)
com.sun.faces.facelets.compiler.SAXCompiler.doMetadataCompile(SAXCompiler.java:373)
com.sun.faces.facelets.compiler.Compiler.metadataCompile(Compiler.java:128)
com.sun.faces.facelets.impl.DefaultFaceletFactory.createMetadataFacelet(DefaultFaceletFactory.java:316)
com.sun.faces.facelets.impl.DefaultFaceletFactory.access$200(DefaultFaceletFactory.java:89)
com.sun.faces.facelets.impl.DefaultFaceletFactory$2.newInstance(DefaultFaceletFactory.java:165)
com.sun.faces.facelets.impl.DefaultFaceletFactory$2.newInstance(DefaultFaceletFactory.java:164)
com.sun.faces.facelets.impl.DefaultFaceletCache$2.newInstance(DefaultFaceletCache.java:90)
com.sun.faces.facelets.impl.DefaultFaceletCache$2.newInstance(DefaultFaceletCache.java:86)
com.sun.faces.util.ExpiringConcurrentCache$1.call(ExpiringConcurrentCache.java:95)
java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
java.util.concurrent.FutureTask.run(Unknown Source)
com.sun.faces.util.ExpiringConcurrentCache.get(ExpiringConcurrentCache.java:110)
com.sun.faces.facelets.impl.DefaultFaceletCache.getMetadataFacelet(DefaultFaceletCache.java:131)
com.sun.faces.facelets.impl.DefaultFaceletCache.getMetadataFacelet(DefaultFaceletCache.java:58)
com.sun.faces.facelets.impl.DefaultFaceletFactory.getMetadataFacelet(DefaultFaceletFactory.java:249)
com.sun.faces.facelets.impl.DefaultFaceletFactory.getMetadataFacelet(DefaultFaceletFactory.java:198)
com.sun.faces.application.view.ViewMetadataImpl.createMetadataView(ViewMetadataImpl.java:102)
com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:223)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:107)
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
org.apache.myfaces.extensions.cdi.jsf2.impl.listener.phase.CodiLifecycleWrapper.execute(CodiLifecycleWrapper.java:97)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)
com.youandshoe.web.controller.EncoderFilter.doFilter(EncoderFilter.java:28)
com.ocpsoft.pretty.PrettyFilter.doFilter(PrettyFilter.java:126)
com.ocpsoft.pretty.PrettyFilter.doFilter(PrettyFilter.java:118)
但是,相同的代码在JSF 1.2,JBoss 5.1中运行良好。
有什么想法吗?感谢名单!
答案 0 :(得分:2)
Facelets是一种基于XML的视图技术。 XML文档必须格式正确。嵌套XML标记无效。要解决您的问题,您需要将整个HTML包含在<h:outputText>
内。
<h:outputText value="<link rel="canonical" href="#{resultPage.currentLink}" />" escape="false" />
顺便说一句,我真的很想知道为什么你不能只使用
<link rel="canonical" href="#{resultPage.currentLink}" />
这应该同样合适。在href
属性中使用XML特殊字符否则会导致浏览器端无效的XML标记错误,并且浏览器将无法下载资源。