Struts1第二天

來源:互聯網
上載者:User

struts的基本編寫流程:
 1 web.xml中指定
 *.do ===== org.apache.struts.action.ActionServlet
 2 要求用戶端發送請求必須遵循*.do的方式
 3 web處理類 extends Action{
  execute(ActionMapping,ActionForm,HttpServletRequest,HttpServletResponse)  {
   //擷取用戶端頁面中的參數
   //req.getParameter("uname");
   MyForm f = (MyForm)form;
   f.getUname();
   //調用業務層的方法
   biz.**();
   //根據返回結果,決定跳轉的目標資源
   return mapping.findForward("邏輯名稱");
  }
 }
 [4 編寫formBean]
 ** extends ActionForm{
  提供私人的屬性,屬性名稱必須和頁面中控制項的名字一樣
  提供上述屬性對應的共有set/get
 }
 5 配置struts-config.xml
 <form-bean name="formBean的邏輯名稱" type="formBean的類路徑">

 <action path="用戶端發送的請求" name="formBean的邏輯名稱" type="action的類路徑">

 path接受資訊,尋找name屬性是否存在,存在,則使用頁面中的form表單中的各個控制項值填充formBean的各個同名屬性值。然後尋找type屬性對應的action資訊.

ForwardAction的作用及編寫方式:
 作用:頁面跳轉頁面,為了符合mvc的核心控制思想
  頁面-----ForwardAction----頁面
 編寫:
  1 頁面:
  <a href="page.do">
  2 struts-config.xml中
  <action path="/page" type="org.apache.struts.actions.ForwardAction"
  parameter="真實資源路徑">

ActionForm在項目的數量:
 0
 1
 n

add.jsp  ----->
   AllForm -----> TestAllFormAction
testAllForm.jsp ----->

ActionForm和頁面的對應關係:
 任意頁面----0個form:
  在Action中擷取參數,只能手動抓取:req.getPrameter();
 一個頁面---1 個form
  在Action中擷取參數,可以手動抓取,也可以通過form擷取
 多個頁面---1 個form
  在Action中擷取參數,可以手動抓取,也可以通過form擷取

struts1的缺陷:
 1 struts1標籤直接耦合了ActionForm
 2 基於struts1架構的web項目,和struts1 api強耦合

注意:
 ActionForm中會儲存曆史頁面中的相關參數資訊
 為了防止資料衝突,則需要在formBean,覆蓋父類ActionForm的方法reset
 在reset方法中,將當前formBean中的所有屬性值設為預設值。
 如:
 @Override
 public void reset(ActionMapping mapping, HttpServletRequest req) {
  this.uname = null;
  this.birthday = null;
  this.stuNo = null;
  this.var1 = 0;
  this.var2 = null;
 }

校正:
 1 用戶端校正 javaScript

 伺服器端校正 ActionForm biz層 

 2 格式校正 javaScript   ActionForm

 業務校正 biz層 

ActionForm的作用:
 1 自動接受用戶端輸入的資料,並且自動類型轉換
 2 針對用戶端提交的資料進行伺服器端格式化校正

ActionForm伺服器端格式化校正的編寫流程:
 1 覆蓋ActionForm父類中的validate方法,將校正規則寫在該方法中
 public ActionErrors validate(ActionMapping mapping, HttpServletRequest req) {
  ActionErrors errors = new ActionErrors();
  if(this.getUname() == ""){
   ActionMessage error = new ActionMessage("error.uname.empty");
   errors.add("", error);
  }
  if(this.getUname().length() > 2){
   ActionMessage error = new ActionMessage("error.uname.length");
   errors.add("", error);
  }
  return errors;
 }

 2 編寫資源檔messageReourse.properties,填寫錯誤描述資訊
 error.uname.empty=/u59d3/u540d/u4e0d/u80fd/u4e3a/u7a7a
 error.uname.length=/u59d3/u540d/u7684/u957f/u5ea6/u5fc5/u987b/u5c0f/u4e8e2

 3 配置資源檔的路徑
 <message-resources parameter="messageResourses"></message-resources>

 4 <action path="請求資訊" name="formBean的邏輯名稱" 
 validate="true" input="驗證失敗跳轉的資源路徑" type="action的類路徑">

 5 在頁面中顯示錯誤描述資訊
 <%@taglib prefix="html" uri="http://struts.apache.org/tags-html" %>
 <html:errors/>

多個商務程序對應同一個action:
 all.do---->AllAction

 add.jsp  ---all.do?name=add
 delete.jsp ---all.do?name=delete
 
 AllAction extends Action{
  execute(mapping,form,req,resp){
   String methodName = req.getParameter("name");
   if(methodName.equals("add")){
    add(mapping,form,req,resp)
   }
   if(methodName.equals("delete")){
    delete(mapping,form,req,resp)
   }
   
  }
  public add(mapping,form,req,resp){
   ...add
  }
  public delete(mapping,form,req,resp){
   ...delete
  }
 } 
 

add.jsp-----/add.do?methodName=add-------------->StuMgmtAction--addStu方法
index.jsp---/findAllStus.do?methodName=findAll-->StuMgmtAction--findAll方法

org.apache.struts.actions.DispatchAction
分發處理類,功能:
 可以將一組相關的業務操作,組織一個類中,便於維護和管理
編寫流程:
 1 web處理類
 StuMgmtAction extends DispatchAction{
  不能覆蓋execute方法
  必須提供多個和execute方法簽名一樣的,業務方法,方法名自訂
  public ActionForward findAll(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
  IStuMgmtBiz biz = new StuMgmtBizImpl();
  Set<Student> stus = null;
  try{
   stus = biz.showAllStudents();
  }catch(Exception e){
   e.printStackTrace();
   return mapping.findForward("errorPage");
  }
  req.setAttribute("stus", stus);
  return mapping.findForward("showStusPage");
  }
  ...
 }

 2 頁面中需要指定一個特殊的參數,該參數的值是為了尋找StuMgmtAction具體的處理方法
  1)operate.do?methodName=addStu
  2)<input type="hidden" name="methodName" value="addStu">
 3 設定檔中,所有的請求只需要配置給同一個處理類
<action path="/operate" type="com.sinojava.stuMgmt.web.dispathAction.StuMgmtAction" parameter="methodName">
 <forward name="showStusPage" path="/WEB-INF/page/showAll.jsp"></forward>
 <forward name="errorPage" path="/WEB-INF/page/error.jsp"></forward>
 <forward name="indexPage" path="/index.jsp"></forward>
</action>
 ============================================
 paramter參數的值 <==> 頁面中指定的特殊參數名
 ============================================
 

 

 

 

 

 

 

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.