Spring form tag library attribute extension

I am using the Spring form tag library in the Spring MVC application that I am developing. The company I work for has implemented some company policies based on defining custom attributes for specific tags. For example, by default (although including a standard javascript file), all tags are automatically converted to uppercase. To disable this, you can define your tag using a custom attribute as follows:

<input type="text" uppercase="false" /> 

The problem is that adding these custom attributes to the spring: form tag causes a runtime error. I inserted an error below.

 org.apache.jasper.JasperException: /WEB-INF/jsp/reportCriteria.jsp(45,5) Attribute uppercase invalid for tag input according to TLD 

My question is: is there a way to extend the TLD to allow these attributes, or is there another way to add these custom attributes to these spring: form tags?

+3
source share
2 answers

It is implemented in Spring 3.0 ( SPR-5931 ).

0
source

Ok, 2 years later ... here we go!

HOW TO CREATE OUR OWN TAG EXTENDING A CLASSIC ONE WITH NEW ATTRIBUTES IN SPRING MVC 3


1. Create your own taglib.tld

You need to create your own TLD file. There you will add a new attribute that you intend to use. The best option is copy / paste spring -form.tld. You can find it in the spring -mvc package (org.springframework.web.servlet-.jar). Search in the META-INF folder.

Feel free to post it directly in WEB-INF or in a subfolder. I put it in /WEB-INF/tld .

Now find in your new TLD file the tag that you are going to change. I changed the input tag, so I had to search:

  ... <tag> <description>Renders an HTML 'input' tag with type 'text' using the bound value.</description> <name>input</name> <tag-class>org.domain.tags.CustomTags</tag-class> <body-content>empty</body-content> <attribute> ... 

Copy and paste it below. Change the new tag to your name. Mine was mine. So now we have this:

  ... <tag> <description>Renders an HTML 'input' tag with type 'text' using the bound value.</description> <name>input</name> <tag-class>org.springframework.web.servlet.tags.form.InputTag</tag-class> <body-content>empty</body-content> <attribute> ... </tag> <tag> <description>Renders an HTML 'input' tag with type 'text' using the bound value.</description> <name>myInput</name> <tag-class>org.domain.tags.CustomTags</tag-class> <body-content>empty</body-content> <attribute> ... </tag> 

SUMMARY: I now have a new taglib.tld file: /WEB-INF/tld/taglib.tld . Pay attention to the <tag-class> part

In your new tag add a new attribute (copy / paste another), render.

 <attribute> <description>Enables/Disables the field rendering</description> <name>render</name> <required>false</required> <rtexprvalue>true</rtexprvalue> <type>java.lang.Boolean</type> </attribute> 

Now we have created the taglib file that we need. See how to use it.

2. Create your own handler

Now we are going to create a class that will handle the new attribute (and classic). I created the CustomTags.java en el paquete org.domain.tags class. Look at the code first and explain it:

 package org.domain.tags; import javax.servlet.jsp.JspException; import org.springframework.web.servlet.tags.form.InputTag; import org.springframework.web.servlet.tags.form.TagWriter; public class CustomTags extends InputTag { private static final long serialVersionUID = 1L; private boolean render; public boolean isRender() { return render; } public void setRender(boolean render) { this.render = render; } protected int writeTagContent(TagWriter tagWriter) throws JspException { if(render){ super.writeTagContent(tagWriter); return SKIP_BODY; } else return SKIP_BODY; } } 

Of course, if we add some functions to the input tag of the SPRING structure, we must extend it to use the rest of the functions. As you can see, we just added the private render attribute as boolean (we also put it in taglib.tld).

We have added getter and setter methods for this attribute.

Finally, we overwrite the writeTagContent method to make the JSP do what we want. In this case, we want the input field to display if render is true. Otherwise, the field cannot be displayed. Therefore, we call writeTagContent of the parent class.

If we need to make some changes to the behavior of a tag, this method will be the right place to execute them.

3. Using a new tag .

Now we only need a JSP with a form to use the new tag. This is easy to do, so I can only enter the code here:

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix = "newtags" uri = "/WEB-INF/tld/taglib.tld" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Spring 3.0 MVC Series: Welcome World</title> </head> <body> <h1>User Data</h1> Please, fill the fields with your information:<br> <newtags:form name="userForm" id="userForm" modelAttribute="userForm" action="user.htm" method="POST"> Name: <newtags:myInput type="text" name="textName" path="userName" render="true" size="50" /><newtags:errors path="userName" /><br> Surname: <newtags:myInput type="text" name="textSurname" path="userSurname" render="true" size="50" /><newtags:errors path="userSurname" /><br> Age: <newtags:myInput type="text" name="textAge" path="userAge" render="true" size="2" /><newtags:errors path="userAge" /><br> Example: <newtags:myInput type="text" name="textSurname" render="false" size="20" path="userSurname"/> <hr> <input type="submit" value="Next" /> </newtags:form> </body> </html> 

Now, instead of calling springframework tld, we call our own TLD. As you can see, the only field that will not be shown is Example 1.

Good luck everyone!

+9
source

All Articles