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

首頁 > 學院 > 開發設計 > 正文

Struts開發指南之安裝與使用部分

2019-11-18 15:55:47
字體:
來源:轉載
供稿:網友

  Struts可以運行在任何一個支持jsp1.2和Servlet2.3的WEB Container中Struts將所有的請求提交到同一個中心控制器,org.apache.struts.action.ActionServlet 類 
  
  web.xml配置 
  
  <servlet-name>action</servlet-name>
 ?。約ervlet-class>org.apache.struts.action.ActionServlet</servlet-class>
 ?。糹nit-param>
 ?。紁aram-name>config</param-name>
 ?。紁aram-value>/WEB-INF/struts-config.xml</param-value>
 ?。?init-param>
 ?。糽oad-on-startup>2</load-on-startup>
  
  <servlet-mapping> 
  
  一個標準的使用了Struts的URL樣式如下: 
  
  擴展映射:http://www.my_site_name.com/mycontext/actionName.do 
  
  路徑映射:http://www.my_site_name.com/mycontext/do/action_Name 
  
 ?。約ervlet-name>action</servlet-name>
 ?。紆rl-pattern>*.do或/do/*</url-pattern>
 ?。?servlet-mapping> 
  
  Struts運行 
  
  Struts首先在Container啟動的時候調用ActionServlet的init()方法。初始化各種配置。這些配置寫在struts-config.xml文件中?!?br />  
  一個標準的struts-config文件包含如下結構: 
  
 ?。?xml version="1.0" encoding="UTF-8"?>
 ?。?DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
  <struts-config>
 ?。糳ata-sources /> // 定義數據源
 ?。糵orm-beans /> // 定義ActionForm
  <global-exceptions /> // 定義全局異常
 ?。糶lobal-forwards /> // 定義全局轉向url
 ?。糰ction-mappings /> // 定義action
  <controller /> // 配置Controller
 ?。糾essage-resources /> // 配置資源文件
 ?。?struts-config> 
  
  Struts由上述幾部分組成。其中最主要的是Action和Form。下面簡單敘述一下其處理過程。
  
Struts開發指南之安裝與使用部分

  一個請求提交給ActionServlet,ActionServlet會尋找相應的Form和Action,首先將提交的request對象,映射到form中。,然后將form傳遞給action來進行處理。action得到form,對xml的mapping,request,response四個對象,并調用execute()方法然后返回一個forward-url(相應視圖)給ActionServlet,最終返回給客戶端。 
  
  我們來看一個最簡單的實例 
  
  說明:實例一是最簡單的Struts程序。它僅僅使用了1個form和1個action功能是將首頁輸入的值傳遞給action,經過判定后返回結果。假如是空則返回empty。代碼如下:
  
  input.jsp:
  
 ?。糵orm method="post" action="/example.do">
  請輸入值
   <input type="text" name="test"/>
   


  ?。糹nput type="submit" name="Submit"?。?br />  ?。糹nput name="reset" type="reset"?。?br />  </form> 
  
  struts-config.xml:
  
  <struts-config>
   // 配置formbean
  ?。糵orm-beans>
  <form-bean name="exampleActionForm" type="com.zotn.struts.example1.ExampleActionForm" />
   </form-beans>
   // 配置action
  ?。糰ction-mappings>
 ?。糰ction name="exampleActionForm" path="/example" type="com.zotn.struts.example1.ExampleAction">
   // action內部的foward
   <forward name="foward" path="/foward.jsp" />
  </action>
  ?。?action-mappings>
 ?。?struts-config>  
  
  Action:
  
  public class ExampleAction extends Action {
   public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
  // 得到對應的form
  ExampleActionForm eaf = (ExampleActionForm)actionForm;
  // 取得輸入的test
  String test = eaf.getTest();
  // 判定并將值放入request
  if ("".equals(test)){
   request.setAttribute("test","empty");
  }else{
   request.setAttribute("test",test);
  }
  // 通過mapping尋找相應的url,返回ActionFoward
  return actionMapping.findForward("foward");
   }
  } 
  
  FormBean:
  
  public class ExampleActionForm extends ActionForm {
   PRivate String test;
   public String getTest() {
  return test;
   }
   public void setTest(String test) {
  this.test = test;
   }
  }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 金湖县| 宜宾县| 南乐县| 石柱| 依安县| 永德县| 莱芜市| 固镇县| 祁东县| 长葛市| 阳谷县| 乌兰浩特市| 宁河县| 芮城县| 牙克石市| 卫辉市| 蓬莱市| 墨江| 伊吾县| 阜康市| 宜良县| 崇仁县| 阜康市| 虹口区| 齐齐哈尔市| 通渭县| 民勤县| 扶余县| 南昌县| 合阳县| 澄城县| 楚雄市| 河池市| 稻城县| 南川市| 乌兰察布市| 涿州市| 江孜县| 龙州县| 淮北市| 五河县|