Spring Framework標記庫初學指南

2021-04-14 23:26:24 字數 4092 閱讀 8728

上面是關於乙個新的framework的簡單介紹,據說是未來j2ee發展的方向。以前在論壇上有關於j2ee最佳組合的討論,聽說這個jstl+struts(或webwork)+spring+hibernate的組合很被看好,如果你感興趣的話,可以到springframework中文論壇上看看。下面轉入本文的正題.

spring 標記庫介紹及用法:

當你在專案中用spring framework的時候,表現層可以選擇spring framework自帶的標記庫。當然,這不是你唯一的選擇。你可以選擇其它標記庫或模板替換技術。spring framework 並沒有為應用提供非常豐富的標記庫。spring framework 提供的標記庫僅僅是為了夠用就行,它並不是無所不包的。呵呵,這點與struts豐富的標記庫不一樣。其實這點符合rodjohnson的出發點,既「不造輪子」。當然,spring framework標記庫有它自身的特點,可以說是個有益的補充吧。

spring 標記庫分類如下:

1、spring:hasbinderrors

2、spring:bind

3、spring:transform

4、spring:message

5、spring:htmlescape

6、spring:theme

下面我們來具體介紹一下用法:

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的名字。這個屬性是必需要的。

這裡是簡單用法,所以就不介紹它包含的errors變數了。具體看英語文件。下同。

2、spring:bind

對應org.springframework.web.servlet.tags.bindtag標記庫處理類

這個標記用來為某個bean或bean 的屬性賦值,通常和form一起用,相當於action的作用。它指明表單要提交到那個類或類的屬性中去。

其中path屬性是必須的,指明轉到的類的路徑。舉例如下:

<form method="post">

<table width="95%" bgcolor="f8f8ff" border="0" cellspacing="0" cellpadding="5">

<tr>

<td alignment="right" width="20%">increase (%):</td>

<spring:bind path="priceincrease.percentage">

<td width="20%">

<input type="text" name="percentage" value="<c:out value="$"/>">

</td>

<td width="60%">

<font color="red"><c:out value="$"/></font>

</td>

</spring:bind>

</tr>

</table>

<br>

……………………省略

</form>

3、spring:transform

對應org.springframework.web.servlet.tags.transformtag標記庫處理類,這個標記用來轉換表單中不與bean中的屬性一一對應的那些屬性,通常和<spring:bind>一起使用。<spring:transform>標記為<spring:bind>使用提供了更好的支援。

屬性如下:

value:必需要的。和當前<spring:bind>標記指向的bean類相同。就是你要轉換的實體類名。

4、spring:message 對應org.springframework.web.servlet.tags.messagetag標記庫處理類

這個標記用來幫助springframework支援國際化。和jstl的fmt:message標記類似。當然這個標記可以很好的工作的本地的springframework框架下。

屬性如下:

code:不是必需的。用來查詢message,如果沒有被使用的話,text將被使用。

text:不是必需的。假如code不存在的話,預設是text輸出。當code和text都沒有設定的話,標記將輸出為null.

5、spring:htmlescape

對應org.springframework.web.servlet.tags.htmlescapetag標記庫處理類

不常用,這裡省略

6、spring:theme

對應org.springframework.web.servlet.tags.themetag標記庫處理類

不常用,這裡省略

學習完這些用法後,讓我們來看個具體的例子吧。

配置步驟如下:

目錄結構圖:

-web-inf

-classes

-lib

-src

-war

1、將spring.tld拷貝到web-inf目錄。

2、將spring.jar拷貝到web-inflib包下

舉例如下:

在web.xml中配置好taglib屬性,**如下:

<?xml version="1.0" encoding="utf-8"?>

<servlet>

<servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

<url-pattern>*.htm</url-pattern>

<welcome-file-list>

<welcome-file> index.jsp </welcome-file>

</welcome-file-list>

<taglib>

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

<taglib-location>/web-inf/spring.tld</taglib-location>

</taglib>

jsp頁面**如下:

<%@ include file="/web-inf/jsp/include.jsp" %>

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

<html>

<head>

<title><fmt:message key="title"/></title>

</head>

<body>

<h1>

<fmt:message key="priceincrease.heading"/>

</h1>

<form method="post">

<table width="95%" bgcolor="f8f8ff" border="0" cellspacing="0" cellpadding="5">

<tr>

<td alignment="right" width="20%">increase (%):</td>

<spring:bind path="priceincrease.percentage">

<td width="20%">

<input type="text" name="percentage" value="<c:out value="$"/>">

</td>

<td width="60%">

<font color="red">

<c:out va 

Spring FrameWork 學習總結

spring framework主要包含以下內容 1.ioc 和 di ioc容器功能 例項化 初始化元件 裝配元件依賴關係 負責元件生命周管理 ioc inversion of control 是乙個重要的物件導向程式設計的法則來削減電腦程式的耦合問題,也是輕量級spring框架核心。di dep...

Spring FrameWork 總結 依賴

package x.y public class foo beanfactory對於它所管理的bean提供兩種注入依賴方式 實際上它也支援同時使用構造器注入和setter方式注入依賴 需要注入的依賴將儲存在beandefinition中,它能根據指定的propertyeditor實現將屬性從一種格式...

用Spring framework實現定時器功能

採用web自動載入timermanager來治理timer鏈,在class更新伺服器熱載入後會發生異常。這要求對timermanager進行一些非凡的處理才能保證timer鏈的正確性。使用spring framework中提供的timertask自動載入功能可以非常輕易的實現定時器鏈的治理。同時,採...