Spring的基础标签库提供了简单易用的数据绑定和显示功能

Spring的基础标签库提供了简单易用的数据绑定和显示功能。

配置基础标签库:html

 须要将Spring安装包中的spring.tld复制到项目的WEB-INF目录下,并在web.xml中配置该标签库: java

<taglib>
        <taglib-uri>/spring</taglib-uri>
        <taglib-location>/WEB-INF/spring.tld</taglib-location>
    </taglib>

 

 而后在JSP页面的启示部分加入下面这行声明:web

<%@ taglib uri="/spring" prefix="/spring"  %> 或者 <%@ taglib uri="/spring" prefix="spring"  %>

其中 “/spring” 中的spring为该标签库的引用别名,即在web.xml中<taglib-uri>/声明的部分spring

 

1. <spring:bind> express

对应org.springframework.web.servlet.tags.BindTag标记库处理类api

该标签用来为某个bean或bean的属性赋值,它一般和form一块儿用,用以指明表单要提交到哪一个类或类的属性中去,举例以下:session

<form method="post">
    <spring:bind path="user.age">
       姓名: <input type="text" name="${status.expression}" value="${status.value}">
          <font color="red">${status.errorMessage}</font>
    </spring:bind>
</form>

 

其中path属性是必需的,指明转到的类的路径。app

 

2. <spring:hasBindErrors>jsp

 

    对应org.springframework.web.servlet.tags.BindErrorsTag标记库处理类。
这个标记提供用于绑定对象的errors,若是这个标记被用到的话,那么关于这个对象的错误将在页面上显示出来。使用这个标记的前提条件是要先使用 <spring:bind>标记,而且<spring:hasBindErrors>这个标记不能用来表示对象的状态,它仅仅能够绑定对象自己和对象的属 性。 具体用法以下: post

   

    <spring:hasBindErrors name="priceIncrease">
      <b>Please fix all errors!</b>
        </spring:hasBindErrors>

 

经过这个简单的例子,我来具体说一下这个标记的属性吧, name:是要被检查的Bean的名字。这个属性是必须要的。这里是简单用法,因此就不介绍它包含的errors变量了。具体看英语文档。下同。 

 

3. <spring:transform>

 

 对应org.springframework.web.servlet.tags.TransformTag标记库处理类,这个标记用来转换表单 中不与bean中的属性一一对应的那些属性,一般和<spring:bind>一块儿使用。<spring:transform> 标记为<spring:bind>使用提供了更好的支持。

属性以下:

value:必须要的。和当前<spring:bind>标记指向的bean类相同。就是你要转换的实体类名。

var:不是必需的。这个字符串被用来绑定输出结果到page,request, session或application scope.默认状况输出到jsp中。

scope:不是必需的。前提条件var必须设置的状况下。它的值能够是page,request, session或application。

4. <spring:message>

 

根据code取得消息资源,若是指定的code没有找到任何对应的消息资源,则采用text定义的内容。通常用于I18N。
属性-
message:MessageSourceResolvable 类型
code:查找消息资源时使用的关键字
arguments:属性message的参数
argumentSeparator:参数分割符号,默认为“,”(英文)
text:默认替代文本(根据code,未找到资源时)
var(scope):存放查找到的消息资源
htmlEscape: boolean类型,设定html escape 属性
javaScriptEscape: boolean类型,设定JavaScript escaping 属性

示范代码:

<spring:message code="username" test="用户名"/>

 

5.<spring:htmlEscape> 

 

设置是否启用 默认html字符转换,默认为“false”
示范代码:

<spring:htmlEscape defaultHtmlEscape="false">

 

   6.<spring:theme>


主题配置,主要用来配置I18N主题message,code,arguments,argumentSeparator,text,var,scope,htmlEscape,javaScriptEscape:相似<spring:message> 
代码:

<spring:theme code="styleSheet"/>

  

7.<spring:nestedpath> 

 

为<spring:bind>配置嵌套路径
变量:nestedPath   java.lang.String类型
path:设置嵌套的路径,例如:配置"customer"后,能够设置为"address.street"替代"customer.address.street" 

 

8.<spring:escapeBody> 

 

相似<spring:htmlEscapey>
escape its enclosed(被附上的) body content, applying HTML escaping and/or JavaScript escaping
属性(boolean类型值,字符串表示):
htmlEscape:同上
javaScriptEscape:相似htmlEscape

相关文章
相关标签/搜索