【问题标题】:Tapestry generates input tags in pairTapestry 成对生成输入标签
【发布时间】:2013-05-12 15:45:00
【问题描述】:

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

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

Tapestry 生成 html 输入元素:

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

但是元素输入成对无效(带有结束标签&lt;/input&gt;)并且html验证器大喊:“错误:杂散的结束标签输入。”。

有什么方法可以生成单一形式的输入标签,比如 &lt;input .../&gt;?

【问题讨论】:

  • 您使用什么版本的 Tapestry?我使用最新的(5.3.7)并得到相同的结果,但没有结束标记。
  • 我的 Tapestry 版本是 5.3.6。由 Nathan 发布的错误已在 5.4 版本中修复(以及 5.3.6 中的受影响版本)。所以显然我需要增加我的 Tapestry 版本。谢谢你们的回答!
  • 我认为 5.4 还没有发布。不过最近有5.3.7发布了,貌似那个版本已经修复了。
  • 是的,我想你是对的。我打算试试5.3.7版本。谢谢

标签: java forms textfield tapestry


【解决方案1】:

您可以使用自己的 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));
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-19
    • 2018-03-23
    • 1970-01-01
    • 2013-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多