隨筆-21  評論-29  文章-0  trackbacks-0
          模塊: 使用Struts增加學(xué)生
          分析: ①Struts環(huán)境
                        ②AddStudentForm(extends ActionForm)
                              sId,sname, major,  birth,  score
                               <form-bean name="addStudentForm" type="cn.itcast.AddStudentForm">
                              </form-bean>
                        ③AddStudentAction(extends Action)
                              覆蓋execute
                              <action path="/addStudentAction" type="cn.itcast.AddStudentAction" name="addStudentForm">
                                  <forward name="addStudentSuccess" path="/addStudentSuccess.jsp">
                                  <forward name="addStudentFailure" path="/addStudent.jsp">
                              </action>


                            IStudentDAO,StudentDAO
                            addStudent(AddStudentForm  form)
                         ④JSP: addStudent.jsp     addStudentSuccess
          流程圖:


          實(shí)踐操作實(shí)現(xiàn)過程
           先建AddStudentForm,其繼承自ActionForm類


          可參看Struts API 參考ActionForm的相關(guān)信息

          AddStudentForm類的代碼為 (注意使用source里面的Generate Getters and Setters)
          package cn.itcast;

          import org.apache.struts.action.ActionForm;

          public class AddStudentForm extends ActionForm {

              
          private static final long serialVersionUID = 1L;
              
          private String sname = null ;
              
          private String major = null ;
              
          private String score = null ;
              
          private java.sql.Date birth = null ;
              
          public String getSname() {
                  
          return sname;
              }

              
          public void setSname(String sname) {
                  
          this.sname = sname;
              }

              
          public String getMajor() {
                  
          return major;
              }

              
          public void setMajor(String major) {
                  
          this.major = major;
              }

              
          public String getScore() {
                  
          return score;
              }

              
          public void setScore(String score) {
                  
          this.score = score;
              }

              
          public java.sql.Date getBirth() {
                  
          return birth;
              }

              
          public void setBirth(java.sql.Date birth) {
                  
          this.birth = birth;
              }

          }

          在struts-config.xml中添加<form-bean name="addStudentForm" type="cn.itcast.AddStudentForm"></form-bean>


          新建一個(gè)AddStudentAction類,其繼承Action類


          AddStudentAction類覆蓋execute方法 并在struts-config.xml中加入action標(biāo)簽 如下所示






          建立IStudentDAO



          新建StudentDAO,使其實(shí)現(xiàn)IStudentDAO接口 并把StudentDAO返回值改為true



          編寫AddStudentAction類代碼 如下
          package cn.itcast;

          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;

          import org.apache.struts.action.Action;
          import org.apache.struts.action.ActionForm;
          import org.apache.struts.action.ActionForward;
          import org.apache.struts.action.ActionMapping;
          public class AddStudentAction extends Action {
              @Override
              
          public ActionForward execute(ActionMapping mapping, ActionForm form,
                      HttpServletRequest request, HttpServletResponse response)
                      
          throws Exception {        
                  
                    
          //1.cast the form to its subclass
                      AddStudentForm addStudentForm = (AddStudentForm) form ;
                      
                    
          //2.invoke the IStudentDAO module
                      IStudentDAO studentDAO = new StudentDAO();
                      
          boolean successful = false ;
                      successful 
          = studentDAO.addStudent(addStudentForm);
                      
                    
          //3.forward to next module by the result of the business logic
                      String returnURLKeyWord = "addStudentFailure";
                      
          if(successful == true){
                          returnURLKeyWord 
          = "addStudentSuccess";
                      }

                      
                      
          return mapping.findForward(returnURLKeyWord);
              }


          }

          新建兩個(gè)JSP頁面 AddStudent.jsp  和 AddStudentSuccess.jsp
          <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
              pageEncoding
          ="ISO-8859-1"
          %>
          <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
          <html>
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
          <title>Insert title here</title>
          </head>
          <body>
              
          <form action="<%= request.getContextPath()%>/addStudentAction.do" method="get">
                 sname:
          <input type="text" name="sname"><br>
                 major:
          <input type="text" name="major"><br>
                 birth:
          <input type="text" name="birth"><br>
                 score:
          <input type="text" name="score"><br>
                 
          <input type="submit" value="add">
              
          </form>
          </body>
          </html>
          <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
              pageEncoding
          ="ISO-8859-1"
          %>
          <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
          <html>
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
          <title>Insert title here</title>
          </head>
          <body>
             Succeed in adding a student!!!
          </body>
          </html>
          部署應(yīng)用 并訪問測試




          以上步驟實(shí)現(xiàn)了添加學(xué)生的基本功能,由于本機(jī)沒有安裝oracle數(shù)據(jù)庫,應(yīng)用數(shù)據(jù)庫實(shí)現(xiàn)模塊的設(shè)計(jì)以后再補(bǔ)充!
          posted on 2009-05-02 16:44 特立獨(dú)行 閱讀(358) 評論(0)  編輯  收藏 所屬分類: Struts框架
          主站蜘蛛池模板: 剑阁县| 漾濞| 长白| 天全县| 五台县| 滨海县| 双流县| 株洲市| 方城县| 东阿县| 新干县| 兴国县| 天台县| 香港 | 册亨县| 长春市| 甘孜| 竹溪县| 林芝县| 崇明县| 兰州市| 阿拉善盟| 彰化县| 明溪县| 马尔康县| 江山市| 平凉市| 河西区| 北票市| 桐庐县| 马尔康县| 米脂县| 平凉市| 扶风县| 攀枝花市| 康乐县| 镇安县| 什邡市| 上虞市| 鄂伦春自治旗| 全南县|