struts实现添加学生

使用struts添加学生

模块分析

1.导入jar包,在WEB-INF下建struts-config.xml,在web.xml对ActionServlet完成注册

<servlet>

<servlet-name>addStudent</servlet-name>

<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>addStudent</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>

2.创建AddStudentForm,继承ActionForm ,注册修改配置文件struts-config.xml

<form-beans>
<form-bean name="addStudentForm" type="demo.AddStudentForm"></form-bean>
</form-beans>

3.创建AddStudentAction,继承Action,覆盖execute方法,注册修改配置文件struts-config.xml

<action path="/addStudent" type="demo.AddStudentAction"  name="addStudentForm">

<forward type="addStudentSuccess" path="/addStudentSuccess.jsp"></forward>

<forward type="addStudentFailure" path="/addStudentFailure.jsp"></forward>

</action>

4.创建jsp页面

package demo;

import org.apache.struts.action.ActionForm;

public class AddStudentForm extends ActionForm {
	private String sname;
	private String major;
	private float score;
	private java.sql.Date birth;
	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 float getScore() {
		return score;
	}
	public void setScore(float score) {
		this.score = score;
	}
	public java.sql.Date getBirth() {
		return birth;
	}
	public void setBirth(java.sql.Date birth) {
		this.birth = birth;
	}
	
}
package demo;

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 {
	private StudentDao studentDao=new StudentDao();
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		AddStudentForm addStudentForm=(AddStudentForm)form;
		boolean bool=studentDao.addStudent(addStudentForm);
		if(bool==true) return mapping.findForward("addStudentSuccess");
		else return mapping.findForward("addStudentFailure");
		
	}
	
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foudation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
	<form-beans>
		<form-bean name="addStudentForm" type="demo.AddStudentForm"></form-bean>
	</form-beans>
	<action-mappings>
		<action path="/addStudent" type="demo.AddStudentAction" name="addStudentForm">
			<forward name="addStudentSuccess" path="/addStudentSuccess.jsp"></forward>
			<forward name="addStudentFailure" path="/addStudentFailure.jsp"></forward>
		</action>
	</action-mappings>
</struts-config>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
  	<servlet-name>addStudent</servlet-name>
	<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>  
  
  	<init-param>
		<param-name>config</param-name>
		<param-value>/WEB-INF/struts-config.xml</param-value>
 	 </init-param>
 	 <load-on-startup>0</load-on-startup>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>addStudent</servlet-name>
	<url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
  	<servlet-name>addStudent</servlet-name>
	<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>  
  
  	<init-param>
		<param-name>config</param-name>
		<param-value>/WEB-INF/struts-config.xml</param-value>
 	 </init-param>
 	 <load-on-startup>0</load-on-startup>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>addStudent</servlet-name>
	<url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>
package demo;

import java.sql.SQLException;

import jdbc.CommonUtils;
import jdbc.TxQueryRunner;

import org.apache.commons.dbutils.QueryRunner;

public class StudentDao {
	private QueryRunner qr=new TxQueryRunner();
	public boolean addStudent(AddStudentForm addStudentForm){
		int res;
		try {
			String sid=CommonUtils.uuid();
			String sql="insert into student values(?,?,?,?,?)";
			Object [] params={sid,addStudentForm.getSname(),addStudentForm.getMajor(),
				addStudentForm.getBirth(),addStudentForm.getScore()};		
			res=qr.update(sql,params);			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
		if(res>0) return true;
		else return false;
	}
}






你可能感兴趣的:(struts实现添加学生)