扩展spring表单标签库属性
我正在开发的 Spring MVC 应用程序中使用 Spring 的表单标签库。我工作的公司已经根据某些标签的自定义属性的定义实施了一些全公司范围的政策。例如,默认情况下(尽管包含标准 JavaScript 文件)所有标签的值都会自动转换为大写。为了禁用此功能,可以通过以下方式使用自定义属性定义其标签:
<input type="text" uppercase="false" />
问题是,将这些自定义属性添加到 spring:form 标签会导致运行时错误。我已将错误粘贴到下面。
org.apache.jasper.JasperException: /WEB-INF/jsp/reportCriteria.jsp(45,5) Attribute uppercase invalid for tag input according to TLD
我的问题是:是否有任何方法可以扩展 TLD 以允许这些属性,或者是否有其他方法将这些自定义属性添加到这些 spring:form 标签中?
I'm using Spring's form tag library in a Spring MVC application that I am developing. The company I am working for has implemented some company-wide policies based on the definition of custom attributes for certain tags. For instance, by default (though the inclusion of a standard javascript file) all tags have their values automatically converted to upper case. In order to disable this one would define their tag with a custom attribute in the following way:
<input type="text" uppercase="false" />
The problem is that the addition of these custom attributes to a spring:form tag causes an error at runtime. I've pasted the 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 any way to extend the TLD to allow for these attributes, or is there any other way add these custom attributes to these spring:form tags?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,两年后...我们开始吧!
如何创建我们自己的标签,使用 Spring MVC 3 中的新属性扩展经典标签
1.创建您自己的 taglib.tld
您需要创建您自己的 TLD 文件。您将在此处添加要使用的新属性。最好的选择是复制/粘贴 spring-form.tld。您可以在 spring-mvc 包(org.springframework.web.servlet-.jar)中找到它。在 META-INF 文件夹中搜索。
请随意将其直接放在 WEB-INF 内或子文件夹中。我把它放在
/WEB-INF/tld
中。现在,在新 TLD 文件中搜索您要修改的标签。我修改了输入标签,所以我必须搜索:
复制到并将其粘贴到下面。将新标签的 更改为您自己的名字。我的是myInput。现在我们有了:
摘要:现在我在这里有一个名为 taglib.tld 的新文件:
/WEB-INF/tld/taglib.tld
。 注意
部分在您自己的新标签中添加一个新属性(复制/粘贴另一个属性)调用 render 。
现在我们已经创建了我们需要的 taglib 文件。让我们看看如何使用它。
2.创建您自己的处理程序
现在我们将创建将处理新属性(以及经典属性)的类。我创建了 CustomTags.java 类 org.domain.tags。我们先看代码并解释一下:
当然,如果我们要向Spring框架的Input标签添加一些功能,我们必须扩展它以保留其余功能。正如您所看到的,我们刚刚添加了 render 私有属性作为布尔值(我们也在 taglib.tld 中添加了它)。
我们为此属性添加了 getter 和 setter 方法。
最后,我们重写了 writeTagContent 方法,以使 JSP 执行我们想要的操作。在本例中,如果 render 为 true,我们希望显示输入字段。否则该字段无法显示。这就是为什么我们调用父类的writeTagContent。
如果我们需要对标签行为进行一些更改,那么此方法是执行这些操作的正确位置。
3.使用新标签。
现在我们只需要一个带有表单的 JSP 即可使用新标签。这很容易做到,所以我只在这里放代码:
现在我们不调用 springframework tld,而是调用我们自己的 TLD。正如您所看到的,唯一不会显示的字段是示例之一。
祝大家好运!
Ok, 2 years later... here we go!
HOW TO CREATE OUR OWN TAG EXTENDING 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 are going to add the new attribute you are going to use. The best option is copy/paste spring-form.tld. You can find it in spring-mvc package (org.springframework.web.servlet-.jar).Search in META-INF folder.
Feel free to put it directly inside WEB-INF or in a subfolder. I putted it in
/WEB-INF/tld
.Now search inside your new TLD file for the tag you are going to modify. I modified input tag, so i had to search for:
Copy from to and paste it below. Change the of the new tag for your own name. Mine was myInput. So now we have this:
SUMMARY: now I have a new file called taglib.tld here:
/WEB-INF/tld/taglib.tld
. Pay attention to the<tag-class>
partIn your own new tag add a new attribute (copy/paste another one) call render.
Now we have created the taglib file we need. Let's see how to use it.
2. Create your own handler
Now we are going to create the class that will handle the new attribute (and the classic ones). I created the class CustomTags.java en el paquete org.domain.tags. Let's see first the code and explain it:
Of course, if we are going to add some features to the Input tag of Spring framework, we must extend it in order to mantain the rest of the features. As you can see, we have just added the render private attribute as boolean (in taglib.tld we also put it).
We have added to the getter and setter methods for this attribute.
Finally we have overwrite the writeTagContent method in order to make the JSP do what we want. In this case we want the input field to be shown if render is true. Otherwise the field can't be shown. That's why we call the writeTagContent of the parent class.
If we need to make some changes on the tag behaviour, this method is the right place to do them.
3. Using the new tag.
Now we only need a JSP with a form to use the new tag. It's easy to do, so i only let here the code:
Now instead of calling springframework tld, we call our own TLD. As you can see the only field that is not going to be shown is Example one.
Good Luck all!
它在 Spring 3.0 (SPR-5931) 中实现。
It's implemented in Spring 3.0 (SPR-5931).