Struts 2.0 学生成绩管理系统

 这里以对学生信息的操作为例:

1.配置文件

 struts.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<package name="default" extends="struts-default">
		<!-- 添加学生信息 -->
		<action name="addStudent" class="org.action.StudentAction">
			<result name="success">/addStudent_success.jsp</result>
			<result name="error">/addStudent.jsp</result>
			<result name="input">/addStudent.jsp</result>
		</action>
		
		<!--查询所有学生  -->
		<action name="showAllStudent" class="org.action.StudentAction" method="showAllStudent">
			<result name="success">/showStudent.jsp</result>
			<result name="error">/error.jsp</result>
			<result name="input">/error.jsp</result>
		</action>
		
		<!--查询一个学生  -->
		<action name="showOneStudent" class="org.action.StudentAction" method="showOneStudent">
			<result name="success">/showOneStudent.jsp</result>
			<result name="error">/error.jsp</result>
		</action>
		<action name="getImage" class="org.action.StudentAction" method="getImage"></action>
		
		<!-- 删除学生 -->
		<action name="deleteStudent" class="org.action.StudentAction" method="deleteStudent">
			<result name="success">/showAllStudent.jsp</result>
			<result name="error">/error.jsp</result>
			<result name="input">/error.jsp</result>
		</action>
		
		<!-- 查询要更新的学生信息 -->
		<action name="updateStudent" class="org.action.StudentAction" method="showOneStudent">
			<result name="success">/updateStudent.jsp</result>
			<result name="error">/error.jsp</result>
			<result name="input">/error.jsp</result>
		</action>
		<!-- 更新学生信息 -->
		<action name="updateSaveStudent" class="org.action.StudentAction" method="updateSaveStudent">
			<result name="success">/showAllStudent.jsp</result>
			<result name="error">/error.jsp</result>
			<result name="input">/error.jsp</result>
		</action>
	</package>
	<constant name="struts.multipart.saveDir" value="/tmp"></constant>
</struts>

2.前段部分页面代码

 main.jsp

<%@ page language="java" pageEncoding="UTF-8"%>
<html>
<head>
	<title>学生成绩管理系统</title>
</head>
<frameset rows="20%,65%,15%" border="0">
	<frame src="head.jsp" name="head">
	<frameset cols="17%,*">
		<frame src="left.jsp" name="left">
		<frame src="right.jsp" name="right">
	</frameset>
	<frame src="foot.jsp" >
</frameset>
</html>

left.jsp

<%@ page language="java" pageEncoding="utf-8"%>
<html>
<body background="image/main.gif" vlink="dfd9aa" link="dfd9aa">
  <img src="image/xsInfo.gif">
  <a href="addStudent.jsp" target="right">
	  <img src="image/xsAdd.gif">
  </a>
 <a href="showAllStudent.jsp" target="right">
	 <img src="image/xsSelect.gif"> 
 </a>
 <img src="image/kcInfo.gif">
 <img src="image/kcAdd.gif">
 <img src="image/kcSelect.gif">
 <img src="image/cjInfo.gif">
 <a href="showAllScore.jsp" target="right">
	<img src="image/cjAdd.gif">
 </a>
 <a href="searchScore.jsp" target="right">
	<img src="image/cjSelect.gif">
 </a>
</body>
</html>

showStudent.jsp

<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<body background="image/bgcolor1.jpg">
	<h2>学生信息情况</h2>
	<hr>
	<table border="1">
	<tr align="center">
		<td>学号</td>
		<td>姓名</td>
		<td>性别</td>
		<td>专业</td>
		<td>出生日期</td>
		<td>总学分</td>
		<td>备注</td>
		<td>详细信息</td>
		<td colspan ="2">操作</td>
	</tr>
	<s:iterator value="#request.studentList" id="xs">
	<tr>
		<td><s:property value="#xs.xh"/></td>
		<td><s:property value="#xs.xm"/></td>
		<td>
			<s:if test="#xs.xb==1">男</s:if>
			<s:else>女</s:else>
		</td>
		<td>
			<s:property value="#xs.zy"/>
		</td>
		<td>
			<s:date name="#xs.cssj" format="yyyy-MM-dd"/>
		</td>
		<td>
			<s:property value="#xs.zxf"/>
		</td>
		<td>
			<s:property value="#xs.bz"/>
		</td>
		<td>
			<a href="showOneStudent.action?student.xh=<s:property value="#xs.xh"/>">详细信息</a>
		</td>
		<td>
			<a href="deleteStudent.action?student.xh=<s:property value="#xs.xh"/>" onClick="if(!confirm('确定删除该信息吗?'))return false;else return true;">删除</a>
		</td>
		<td>
			<a href="updateStudent.action?student.xh=<s:property value="#xs.xh"/>">修改</a>
		</td>
	</tr>
	</s:iterator>
	</table>
	<p>
</body>
</html>

showAllStudent.jsp

<%@ page language="java" contentType="text/html;charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<body>
	<s:action name="showAllStudent" executeResult="true"/>
</body>
</html>

showOneStudent.jsp

<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<body background="image/bgcolor1.jpg">
    <h3>该学生信息如下:</h3>
    <s:set name="xs" value="#request.student1"></s:set>
    <table border="0">
      <tr>
		<td>学号:</td>
		<td width="400"><s:property value="#xs.xh"/></td>
	  </tr>
	  <tr>
		 <td>姓名:</td>
		 <td width="400"><s:property value="#xs.xm"/></td>
	  </tr>
	  <tr>
		 <td>性别:</td>
		 <td width="400">
			<s:if test="#xs.xb==1">男</s:if>
			<s:else>女</s:else>
		 </td>
	  </tr>
	  <tr>
		 <td>专业:</td>
		 <td width="400"><s:property value="#xs.zy"/></td>
	  </tr>
	  <tr>
		  <td>出生日期:</td>
		  <td width="400"><s:date name="#xs.cssj" format="yyyy-MM-dd"/></td>
	  </tr>
	  <tr>
		  <td>总学分</td>
		  <td width="400"><s:property value="#xs.zxf"/></td>
	  </tr>
	  <tr>
		  <td>备注</td>
		  <td width="400"><s:property value="#xs.bz"/></td>
	  </tr>
	  <tr>
	    <td>照片:</td>
		<td width="400"> <img src="getImage.action?student.xh=<s:property value="#xs.xh"/>" width="150"></td>
	  </tr>
    </table>
    <input type="button" value="返回" onClick="javaScript:history.back()" />
</body>
</html>

updateStudent.jsp

<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<body background="image/bgcolor1.jpg">
	<center>
	<h3>该学生信息</h3>
	<hr>
	<s:set name="xs" value="#request.student1"></s:set>
	<form action="updateSaveStudent.action" method="post" enctype="multipart/form-data" >
		<table border="1" width="400">
		<tr>
			<td>学号:</td> 
			<td>
				<input type="text" name="student.xh" value="<s:property value="#xs.xh"/>" readonly/>
			</td>
		</tr>
		<tr>
			<td>姓名:</td>
			<td>
				<input type="text" name="student.xm" value="<s:property value="#xs.xm"/>"/>
			</td>
		</tr>
		<tr>
			<s:radio list="#{1:'男',0:'女'}" value="#xs.xb" label="性别" name="student.xb"></s:radio>
		</tr>
		<tr>
			<td>专业:</td>
			<td>
				<input type="text" name="student.zy" value="<s:property value="#xs.zy"/>"/>
			</td>
		</tr>
		<tr>
			<td>出生日期:</td>
			<td>
				<input type="text" name="student.cssj" value="<s:date name="#xs.cssj" format="yyyy-MM-dd"/>"/>
			</td>
		</tr>
		<tr>
			<td>总学分:</td>
			<td>
				<input type="text" name="student.zxf" value="<s:property value="#xs.zxf"/>"/>
			</td>
		</tr>
		<tr>
			<td>备注:</td>
			<td>
				<input type="text" name="student.bz" value="<s:property value="#xs.bz"/>"/>
			</td>
		</tr>
		<tr>
			<td>照片</td>
			<td>
				<input type="file" name="zpfile" value=""/>
			</td>
		</tr>
		</table>
		<p>
		<input type="submit" value="修改"/>
		<input type="button" value="返回" onclick="javascript:history.back();"/>
	</form>
	</center>
</body>
</html>

addStudent.jsp

<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<body background="image/bgcolor1.jpg">
    <h3>请填写学生信息</h3>
    <form action="addStudent.action" method="post" enctype="multipart/form-data">
      <table border="1">
		<tr>
			<s:textfield name="student.xh" label="学号"></s:textfield>
		</tr>
		<tr>
			<s:textfield name="student.xm" label="姓名"></s:textfield>
		</tr>
		<tr>
			<s:textfield name="student.zy" label="专业名"></s:textfield>
		</tr>
		<tr>
			<s:radio name="student.xb" value="1" list="#{1:'男',0:'女'}" label="性别" />
		</tr>
		<tr>
			<s:textfield name="student.cssj" label="出生日期"></s:textfield>
		</tr>
		<tr>
			<s:textfield name="student.zxf" label="总学分"></s:textfield>
		</tr>
		<tr>
			<s:file name="zpfile" label="照片"></s:file>
		</tr>
		<tr>
			<s:textarea name="student.bz" label="备注"></s:textarea>
		</tr>
		</table>
		<p>
		<input type="submit" value="添加"/>
		<input type="reset" value="重置"/>
    </form>
</body>
</html>

addStudent_success.jsp

<%@ page language="java" pageEncoding="gb2312"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<body background="image/bgcolor1.jpg">学生信息添加成功
	<a href="addStudent.jsp">继续添加学生</a>
</body>
</html>
 

3.后端逻辑实现

  (1)数据库的操作

DBConn.java

package org.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBConn {
	private Connection conn=null;

	public DBConn() {
		this.conn=this.GetConnection();
		// TODO Auto-generated constructor stub
	}
	
	/*
	 * 获取连接类
	 */
	public Connection GetConnection(){
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/xscj?user=root&password=123456&useUnicode=true&characterEncoding=UTF-8");
			System.out.println("数据库连接成功!");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("数据库连接失败!");
			e.printStackTrace();
		} 
		return conn;
	}
	
	public Connection getConn(){
		return conn;
	}

}

StudentJdbc.java

package org.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.vo.Student;


public class StudentJdbc {
  private Connection conn=null;
  private PreparedStatement psmt=null;
  private ResultSet rs=null;
  public StudentJdbc() {
	// TODO Auto-generated constructor stub
}

  /*
   * 获取数据库连接
   */
  public Connection getConn(){
	  try {
		if(this.conn==null || this.conn.isClosed()){
			 DBConn mc=new DBConn();  //创建数据库连接类 
			 this.conn=mc.getConn();  //获取Connection对象
		  }
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	  return conn;
  }
  
   /*
    * 添加学生类
    */
  public Student addStudent(Student student){
	  String sql="insert into xs values(?,?,?,?,?,?,?,?)";//预编译语句
	  try {
		  psmt = this.getConn().prepareStatement(sql);
			psmt.setString(1, student.getXh());
			psmt.setString(2, student.getXm());
			psmt.setString(3, student.getZy());
			psmt.setByte(4, student.getXb());
			psmt.setTimestamp(5, new Timestamp(student.getCssj().getTime()));
			psmt.setInt(6, student.getZxf());
			psmt.setBytes(7, student.getZp());
			psmt.setString(8, student.getBz());
			psmt.execute();
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally {
		try {
			if(psmt != null) {
				psmt.close();				// 关闭PreparedStatement对象
				psmt = null;
			}
			if(conn != null) {
				conn.close();				// 关闭Connection对象
				conn = null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	  return student;    //返回Student对象给Action
  }
  
  /*
   * 查询所有学生
   */
  public List showStudent() throws SQLException {
		String sql = "select * from xs";
		// 创建一个ArrayList容器,将从数据库中查询的学生信息存放在容器中
		List studentList = new ArrayList();		
		try {
			psmt = this.getConn().prepareStatement(sql);
			rs = psmt.executeQuery();
			while (rs.next()) {
				Student student = new Student();
				//注意对应字段名
				student.setXh(rs.getString("xh"));
				student.setXm(rs.getString("xm"));
				student.setXb(rs.getByte("xb"));
				student.setCssj(rs.getDate("cssj"));
				student.setZy(rs.getString("zy"));
				student.setZxf(rs.getInt("zxf"));
				student.setBz(rs.getString("bz"));
				student.setZp(rs.getBytes("zp"));
				studentList.add(student);
			}
			return studentList;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(rs != null) {
					rs.close();					// 关闭ResultSet对象
					rs = null;
				}
				if(psmt != null) {
					psmt.close();				// 关闭PreparedStatement对象
					psmt = null;
				}
				if(conn != null) {
					conn.close();				// 关闭Connection对象
					conn = null;
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return studentList;
	}
  
  public Student showOneStudent(String xh) {
		ResultSet rs = null;
		String sql = "select * from xs where xh=" + xh;
		Student student = new Student();
		try {
			psmt = this.getConn().prepareStatement(sql);
			rs = psmt.executeQuery();
			while (rs.next()) {
				student.setXh(rs.getString("xh"));
				student.setXm(rs.getString("xm"));
				student.setXb(rs.getByte("xb"));
				student.setCssj(rs.getDate("cssj"));
				student.setZy(rs.getString("zy"));
				student.setZxf(rs.getInt("zxf"));
				student.setBz(rs.getString("bz"));
				student.setZp(rs.getBytes("zp"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				psmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return student;
	}
	
  /*
   * 删除一个学生
   */
  public void deleteStudent(String xh) {
		String sql = "delete from xs where xh=" + xh;
		System.out.println(xh + "studentjdbc");
		try {
			psmt = this.getConn().prepareStatement(sql);
			psmt.execute();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				psmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
  
  /*
   * 更新一个学生的信息
   */
  public Student updateSaveStudent(Student student) {
		String sql = "update xs set xh=?,xm=?,zy=?,xb=?,cssj=?,zxf=?,zp=?,bz=? where xh=" + student.getXh();
		try {
			psmt = this.getConn().prepareStatement(sql);
			psmt.setString(1, student.getXh());
			psmt.setString(2, student.getXm());
			psmt.setString(3, student.getZy());
			psmt.setByte(4, student.getXb());
			System.out.println(student.getCssj());
			psmt.setTimestamp(5, new Timestamp( student.getCssj().getTime()));
			psmt.setInt(6, student.getZxf());
			psmt.setBytes(7, student.getZp());
			psmt.setString(8, student.getBz());
			psmt.execute();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				psmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return student;
	}
  
	
}

(2)实体类

Student.java

package org.vo;

import java.io.Serializable;
import java.util.Date;

public class Student implements Serializable {
	private String xh;     //学号
	private String xm;     //姓名
	private String zy;     //专业名
	private byte xb;       //性别
	private Date cssj;     //出生日期
	private int zxf;       //总学分
	private byte[] zp;     //照片,字节数组
	private String bz;     //备注
	public Student() {
		// TODO Auto-generated constructor stub
	}
	
	public Student(String xm, String zy, byte xb, Date cssj, int zxf, byte[] zp, String bz) {
		super();
		this.xm = xm;
		this.zy = zy;
		this.xb = xb;
		this.cssj = cssj;
		this.zxf = zxf;
		this.zp = zp;
		this.bz = bz;
	}

	public String getXh() {
		return xh;
	}

	public void setXh(String xh) {
		this.xh = xh;
	}

	public String getXm() {
		return xm;
	}

	public void setXm(String xm) {
		this.xm = xm;
	}

	public String getZy() {
		return zy;
	}

	public void setZy(String zy) {
		this.zy = zy;
	}

	public byte getXb() {
		return xb;
	}

	public void setXb(byte xb) {
		this.xb = xb;
	}

	public Date getCssj() {
		return cssj;
	}

	public void setCssj(Date cssj) {
		this.cssj = cssj;
	}

	public int getZxf() {
		return zxf;
	}

	public void setZxf(int zxf) {
		this.zxf = zxf;
	}

	public byte[] getZp() {
		return zp;
	}

	public void setZp(byte[] zp) {
		this.zp = zp;
	}

	public String getBz() {
		return bz;
	}

	public void setBz(String bz) {
		this.bz = bz;
	}
	

}

(3)控制类

StudentAction.java

package org.action;

import java.io.File;
import java.io.FileInputStream;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.jdbc.StudentJdbc;
import org.vo.Student;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class StudentAction extends ActionSupport {
  private Student student;
  public File zpfile;
public Student getStudent() {
	return student;
}
public void setStudent(Student student) {
	this.student = student;
}
public File getZpfile() {
	return zpfile;
}
public void setZpfile(File zpfile) {
	this.zpfile = zpfile;
}

private List<Student> studentList;
public String execute() throws Exception {
	// TODO Auto-generated method stub
	StudentJdbc studentJ=new StudentJdbc();
	student.setXh(student.getXh());
	student.setXm(student.getXm());
	student.setZy(student.getZy());
	student.setXb(student.getXb());
	student.setCssj(student.getCssj());
	student.setZxf(student.getZxf());
	if(this.getZpfile()!=null){
		//创建文件输入流,用于读取图片内容
		FileInputStream fis=new FileInputStream(this.getZpfile());
		//创建字节类型的数组,用于存放图片的二进制数据
		byte []buffer=new byte[fis.available()];
		fis.read(buffer);//将图片内容读入到字节数组中
		student.setZp(buffer);
	}
	student.setBz(student.getBz());
	studentJ.addStudent(student);
	return SUCCESS;
}

/*
 *查询所有 学生
 */
public String showAllStudent() {
	Student student1 = new Student();
	StudentJdbc studentJ = new StudentJdbc();
	try {
		studentList = studentJ.showStudent();
	} catch (SQLException e) {
		e.printStackTrace();
	}
	Map request = (Map) ActionContext.getContext().get("request");
	request.put("studentList", studentList);
	return SUCCESS;
}

/*
 * 查询一个学生信息
 */
public String showOneStudent() throws Exception{
	Student student1=new Student();
	StudentJdbc studentJ=new StudentJdbc();
	student1=studentJ.showOneStudent(student.getXh());//查询一个学生信息
	Map request=(Map) ActionContext.getContext().get("request");
	request.put("student1", student1);//将查询的学生信息放到Map容器中
	return SUCCESS;   
}

/*
 * 获取图片信息
 */
 public String getImage() throws Exception {
	 HttpServletResponse response=ServletActionContext.getResponse();
	 Student student1=new Student();
	 StudentJdbc studentJ=new StudentJdbc();
	 String xh=student.getXh();
	 student1=studentJ.showOneStudent(xh);//查询一个学生的信息
	 byte []img=student1.getZp();//获取照片信息
	 response.setContentType("image/jpeg");//制定Http响应的编码
     ServletOutputStream os = response.getOutputStream();//返回一个输出流
     if(img!=null && img.length!=0){
    	 for(int i=0;i<img.length;i++){
    		 os.write(img[i]);
    	 }
     }
     return NONE;
 }
 
 /*
  * 删除一个学生信息
  */
 public String deleteStudent() throws Exception {
		Student student1 = new Student();
		StudentJdbc studentJ = new StudentJdbc();
		studentJ.deleteStudent(student.getXh());
		System.out.println("学号为"+student.getXh() + "的学生信息已经被删除!");
		return SUCCESS;
	}
 
 /*
  * 更新一个学生信息
  */
 public String updateSaveStudent() throws Exception {
		StudentJdbc studentJ = new StudentJdbc();
		student.setXh(student.getXh());
		student.setXm(student.getXm());
		student.setXb(student.getXb());
		student.setCssj(student.getCssj());
		student.setZy(student.getZy());
		student.setZxf(student.getZxf());
		student.setBz(student.getBz());
		if (this.getZpfile() != null) {
			FileInputStream fis = new FileInputStream(this.getZpfile());
			byte[] buffer = new byte[fis.available()];
			fis.read(buffer);
			student.setZp(buffer);
		}
		studentJ.updateSaveStudent(student);
		return SUCCESS;
	}
}

4.部分功能截图


                                        学生信息录入



                                               学生信息查询



                                                 学生详细信息



                                                       修改学生信息



                                                                                                                       学生成绩录入


总结:剩下的课程信息管理和成绩信息管理两大模块皆参照学生信息管理模块的思想来做,代码几乎雷同。在学了数据库连接池后,再次使用基本的JDBC来做项目,果然轻松。本次项目主要是为了实现数据库设计而做的项目,所以仅仅为了做功能,不考虑数据库连接的用户数和连接高峰期的处理等复杂问题。

你可能感兴趣的:(java,spring,管理,javabean,struts2.0)