3

编辑:重现此问题所需的唯一技术是JSF 2.2Spring Boot 1.2.1+ 它的嵌入式Tomcat 8.0.5服务器。这个问题中列出的所有其他内容只是为了提供我正在使用的技术的背景信息。

更新 #2:按照 BalusC 的想法,我将示例自定义组件移植到准系统Servlet 3.1+JSF 2.2应用程序中。你可以在 Github 上找到它的代码

这个简单的案例没有表现出我在这里描述的问题。@FacesComponent注释有效。这在很大程度上意味着问题是由其本身引起Spring 4.1.2Spring Boot。时间不早了,明天我会进一步调查。

TL;DR:我想使用@FacesComponent它的属性来替换foundation-components-html.taglib.xml<component>输入faces-config.xml

我目前使用 XML 定义在我的项目中使用自定义组件。我最近了解到 JSF 2.2 引入了一个完全不需要 XML的特性。我很想使用它,但是当我纯粹使用注释时,JSF 会忽略它们。原始标签显示在我的 HTML 中。

(即<custom:paragraph></custom:paragraph>

我已经在我一直托管在 Github 上的沙箱中演示了这个问题。如果你想对此有所了解,我将在这篇文章的底部解释如何。

您需要做的就是删除foundation-components-html.taglib.xml,并注释掉 > 的faces-config.xml条目<component并运行应用程序以遇到问题。我将其置于“运行”状态,以便任何希望提供帮助的人都有一个简单、可验证的正确起点。只需点击http://localhost:8080

使用的技术:

  • 春季启动 1.2.1

  • JSF 2.2 通过 Mojarra 2.2.6

  • 嵌入式Tomcat 8.0.5

注意:请记住,此设置当前有效,但它在 taglib 和 faces-config 条目上运行!我的问题是如何使用中的最新功能删除这些依赖项JSF 2.2

完整项目

自定义组件

package foundation.components;

import java.io.IOException;

import javax.faces.component.FacesComponent;

import javax.faces.component.UIComponentBase;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

/**
 * The Paragraph Component
 * @author Seth Ellison
 */
@FacesComponent(value=UIParagraph.COMPONENT_TYPE, createTag=true, tagName="paragraph", namespace="http://www.blah.com/components/html")
public class UIParagraph extends UIComponentBase {

public static final String COMPONENT_TYPE = "foundation.components.Paragraph";

private String value;
private String styleClass;

@Override
public void encodeBegin(final FacesContext facesContext) throws IOException {

    // Encode Implementation Omitted for Brevity. 
}

@Override
public String getFamily() {        
    return "blah.components.family";
}

// Getters/Setters...

}

标签库定义

<facelet-taglib version="2.2"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facelettaglibrary_2_2.xsd">

    <namespace>http://www.blah.com/components/html</namespace>

    <tag>
        <tag-name>paragraph</tag-name>
        <component>
            <component-type>foundation.components.Paragraph</component-type>
        </component>
    </tag>
</facelet-taglib>

人脸配置

<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"             
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
        version="2.2" metadata-complete="false">

    <component>
        <component-type>foundation.components.Paragraph</component-type>
        <component-class>foundation.components.UIParagraph</component-class>
    </component>  
</faces-config>

XHTML 模板(为清楚起见,已精简)

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:jsf="http://xmlns.jcp.org/jsf"
  xmlns:custom="http://www.blah.com/components/html">

    <head jsf:id="head"></head>
    <body jsf:id="body">
       <custom:paragraph value="This is a template for a simple marketing or informational website. It includes a large callout called a jumbotron and three supporting pieces of content. Use it as a starting point to create something more unique." />
    </body>
</html>

如果您想运行它,最简单的方法是下载 Spring Tool Suite,从 Github 获取代码,右键单击该项目,然后将其作为 Spring Boot App 运行。当 JPA 配置启动时,您将收到连接错误,因为您(可能)没有运行本地 MySQL 服务器。不要担心这个。完全不需要访问索引页面并查看标签状态。我经常在使用和不使用数据库的情况下运行该应用程序,但不会产生不良影响。最后,为了让 PrettyFaces 与 Spring Boot 配合得很好,您必须从目标/类创建一个符号链接或硬链接到 WEB-INF/——PrettyFaces 被编码为在 WEB-INF/classes 或 WEB-INF 中查看/lib 扫描注释时。

BalusC 的片段

这个函数存在于一个被标记@Configuration并实现的类中ServletContextAware

@Bean
public ServletListenerRegistrationBean<ConfigureListener> jsfConfigureListener() {
    return new ServletListenerRegistrationBean<ConfigureListener>(
            new ConfigureListener());
}
4

1 回答 1

3

好吧,我知道是什么导致了这个问题。

今天早上我坐下来思考我Servlet 3.1的代码工作版本和损坏Spring Boot版本之间的差异。主要区别在于代码的运行方式。嵌入式服务器与独立服务器。

Spring Boot的嵌入式 Tomcat服务器是原因。

当我按照这个答案切换我的沙箱时,一切正常,我的自定义组件完全脱离了@FacesComponent注释!

我认为这与嵌入式服务器上启动后类的组织方式与对 Pivotal Tomcat 服务器的离散部署有关。在这种情况下,JSF 的注解扫描器似乎只是简单地忽略了注解。

于 2015-02-18T17:03:57.490 回答