Spring提供它自訂的標籤庫,可以與相關的組件結合,提供頁面與表單物件、錯誤訊息的數據綁定功能,要使用Spring的標籤數據綁定,請將其發行檔案中dist目錄下的spring.tld複製至Web應用程式的/WEB-INF/下,並在web.xml中加入:
代碼:
<taglib>
<taglib-uri>/spring</taglib-uri>
<taglib-location>/WEB-INF/spring.tld</taglib-location>
</taglib>
我們以前一個主題所使用的SimpleFormController作為擴充,加入數據綁定功能,首先看看form.jsp:
代碼:
<%@taglib prefix="spring" uri="/spring"%>
<%@page contentType="text/html;charset=Big5"%>
<html>
<head><title>Login</title></head>
<body>
<spring:bind path="command.*">
<font color="red"><b>${status.errorMessage}</b></font><br>
</spring:bind>
請輸入使用者名稱與密碼:<p>
<form name="loginform" action="/springapp/login.do" method="post">
<spring:bind path="command.username">
名稱 <input type="text" name="${status.expression}" value="${status.value}"/><br>
</spring:bind>
<spring:bind path="command.password">
密碼 <input type="password" name="${status.expression}" value="${status.value}"/><br>
</spring:bind>
<input type="submit" value="確定"/>
</form>
注意:輸入錯誤會再回到這個頁面中。
</body>
</html>
<spring:bind>的path屬性設定了要綁定表單物件名稱,這個名稱是設定在loginAction中的commandName屬性,預設名稱是command,當設定為command.*時,表示綁定表單物件所有相關的數據,status的errorMessage會顯示在Controller中設定的錯誤訊息,這待會可以看到。
在表單中,對於username欄位,我們綁定了command.username屬性,status的expression會顯示綁定的屬性名稱,而value則顯示表單物件中所儲存的值,我們設計的程式在登入失敗後會回到form.jsp,這樣我們可以在同一個頁面上顯示錯誤訊息與之前輸入錯誤的值。
我們對LoginAction作一些修正:
代碼:
package onlyfun.caterpillar;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.*;
public class LoginAction extends SimpleFormController {
protected ModelAndView onSubmit(Object command, BindException errors) throws Exception {
LoginForm form = (LoginForm) command;
if("caterpillar".equals(form.getUsername()) &&
"123456".equals(form.getPassword())) {
return new ModelAndView(this.getSuccessView(),"user", form.getUsername());
}
else {
errors.reject("loginfail", "使用者名稱或密碼錯誤");
return new ModelAndView(this.getFormView(), errors.getModel());
}
}
}
Spring標籤綁定需要一個BindException物件,所以這次使用了onSumit()的另一個版本,當驗證失敗時,我們在BindException中使用reject()方法,表示拒絕接受這次輸入的數據,reject()接受兩個參數,第一個是error code,如果您有設定MessageResourceSource,這會到您設定的properties檔中依error code為key值,找出相對應的訊息,如果沒有設定MessageResource,則使用第二個參數所設定的值作為錯誤訊息輸出。
errors.getModel()返回一個Map物件,之前使用reject()所儲存的錯誤訊息就包括在這個物件中,我們將之設定給ModelAndView,這之後會處理為綁定訊息以在標籤上輸出。
剩下的JSP網頁與定義檔,都與前一個主題相同,我們來看看在還沒有登入前form.jsp的顯示內容:
代碼:
<html>
<head><title>Login</title></head>
<body>
<font color="red"><b></b></font><br>
請輸入使用者名稱與密碼:<p>
<form name="loginform" action="/springapp/login.do" method="post">
名稱 <input type="text" name="username" value=""/><br>
密碼 <input type="password" name="password" value=""/><br>
<input type="submit" value="確定"/>
</form>
注意:輸入錯誤會再回到這個頁面中。
</body>
</html>
如果登入錯誤,則會顯示以下的內容,數據綁定標籤將錯誤訊息與之前輸入的數據顯示在對應的標籤上了:
代碼:
<html>
<head><title>Login</title></head>
<body>
<font color="red"><b>使用者名稱或密碼錯誤</b></font><br>
請輸入使用者名稱與密碼:<p>
<form name="loginform" action="/springapp/login.do" method="post">
名稱 <input type="text" name="username" value="caterpillar"/><br>
密碼 <input type="password" name="password" value="654321"/><br>
<input type="submit" value="確定"/>
</form>
注意:輸入錯誤會再回到這個頁面中。
</body>
</html>
這邊先簡單的介紹一下基本的數據綁定,數據綁定還有一些可以要介紹的設定細節,這在下一個主題中介紹。