使用Spring标签库

A、Spring标签库 html

Web项目若使用Spring Web MVC并使用JSP做为表现的话。从Spring2.0版本开始提供一套标签库可供使用。web

使用标签库无非是易于开发,维护之类云云。这里就不阐述了。咱们仍是更关注spring有哪些标签库和如何使用。spring

B、spring.tld标签库网络

spring.tld标签库核心类的包在org.springframework.web.servlet.tags。session

B.1、spring:hasBindErrorsapp

对应org.springframework.web.servlet.tags.BindErrorsTag标记库处理类。框架

这个标记提供用于绑定对象的errors,若是这个标记被用到的话,那么关于这个对象的错误将在页面上显示出来。使用这个标记的前提条件是要先使用<spring:bind>标记,而且<spring:hasBindErrors>这个标记不能用来表示对象的状态,它仅仅能够绑定对象自己和对象的属性。jsp

举例spa

<spring:hasBindErrors name="priceIncrease">code

<b>Please fix all errors!</b>

</spring:hasBindErrors>

属性

  name:是要被检查的Bean的名字。这个属性是必须要的。

B.2、spring:bind

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

  这个标记用来为某个bean或bean 的属性赋值,一般和form一块儿用,至关于action的做用。它指明表单要提交到那个类或类的属性中去。

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

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

B.4、spring:message

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

  这个标记用来帮助springframework支持国际化。和JSTL的fmt:message标记相似。固然这个标记能够很好的工做的本地的springframework框架下。

 

属性

  code:不是必需的。用来查找message,若是没有被使用的话,text将被使用。

text:不是必需的。假如code不存在的话,默认是text输出。当code和text都没有设置的话,标记将输出为null.

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

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

 

B.5、spring:htmlEscape

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

B.6、spring:theme

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

 

C、spring-form.tld标签库

Spring-form.tld标签库核心类的包在org.springframework.web.servlet.tags.form。

spring的表单标签库

D、使用Spring标签库

D.1、方法1

曾在《[JSP]自定义标签》介绍过如何自定义标签,那么咱们知道咱们必须取得标签库描述文件(spring.tld和Spring-form.tld)、标签处理类、并在web.xml中引入、最后才在jsp中使用。

一、将spring.tld和Spring-form.tld拷贝到WEB-INF目录。

二、将spring.jar拷贝到WEB-INF\lib包下

三、配置web.xml

<!-- 定义标签库描述文件 -->

<jsp-config>

    <taglib>

       <taglib-uri>/spring-form</taglib-uri>

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

    </taglib>

    <taglib>

       <taglib-uri>/spring</taglib-uri>

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

    </taglib>

</jsp-config>

<!-- 使用监听器加载spring配置文件 -->

<context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>

</context-param>

<listener>

    <listener-class>

       org.springframework.web.context.ContextLoaderListener

    </listener-class>

</listener>

必定要使用listener加载spring配置文件,否则会报“No WebApplicationContext found”的错。

4.JSP代码

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

<%@ taglib uri="/spring-form" prefix="from"%>

<html>

    <head>

       <title></title>

    </head>

    <body>

       <spring:message></spring:message>

       <from:form></from:form>

    </body>

</html>

这种方法咱们使用本地的tld,这种方法的好处咱们能够自定义dtl。固然咱们也可使用网络上的tld。

D.2、方法2

一、配置web.xml

<!-- 使用监听器加载spring配置文件 -->

<context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>

</context-param>

<listener>

    <listener-class>

       org.springframework.web.context.ContextLoaderListener

    </listener-class>

</listener>

2.JSP代码

<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="from"%>

<html>

    <head>

       <title></title>

    </head>

    <body>

       <spring:message></spring:message>

       <from:form></from:form>

    </body>

</html>
相关文章
相关标签/搜索