zoukankan      html  css  js  c++  java
  • 使用Spring标签库

    A、Spring标签库

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

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

    B、spring.tld标签库

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

    B.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的名字。这个属性是必需要的。

    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中使用。

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

    2、将spring.jar拷贝到WEB-INFlib包下

    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。

    D.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>
  • 相关阅读:
    Laravel源码解析 — 服务容器
    Java日志框架中需要判断log.isDebugEnabled()吗?
    使用C语言实现线性表
    new-delete
    webrtc-AudioprcessingModule 3A算法demo
    关于iPhone语音备忘录访问
    关于音频通话耗时
    关于Windows上使用OpenAL API声源音效
    c++ 类术语
    二分法查找
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/4870668.html
Copyright © 2011-2022 走看看