隨筆-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
          流程圖:


          實踐操作實現(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>


          新建一個AddStudentAction類,其繼承Action類


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






          建立IStudentDAO



          新建StudentDAO,使其實現(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);
              }


          }

          新建兩個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)用 并訪問測試




          以上步驟實現(xiàn)了添加學(xué)生的基本功能,由于本機沒有安裝oracle數(shù)據(jù)庫,應(yīng)用數(shù)據(jù)庫實現(xiàn)模塊的設(shè)計以后再補充!
          posted on 2009-05-02 16:44 特立獨行 閱讀(361) 評論(0)  編輯  收藏 所屬分類: Struts框架
          主站蜘蛛池模板: 张家港市| 常州市| 汉阴县| 富源县| 康马县| 蒙自县| 孟州市| 丁青县| 菏泽市| 美姑县| 南投县| 新巴尔虎右旗| 长葛市| 民勤县| 四子王旗| 盐源县| 蓬安县| 驻马店市| 南陵县| 芜湖市| 天气| 冷水江市| 西乌珠穆沁旗| 肇州县| 蛟河市| 察雅县| 德州市| 广丰县| 新和县| 营山县| 平山县| 四川省| 于田县| 东辽县| 文登市| 隆化县| 磴口县| 潞城市| 阿巴嘎旗| 化德县| 友谊县|