3

我想生成 HTML5 有效文档,但我的 Tapestry 应用程序中的表单有问题。我正在使用如下挂毯文本字段:

<t:textfield t:id="specId" value="val" />

Tapestry 生成 html 输入元素:

<input id="specId" name="specId" type="text"></input>

但是元素输入成对无效(带有结束标签</input>)并且html验证器大喊:“错误:流浪结束标签输入。”。

有什么方法可以生成单一形式的输入标签 <input .../>吗?

4

1 回答 1

0

您可以使用自己的 MarkupModel 覆盖 MarkupWriterFactory 服务,该服务将缩写 html5 void 元素而不是呈现结束标记。

public class Html5MarkupModel extends AbstractMarkupModel {
    private static final Set<String> VOID_ELEMENTS = new HashSet<String>(Arrays.asList(
            "area", "base", "br", "col", "command", "embed", "hr", "img", "input", "keygen", "link", "meta", "param", "source", "track", "wbr"
    ));

    public Html5MarkupModel(boolean useApostropheForAttributes) {
        super(useApostropheForAttributes);
    }

    public EndTagStyle getEndTagStyle(String element) {
        return VOID_ELEMENTS.contains(element) ? EndTagStyle.ABBREVIATE : EndTagStyle.REQUIRE;
    }

    public boolean isXML() {
        return false;
    }
}

public class Html5MarkupWriterFactory implements MarkupWriterFactory {

    private final PageContentTypeAnalyzer analyzer;
    private final RequestPageCache cache;

    private final MarkupModel htmlModel = new Html5MarkupModel(false);
    private final MarkupModel htmlPartialModel = new Html5MarkupModel(true);
    private final MarkupModel xmlModel = new XMLMarkupModel();
    private final MarkupModel xmlPartialModel = new XMLMarkupModel(true);

    public Html5MarkupWriterFactory(PageContentTypeAnalyzer analyzer, RequestPageCache cache) {
        this.analyzer = analyzer;
        this.cache = cache;
    }

    public MarkupWriter newMarkupWriter(ContentType contentType) {
        return newMarkupWriter(contentType, false);
    }

    public MarkupWriter newPartialMarkupWriter(ContentType contentType) {
        return newMarkupWriter(contentType, true);
    }

    public MarkupWriter newMarkupWriter(String pageName) {
        return newMarkupWriter(analyzer.findContentType(cache.get(pageName)));
    }

    private MarkupWriter newMarkupWriter(ContentType contentType, boolean partial) {
        boolean isHTML = contentType.getMimeType().equalsIgnoreCase("text/html");

        MarkupModel model = partial
                ? (isHTML ? htmlPartialModel : xmlPartialModel)
                : (isHTML ? htmlModel : xmlModel);

        // The charset parameter sets the encoding attribute of the XML declaration, if
        // not null and if using the XML model.

        return new MarkupWriterImpl(model, contentType.getCharset());
    }
}

和服务覆盖贡献:

@Contribute(ServiceOverride.class)
public void contributeServiceOverrides(MappedConfiguration<Class, Object> configuration,
                                       ObjectLocator objectLocator) {
    // use proxy instead of real service instance
    // to prevent recursion on initialization cycle
    configuration.add(MarkupWriterFactory.class,
            objectLocator.proxy(MarkupWriterFactory.class, Html5MarkupWriterFactory.class));
}
于 2013-05-24T07:35:58.467 回答