专注Java教育14年 全国咨询/投诉热线:400-8080-105
动力节点LOGO图
始于2009,口口相传的Java黄埔军校
首页 hot资讯 使用Spring标签库的方法

使用Spring标签库的方法

更新时间:2021-09-13 11:39:46 来源:动力节点 浏览852次

1.Spring标签库

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

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

2.spring.tld标签库

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

(1)spring:hasBindErrors

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

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

举例

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

属性

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

(2)spring:bind

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

这个标记用来为某个bean或bean的属性赋值,通常和form一起用,相当于action的作用。它指明表单要提交到那个类或类的属性中去。

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

(3)spring:transform

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

属性

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

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

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

(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。

(5)spring:htmlEscape

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

(6)spring:theme

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

3.spring-form.tld标签库

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

spring的表单标签库

4.使用Spring标签库

(1)方法1

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

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

2)将spring.jar拷贝到WEB-INF\lib包下

3)配置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。

(2)方法2

1)配置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>

以上就是动力节点小编介绍的"使用Spring标签库的方法",希望对大家有帮助,想了解更多可查看Spring框架教程。动力节点在线学习教程,针对没有任何Java基础的读者学习,让你从入门到精通,主要介绍了一些Java基础的核心知识,让同学们更好更方便的进行Java学习,感兴趣的同学可以关注一下。

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>