Struts1第二天

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參數的值 <==> 頁面中指定的特殊參數名
 ============================================
 

 

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章