【问题标题】:How do I use html5 with jsf 2 and facelets?如何将 html5 与 jsf 2 和 facelets 一起使用?
【发布时间】:2013-06-17 09:26:00
【问题描述】:

我正在尝试在 Glassfish 3.1.2 服务器上使用 HTML5 文档类型和 jsf 2.1 和 Facelets。

我见过很多我们可以将 xhtml doctype 替换为 html5 doctype 的地方

 <!DOCTYPE html>

并将 xhtml 的 xmlns 保留在 -tag 中。在一些论坛上,他们说就是这样。但事实并非如此,至少 Firefox 不同意,因为它将页面呈现为“HTML 专有”而不是 HTML5。

似乎没有教程考虑 html 页面文件扩展名?这是浏览器关心的事情吗?我尝试将其从 *.xhtml 更改为 *.html,从 -tag 中删除 xhtml 命名空间,并在 web.xml 中更改

 <context-param>
   <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
   <param-value>.xhtml</param-value>
</context-param>

到

 <context-param>
   <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
   <param-value>.html</param-value>
</context-param>

并为 html 添加了 servlet 映射。这根本不起作用。

好的,然后我将所有内容恢复为默认值(xhtml/facelets),仅替换了 doctype-tag 并删除了引用 xhtml 的 xmlns(从所有文件/模板中)。现在我收到很多警告,例如:

 Warning: This page calls for XML namespace declared with prefix li but no taglibrary   exists for that namespace.
 Warning: This page calls for XML namespace declared with prefix li but no taglibrary exists for that namespace.
 Warning: This page calls for XML namespace declared with prefix li but no taglibrary exists for that namespace.
 Warning: This page calls for XML namespace declared with prefix body but no taglibrary exists for that namespace.
 Warning: This page calls for XML namespace declared with prefix ul but no taglibrary exists for that namespace.
 Warning: This page calls for XML namespace declared with prefix form but no taglibrary exists for that namespace.
 Warning: This page calls for XML namespace declared with prefix a but no taglibrary exists for that namespace.
 Warning: This page calls for XML namespace declared with prefix a but no taglibrary exists for that namespace.
 Warning: This page calls for XML namespace declared with prefix a but no taglibrary exists for that namespace.
 Warning: This page calls for XML namespace declared with prefix a but no taglibrary exists for that namespace.

好吧,这看起来不太乐观。在 web.xml 中,我将项目阶段从开发更改为生产。

 <context-param>
     <param-name>javax.faces.PROJECT_STAGE</param-name>
     <param-value>Production</param-value>
 </context-param>

警告消失了,w3c 的验证器说这是有效的 html5。

有人对此有更好的解决方案吗? jsf/facelets 是否要求文件具有扩展名 xhtml?

【问题讨论】:

    标签: html jsf jsf-2 facelets


    【解决方案1】:

    警告:此页面要求使用前缀 (...) 声明的 XML 命名空间,但该命名空间不存在标记库。

    您需要将定义 HTML 元素的 XML 命名空间声明为全局 XML 命名空间。

    也就是说,

    <html xmlns="http://www.w3.org/1999/xhtml">
    

    【讨论】:

    • 嗯,如果给xhtml加上xml命名空间,真的是html5吗?如果您阅读我的问题,您会发现我已经尝试过您提出的解决方案,但 Firefox 将页面呈现为“HTML 专有”而不是 HTML5。
    • 再次尝试为 xhtml 添加 xmlns,现在 Firefox 和 W3C 都显示有效的 html5。你做了什么? :D 无论如何,根据这个diveintohtml5.info/semantics.html html5 always 在 xhtml 命名空间中,因此没有必要使用。 “但 HTML5 中的元素始终位于此命名空间中,因此您不再需要显式声明它。您的 HTML5 页面将在所有浏览器中完全相同,无论此属性是否存在。”并且 facelets 总是必须有 *.xhtml 作为文件扩展名..?
    • 这是为了让 Facelets 的 XML 解析器满意。不过,您可以随意忽略这些警告。
    【解决方案2】:

    就我而言,添加命名空间不是一个好的解决方案:

    <html xmlns="http://www.w3.org/1999/xhtml">
    

    上述内容确实让 Facelets 解析器满意,但命名空间被写入生成的 HTML,W3C 验证器没有正确地将其检测为 HTML5。

    不使用命名空间可以正常工作并且可以很好地验证,但是对于我的 .xhtml 文件中使用的每个纯 HTML 标记,上面当然会出现一个警告。

    为了摆脱这些警告,我不得不创建一个自定义FacesContext:

    public class Html5FacesContextImpl extends FacesContextWrapper {
    
        private final FacesContext wrapped;
    
        public Html5FacesContextImpl(FacesContext wrapped) {
            this.wrapped = wrapped;
        }
    
        @Override
        public FacesContext getWrapped() {
            return wrapped;
        }
    
    
        private final static String XML_NAMESPACE_MSG_FILTER = "Warning: This page calls for XML namespace";
    
        @Override
        public void addMessage(String clientId, FacesMessage message) {
            if ( !message.getSummary().startsWith(XML_NAMESPACE_MSG_FILTER) ) {
                wrapped.addMessage(clientId, message);
            }
        }
    
    
    }
    

    工厂:

    public class Html5FacesContextFactory extends FacesContextFactory {
    
        private FacesContextFactory delegate;
    
        public Html5FacesContextFactory(FacesContextFactory facesContextFactory) {
          delegate = facesContextFactory;
        }
    
        @Override
        public FacesContext getFacesContext(Object context, Object request, Object response, Lifecycle lifecycle) throws FacesException {
            return new Html5FacesContextImpl(delegate.getFacesContext(context, request, response, lifecycle));
        }
    
    }
    

    faces-config.xml:

    <factory>
        <faces-context-factory>ca.gc.agr.common.web.jsf.context.Html5FacesContextFactory</faces-context-factory>
    </factory>
    

    这似乎有效。

    【讨论】:

      猜你喜欢
      • 2010-09-18
      • 2016-10-30
      • 2015-02-22
      • 2013-03-12
      • 1970-01-01
      • 2011-07-25
      • 2011-03-24
      • 2010-12-28
      相关资源
      最近更新 更多