2013-01-19 46 views
0

是否有可能採用HTML 5標籤並創建像 <form:canvas path="" id="" title=""/>這樣的彈簧窗體標籤,就像<form:input path="" id="" title=""/>一樣。如何實現這一點,我是否需要添加到我的spring-form.tld副本中?SpringMVC自定義窗體標籤

我很困惑,我可以如何創建這個有人可以解釋這一點。我想這樣做,所以我可以從彈簧綁定中獲益,從而形成元素。

回答

1

因爲春天自定義標籤看到 Create a custom tag library which extends the Spring tag library 按照你的問題,以下是序列春天FormTag如何工作

.doStartTag()RequestContextAwareTag類的方法被稱爲先上。

AbstractFormTag類的.doStartTagInternal()方法在第二次被調用。

。第三次調用FormTag的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的範圍內改變形式的畫布。 。您可以編寫不擴展彈簧標籤庫的自定義標籤