SpringMVC自定义表单标签

时间:2013-01-19 14:44:41

标签: spring-mvc tags jsp-tags

是否可以使用HTML 5标记并创建一个弹簧形式标记 <form:canvas path="" id="" title=""/>就像<form:input path="" id="" title=""/>一样。如何实现这一点,我是否需要添加到spring-form.tld的副本中?

我很困惑,我怎么能创造这个可以有人解释这个。我想这样做,所以我可以受益于弹簧绑定形成元素。

1 个答案:

答案 0 :(得分:1)

对于春季自定义标签,请参阅 Create a custom tag library which extends the Spring tag library 根据你的问题,以下是Spring FormTag如何工作的顺序

首先调用RequestContextAwareTag类的

1 .doStartTag()方法。

2 。第二次调用AbstractFormTag类的.doStartTagInternal()方法。

第三次调用FormTag的

3 .writeTagContent(TagWriter tagwriter)方法。

现在让我们按照这个调用顺序来扩展FormTag类。 对于1和2,调用序列将是相同的。但是当CustomFormTag扩展FormTag时,调用序列将是3,所以这次将调用CustomFormTag的writeTagContent。

所以我们的代码将是

public class CustomFormTag extends FormTag
{

    public CustomFormTag ()
    {
    }

    protected int writeTagContent(TagWriter tagWriter)
        throws JspException
    {
        int result = super.writeTagContent(tagWriter);  

        writeOptionalAttribute(tagWriter, "testattribute", getTestAttribute());


        return result;
    }
//getter and setter for testattribute.

调用代码super.writeTagContent(tagWriter);
它调用FormTag类的方法writeTagContent。

protected int writeTagContent(TagWriter tagWriter)
        throws JspException
    {
        this.tagWriter = tagWriter;
        tagWriter.startTag("form");  // form tag is here so we can not change it with canvas
        writeDefaultAttributes(tagWriter);
        tagWriter.writeAttribute("action", resolveAction());
        writeOptionalAttribute(tagWriter, "method", getMethod());
        writeOptionalAttribute(tagWriter, "target", getTarget());
        writeOptionalAttribute(tagWriter, "enctype", getEnctype());
        writeOptionalAttribute(tagWriter, "accept-charset", getAcceptCharset());
        writeOptionalAttribute(tagWriter, "onsubmit", getOnsubmit());
        writeOptionalAttribute(tagWriter, "onreset", getOnreset());
        writeOptionalAttribute(tagWriter, "autocomplete", getAutocomplete());
        tagWriter.forceBlock();
        String modelAttribute = resolveModelAttribute();
        pageContext.setAttribute(MODEL_ATTRIBUTE_VARIABLE_NAME, modelAttribute, 2);
        pageContext.setAttribute(COMMAND_NAME_VARIABLE_NAME, modelAttribute, 2);
        previousNestedPath = (String)pageContext.getAttribute("nestedPath", 2);
        pageContext.setAttribute("nestedPath", modelAttribute + ".", 2);
        return 1;
    }

所以你不能在spring mvc中改变形式到canvas,它扩展了spring标签库。你可以编写自定义标签,不扩展弹簧标签库