随笔-21  评论-29  文章-0  trackbacks-0
模块: 使用Struts增加学生
分析: ①Struts环境
              ②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
流程图:


实践操作实现过程
 先建AddStudentForm,其继承自ActionForm类


可参看Struts API 参考ActionForm的相关信息

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,使其实现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>
部署应用 并访问测试




以上步骤实现了添加学生的基本功能,由于本机没有安装oracle数据库,应用数据库实现模块的设计以后再补充!
posted on 2009-05-02 16:44 特立独行 阅读(342) 评论(0)  编辑  收藏 所属分类: Struts框架

只有注册用户登录后才能发表评论。


网站导航: