扩展spring表单标签库属性

发布于 2024-08-26 12:40:01 字数 526 浏览 7 评论 0原文

我正在开发的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦与时光遇 2024-09-02 12:40:01

好吧,两年后...我们开始吧!

如何创建我们自己的标签,使用 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 文件中搜索您要修改的标签。我修改了输入标签,所以我必须搜索:

  ...
  <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>
  ...

复制到并将其粘贴到下面。将新标签的 更改为您自己的名字。我的是myInput。现在我们有了:

  ...
  <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>

摘要:现在我在这里有一个名为 taglib.tld 的新文件:/WEB-INF/tld/taglib.tld注意 部分

在您自己的新标签中添加一个新属性(复制/粘贴另一个属性)调用 render

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

现在我们已经创建了我们需要的 taglib 文件。让我们看看如何使用它。

2.创建您自己的处理程序

现在我们将创建将处理新属性(以及经典属性)的类。我创建了 CustomTags.javaorg.domain.tags。我们先看代码并解释一下:

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

当然,如果我们要向Spring框架的Input标签添加一些功能,我们必须扩展它以保留其余功能。正如您所看到的,我们刚刚添加了 render 私有属性作为布尔值(我们也在 taglib.tld 中添加了它)。

我们为此属性添加了 gettersetter 方法。

最后,我们重写了 writeTagContent 方法,以使 JSP 执行我们想要的操作。在本例中,如果 render 为 true,我们希望显示输入字段。否则该字段无法显示。这就是为什么我们调用父类的writeTagContent。

如果我们需要对标签行为进行一些更改,那么此方法是执行这些操作的正确位置。

3.使用新标签。

现在我们只需要一个带有表单的 JSP 即可使用新标签。这很容易做到,所以我只在这里放代码:

<%@ 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>

现在我们不调用 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:

  ...
  <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 from to and paste it below. Change the of the new tag for your own name. Mine was myInput. 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: now I have a new file called taglib.tld here: /WEB-INF/tld/taglib.tld. Pay attention to the <tag-class> part

In your own new tag add a new attribute (copy/paste another one) call 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 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:

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 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:

<%@ 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 is not going to be shown is Example one.

Good Luck all!

只想待在家 2024-09-02 12:40:01

它在 Spring 3.0 (SPR-5931) 中实现。

It's implemented in Spring 3.0 (SPR-5931).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文