国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

創建JSP文件和ActionForm Bean

2024-09-05 00:20:03
字體:
來源:轉載
供稿:網友
   本例中,視圖包括兩個組件:
    ·一個jsp文件:hello.jsp
    ·一個actionform bean: helloform bean
    下面分別講述如何創建這兩個組件。

    創建jsp文件

    hello.jsp提供用戶界面,能夠接受用戶輸入的姓名。此外,本web應用的所有輸出結果也都由hello.jsp顯示給用戶。圖2-1顯示了hello.jsp提供的網頁。


圖2-1 hello.jsp的網頁


    在圖2-1中,用戶輸入姓名"weiqin"后,按提交表單,本應用將返回"hello weiqin!",參見圖2-2。


圖2-2 hello.jsp接受用戶輸入后正常返回的網頁


    例程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>標簽
    沒有直接提供文本內容,取而代之的是<bean:message>標簽,輸出到網頁上的文本內容都是由<bean:message> 標簽來生成的。例如:

<bean:message key="hello.jsp.prompt.person"/>

    struts客戶化標簽是聯系視圖組件和struts框架中其它組件的紐帶。這些標簽可以訪問或顯示來自于控制器和模型組件的數據。在本書第12章至16章講專門介紹struts標簽的用法,本節先簡單介紹幾種重要的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框架中其他組件產生的錯誤消息。
    ·<html:form>:用于創建html表單,它能夠把html表單的字段和actionform bean的屬性關聯起來。
    ·<html:text>:該標簽是<html:form>的子標簽,用于創建html表單的文本框。它和actionform bean的屬性相關聯。

    hello.jsp中使用了來自struts bean標簽庫的兩個標簽<bean:message>和<bean:write>:
    <bean:message>:用于輸出本地化的文本內容,它的key屬性指定消息key,和消息key匹配的文本內容來自于專門的resource bundle,關于resource bundle的概念參見本書第9章(struts應用的國際化)。
    <bean:write>:用于輸出javabean的屬性值。本例中,它用于輸出personbean對象的username屬性值:
<bean:write name="personbean" property="username" />

    hello.jsp使用了來自struts logic標簽庫的<logic:present>標簽。<logic:present>標簽用來判斷javabean在特定的范圍內是否存在,只有當javabean存在,才會執行標簽主體中的內容:

<logic:present name="personbean" scope="request">       <h2>         hello <bean:write name="personbean" property="username" />!<p>       </h2></logic:present>


    本例中,<logic:present>標簽用來判斷在request范圍內是否存在personbean對象,如果存在,就輸出personbean的username屬性值。和<logic:present>標簽相對的是<logic:notpresent>標簽,它表示只有當javabean在特定的范圍內不存在,才會執行標簽主體中的內容。

    創建消息資源文件

    hello.jsp使用<bean:message>標簽來輸出文本內容。這些文本來自于resource bundle,每個resource bundle都對應一個或多個本地化的消息資源文件,本例中的資源文件為application.properties,例程2-2是該消息資源文件的內容。

例程  2-2 application.properties文件#application resources for the "hello" sample applicationhello.jsp.title=hello - a first struts programhello.jsp.page.heading=hello world! a first struts applicationhello.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.javahello.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/消息文本"的格式存放數據,文件中"#"后面為注釋行。對于以下jsp代碼:

<bean:message key="hello.jsp.title"/>

<bean:message>標簽的key屬性為"hello.jsp.tilte",在resource bundle中與之匹配的內容為:hello.jsp.title=hello - a first struts program

    因此,以上<bean:message>標簽將把"hello - a first struts program"輸出到網頁上。

    創建actionform bean

    當用戶提交了html表單,struts框架自動把表單數據組裝到actionform bean中。actionform bean中的屬性和html表單中的字段一一對應。actionform bean還提供數據驗證方法,以及把屬性重新設置為默認值的方法。struts框架中定義的actionform類是抽象的,必須在應用中創建它的子類,來存放具體的html表單數據。例程2-3為helloform.java的源程序, 它用于處理hello.jsp中的表單數據。

例程2-3  helloform.javapackage 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實質上是一種javabean,不過它除了具有javabean的常規方法,還有兩個特殊方法:
    ·validate():用于表單驗證。
    ·reset():把屬性重新設置為默認值。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 柳江县| 承德市| 双江| 全州县| 屯门区| 拜城县| 凤翔县| 金坛市| 紫阳县| 美姑县| 从化市| 呈贡县| 新营市| 谷城县| 无极县| 中宁县| 德令哈市| 莱阳市| 鲁山县| 光泽县| 都昌县| 通道| 如东县| 黄石市| 泽库县| 木兰县| 郑州市| 高台县| 黄冈市| 松滋市| 瑞金市| 彭州市| 庄浪县| 邵阳市| 北宁市| 东乌| 盐城市| 麻江县| 富川| 花莲县| 山西省|