本例中,,視圖包括兩個組件:
·一個JSP文件:hello.jsp
·一個ActionForm Bean: HelloForm Bean
下面分別講述如何創(chuàng)建這兩個組件,。
創(chuàng)建JSP文件 hello.jsp提供用戶界面,能夠接受用戶輸入的姓名,。此外,,本W(wǎng)eb應(yīng)用的所有輸出結(jié)果也都由hello.jsp顯示給用戶。圖2-1顯示了hello.jsp提供的網(wǎng)頁,。
圖2-1 hello.jsp的網(wǎng)頁 在圖2-1中,,用戶輸入姓名"Weiqin"后,按提交表單,本應(yīng)用將返回"Hello Weiqin!",,參見圖2-2,。
圖2-2 hello.jsp接受用戶輸入后正常返回的網(wǎng)頁 例程2-1為hello.jsp文件的源代碼。
例程2-1 hello.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html:html locale="true">
<head>
<title><bean:message key="hello.jsp.title"/></title>
<html:base/>
</head>
<body bgcolor="white"><p>
<h2><bean:message key="hello.jsp.page.heading"/></h2><p>
<html:errors/><p>
<logic:present name="personbean" scope="request">
<h2>
<bean:message key="hello.jsp.page.hello"/>
<bean:write name="personbean" property="userName" />!<p>
</h2>
</logic:present>
<html:form action="/HelloWorld.do" focus="userName" >
<bean:message key="hello.jsp.prompt.person"/>
<html:text property="userName" size="16" maxlength="16"/><br>
<html:submit property="submit" value="Submit"/>
<html:reset/>
</html:form><br>
<html:img page="/struts-power.gif" alt="Powered by Struts"/>
</body>
</html:html>
以上基于Struts框架的JSP文件有以下特點:
·沒有任何Java程序代碼
·使用了許多Struts的客戶化標簽,,例如
<html:form>和<logic:present>標簽
沒有直接提供文本內(nèi)容,取而代之的是
<bean:message>標簽,,輸出到網(wǎng)頁上的文本內(nèi)容都是由<bean:message> 標簽來生成的,。例如:
<bean:message key="hello.jsp.prompt.person"/> Struts客戶化標簽是聯(lián)系視圖組件和Struts框架中其它組件的紐帶。這些標簽可以訪問或顯示來自于控制器和模型組件的數(shù)據(jù),。在本書第12章至16章講專門介紹Struts標簽的用法,,本節(jié)先簡單介紹幾種重要的Struts標簽。
hello.jsp開頭幾行用于聲明和加載Struts標簽庫:
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
以上代碼表明該JSP文件使用了Struts Bean,、Html和Logic 標簽庫,,這是加載客戶化標簽庫的標準JSP語法。
hello.jsp中使用了來自 Struts HTML標簽庫中的標簽,,包括
<html:errors>, <html:form>和<html:text>:
·
<html:errors>:用于顯示Struts框架中其他組件產(chǎn)生的錯誤消息,。
·
<html:form>:用于創(chuàng)建HTML表單,它能夠把HTML表單的字段和ActionForm Bean的屬性關(guān)聯(lián)起來,。
·
<html:text>:該標簽是
<html:form>的子標簽,,用于創(chuàng)建HTML表單的文本框。它和ActionForm Bean的屬性相關(guān)聯(lián),。
hello.jsp中使用了來自Struts Bean標簽庫的兩個標簽
<bean:message>和<bean:write>:
<bean:message>:用于輸出本地化的文本內(nèi)容,,它的key屬性指定消息key,和消息key匹配的文本內(nèi)容來自于專門的Resource Bundle,,關(guān)于Resource Bundle的概念參見本書第9章(Struts應(yīng)用的國際化),。
<bean:write>:用于輸出JavaBean的屬性值。本例中,,它用于輸出personbean對象的userName屬性值:
<bean:write name="personbean" property="userName" /> hello.jsp使用了來自Struts Logic標簽庫的
<logic:present>標簽,。<logic:present>標簽用來判斷JavaBean在特定的范圍內(nèi)是否存在,只有當JavaBean存在,,才會執(zhí)行標簽主體中的內(nèi)容:
<logic:present name="personbean" scope="request">
<h2>
Hello <bean:write name="personbean" property="userName" />!<p>
</h2>
</logic:present>
本例中,,
<logic:present>標簽用來判斷在request范圍內(nèi)是否存在personbean對象,如果存在,,就輸出personbean的userName屬性值,。和<logic:present>標簽相對的是<logic:notPresent>標簽,它表示只有當JavaBean在特定的范圍內(nèi)不存在,,才會執(zhí)行標簽主體中的內(nèi)容,。
創(chuàng)建消息資源文件 hello.jsp使用
<bean:message>標簽來輸出文本內(nèi)容。這些文本來自于Resource Bundle,每個Resource Bundle都對應(yīng)一個或多個本地化的消息資源文件,,本例中的資源文件為application.properties,,例程2-2是該消息資源文件的內(nèi)容。
例程 2-2 application.properties文件
#Application Resources for the "Hello" sample application
hello.jsp.title=Hello - A first Struts program
hello.jsp.page.heading=Hello World! A first Struts application
hello.jsp.prompt.person=Please enter a UserName to say hello to :
hello.jsp.page.hello=Hello
#Validation and error messages for HelloForm.java and HelloAction.java
hello.dont.talk.to.monster=We don't want to say hello to Monster!!!
hello.no.username.error=Please enter a <i>UserName</i> to say hello to!
以上文件以"消息key/消息文本"的格式存放數(shù)據(jù),,文件中"#"后面為注釋行,。對于以下JSP代碼:
<bean:message key="hello.jsp.title"/> <bean:message>標簽的key屬性為"hello.jsp.tilte",在Resource Bundle中與之匹配的內(nèi)容為:hello.jsp.title=Hello - A first Struts program
因此,,以上
<bean:message>標簽將把"Hello - A first Struts program"輸出到網(wǎng)頁上,。
創(chuàng)建ActionForm Bean 當用戶提交了HTML表單,Struts框架自動把表單數(shù)據(jù)組裝到ActionForm Bean中,。ActionForm Bean中的屬性和HTML表單中的字段一一對應(yīng),。ActionForm Bean還提供數(shù)據(jù)驗證方法,以及把屬性重新設(shè)置為默認值的方法,。Struts框架中定義的ActionForm類是抽象的,,必須在應(yīng)用中創(chuàng)建它的子類,來存放具體的HTML表單數(shù)據(jù),。例程2-3為HelloForm.java的源程序, 它用于處理hello.jsp中的表單數(shù)據(jù),。
例程2-3 HelloForm.java
package hello;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
public final class HelloForm extends ActionForm {
private String userName = null;
public String getUserName() {
return (this.userName);
}
public void setUserName(String userName) {
this.userName = userName;
}
/**
* Reset all properties to their default values.
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
this.userName = null;
}
/**
* Validate the properties posted in this request. If validation errors are
* found, return an <code>ActionErrors</code> object containing the errors.
* If no validation errors occur, return <code>null</code> or an empty
* <code>ActionErrors</code> object.
*/
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if ((userName == null) || (userName.length() < 1))
errors.add("username", new ActionMessage("hello.no.username.error"));
return errors;
}
}
從以上代碼可以看出,ActionForm Bean實質(zhì)上是一種JavaBean,,不過它除了具有JavaBean的常規(guī)方法,,還有兩個特殊方法:
·validate():用于表單驗證。
·reset():把屬性重新設(shè)置為默認值,。
葉子樹:www.