devdar
devdar

Reputation: 5654

SpringMVC Custom Form Tags

Is it possible to take the HTML 5 tag and create a spring form tag like <form:canvas path="" id="" title=""/> just like <form:input path="" id="" title=""/>. How can this be achieved, would i be required to add to my copy of the spring-form.tld?

I am confused as to how i can create this can someone explain this. I would like to do this so i can benefit from spring binding to form elements.

Upvotes: 0

Views: 3061

Answers (1)

abishkar bhattarai
abishkar bhattarai

Reputation: 7641

For spring custom tag see Create a custom tag library which extends the Spring tag library As per yours question following is the sequence how spring FormTag works

1.doStartTag() method of RequestContextAwareTag class is called on first.

2.doStartTagInternal() method of AbstractFormTag class is called on second.

3.writeTagContent(TagWriter tagwriter) method of FormTag is called on third.

Now let us follow this sequence of call and extend FormTag class. The call sequence will be same for 1 and 2 .But in 3 when CustomFormTag extends FormTag so this time writeTagContent of CustomFormTag will be called.

So our code will be

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.

on call to code super.writeTagContent(tagWriter);
it calls method writeTagContent of FormTag class.

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;
    }

So you cannot change form to canvas within spring mvc which extends spring tag library. .You can write custom tag which does not extends spring tag library

Upvotes: 1

Related Questions