如何将html5与jsf 2和facelets一起使用?

时间:2013-06-17 09:26:00

标签: html5 jsf jsf-2 facelets

我正在尝试在Glassfish 3.1.2服务器上使用带有jsf 2.1和Facelets的HTML5 doctype。

我见过很多地方我们可以用html5 doctype替换xhtml doctype

 <!DOCTYPE html>

并在-tag中留下xhtml的xml。在一些论坛中,他们说就是这样。但事实并非如此,至少Firefox不同意,因为它将页面呈现为“HTML propritary”而不是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?

2 个答案:

答案 0 :(得分:2)

  

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

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

即,

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

答案 1 :(得分:0)

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

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

以上肯定会使Facelets解析器满意,但命名空间会被写入生成的HTML中,而W3C Validator无法将其正确检测为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>

这似乎有效。