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

首頁 > 開發 > 綜合 > 正文

創建Web應用和Struts框架的配置文件

2024-07-21 02:15:20
字體:
來源:轉載
供稿:網友


收集最實用的網頁特效代碼!

    創建web應用的配置文件

    對于struts應用,它的配置文件web.xml應該對actionservlet類進行配置,此外,還應該聲明web應用所使用的struts標簽庫,本例中聲明使用了三個標簽庫: struts bean、struts html和struts logic標簽庫。例程2-7為web.xml的源代碼。

例程2-7  web.xml<?xml version="1.0" encoding="utf-8"?><!doctype web-app  public "-//sun microsystems, inc.//dtd web application 2.2//en"  "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"><web-app>  <display-name>helloapp struts application</display-name>   <!-- standard action servlet configuration  -->  <servlet>    <servlet-name>action</servlet-name>    <servlet-class>org.apache.struts.action.actionservlet</servlet-class>    <init-param>      <param-name>config</param-name>      <param-value>/web-inf/struts-config.xml</param-value>    </init-param>     <load-on-startup>2</load-on-startup>  </servlet>  <!-- standard action servlet mapping -->  <servlet-mapping>    <servlet-name>action</servlet-name>    <url-pattern>*.do</url-pattern>  </servlet-mapping>  <!-- the usual welcome file list -->  <welcome-file-list>    <welcome-file>hello.jsp</welcome-file>  </welcome-file-list>  <!-- struts tag library descriptors -->  <taglib>    <taglib-uri>/web-inf/struts-bean.tld</taglib-uri>    <taglib-location>/web-inf/struts-bean.tld</taglib-location>  </taglib>  <taglib>    <taglib-uri>/web-inf/struts-html.tld</taglib-uri>    <taglib-location>/web-inf/struts-html.tld</taglib-location>  </taglib>  <taglib>    <taglib-uri>/web-inf/struts-logic.tld</taglib-uri>    <taglib-location>/web-inf/struts-logic.tld</taglib-location>  </taglib></web-app>


    創建struts框架的配置文件

    正如前面提及的,struts框架允許把應用劃分成多個組件,提高開發速度。而struts框架的配置文件struts-config.xml可以把這些組件組裝起來,決定如何使用它們。例程2-8是helloapp應用的struts-config.xml文件的源代碼。

例程2-8  struts-config.xml<?xml version="1.0" encoding="iso-8859-1" ?><!doctype struts-config public          "-//apache software foundation//dtd struts configuration 1.1//en"          "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"><!--     this is the struts configuration file for the "hello!" sample application--><struts-config>    <!-- ======== form bean definitions =================================== -->    <form-beans>        <form-bean name="helloform" type="hello.helloform"/>    </form-beans>  <!-- ========== action mapping definitions ============================== -->  <action-mappings>    <!-- say hello! -->    <action    path      = "/helloworld"               type      = "hello.helloaction"               name      = "helloform"               scope     = "request"               validate  = "true"               input     = "/hello.jsp"     >        <forward name="sayhello" path="/hello.jsp" />    </action>  </action-mappings>  <!-- ========== message resources definitions =========================== -->  <message-resources parameter="hello.application"/></struts-config>


    以上代碼對helloapp應用的helloform、helloaction和消息資源文件進行了配置,首先通過<form-bean>元素配置了一個actionform bean,名叫helloform,它對應的類為hello.helloform:
<form-bean name="helloform" type="hello.helloform"/>

    接著通過元素配置了一個action組件:

<action    path      = "/helloworld"          type      = "hello.helloaction"          name     = "helloform"          scope     = "request"          validate   = "true"             input     = "/hello.jsp">        <forward name="sayhello" path="/hello.jsp" /></action>


    <action>元素的path屬性指定請求訪問action的路徑,type屬性指定action的完整類名,name屬性指定需要傳遞給action的actionform bean,scope屬性指定actionform bean的存放范圍,validate屬性指定是否執行表單驗證,input屬性指定當表單驗證失敗時的轉發路徑。<action>元素還包含一個<forward>子元素,它定義了一個請求轉發路徑。

    本例中的 <action>元素配置了helloaction組件,對應的類為hello.helloaction,請求訪問路徑為"helloworld",當action類被調用時,struts框架應該把已經包含表單數據的helloform bean傳給它。helloform bean存放在request范圍內,并且在調用action類之前,應該進行表單驗證。如果表單驗證失敗,請求將被轉發到接收用戶輸入的網頁hello.jsp,讓用戶糾正錯誤。

    struts-config.xml文件最后通過元素定義了一個resource bundle: <message-resources parameter="hello.application"/>

    <message-resources>元素的parameter屬性指定resource bundle使用的消息資源文件。本例中parameter屬性為"hello.application",表明消息資源文件名為"application.properties",它的存放路徑為web-inf/classes/hello/application.properties。



本文選自飛思圖書《精通struts:基于mvc的java web設計與開發》
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 天津市| 平陆县| 湘潭市| 乐昌市| 阜阳市| 玉溪市| 礼泉县| 楚雄市| 天津市| 清原| 当阳市| 陈巴尔虎旗| 井研县| 义马市| 区。| 霸州市| 卓资县| 永丰县| 邵东县| 贡山| 穆棱市| 武乡县| 牡丹江市| 满城县| 都江堰市| 衡阳县| 凌源市| 共和县| 汶上县| 泰和县| 隆尧县| 高陵县| 祁阳县| 巢湖市| 和田市| 英超| 托克逊县| 德昌县| 大洼县| 迭部县| 山阳县|