扩展spring表单标记库属性

Tim*_*myJ 3 forms spring jsp-tags spring-mvc

我正在开发的Spring MVC应用程序中使用Spring的表单标记库.我正在为之工作的公司根据某些标签的自定义属性的定义实施了一些公司范围的政策.例如,默认情况下(虽然包含标准的javascript文件)所有标签的值都会自动转换为大写.为了禁用此功能,可以通过以下方式使用自定义属性定义其标记:

<input type="text" uppercase="false" />
Run Code Online (Sandbox Code Playgroud)

问题是将这些自定义属性添加到spring:form标记会在运行时导致错误.我已经粘贴了下面的错误.

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

我的问题是:有没有办法扩展TLD以允许这些属性,还是有任何其他方式将这些自定义属性添加到这些spring:form标签?

Ful*_*ara 9

好的,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文件中搜索您要修改的标记.我修改了输入标签,所以我不得不搜索:

  ...
  <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>
  ...
Run Code Online (Sandbox Code Playgroud)

从中复制并粘贴到下面.更改您自己名称的新标签.我是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>
Run Code Online (Sandbox Code Playgroud)

总结:现在我在这里有一个名为taglib.tld的新文件:/WEB-INF/tld/taglib.tld.注意<tag-class>部分

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

<attribute>
  <description>Enables/Disables the field rendering</description>
  <name>render</name>
  <required>false</required>
  <rtexprvalue>true</rtexprvalue>
  <type>java.lang.Boolean</type>
</attribute>
Run Code Online (Sandbox Code Playgroud)

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

2.创建自己的处理程序

现在我们将创建将处理新属性(以及经典属性)的类.我创建了类CustomTags.java en el paquete org.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;
  }
}
Run Code Online (Sandbox Code Playgroud)

当然,如果我们要在Spring框架的Input标签中添加一些功能,我们必须扩展它以保留其余的功能.如您所见,我们刚刚将render私有属性添加为boolean(在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>
Run Code Online (Sandbox Code Playgroud)

现在我们称之为springframework tld而不是调用springframework tld.如您所见,唯一不会显示的字段是示例一.

祝大家好运!