📄 studentaction.java
字号:
/**
*
*/
package edu.yinhe.mis.control;
import java.io.BufferedInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import edu.yinhe.mis.dto.DictionaryDTO;
import edu.yinhe.mis.dto.StudentDTO;
import edu.yinhe.mis.dto.TeacherDTO;
import edu.yinhe.mis.util.StudentExcel;
import edu.yinhe.mis.vo.StudentVO;
import edu.yinhe.system.common.AppException;
import edu.yinhe.system.control.BaseAction;
import edu.yinhe.system.vo.PaginationVO;
/**
* @author 吴朋
* @cation date:04-30-2008
* @struts.form name="StudentForm"
*/
public class StudentAction extends BaseAction{
/**
* 增加学生信息
* @author 吴朋
* @param mapping 响应转发请求
* @param form
* @param request 请求响应
* @param response 响应结果
* @return
* @throws AppException 应用程序异常
*/
public ActionForward add(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ActionForward forward=mapping.getInputForward();
forward=mapping.findForward("addstudent");
return forward;
}
/**
* 增加学生信息
* @author 吴朋
* @param mapping 响应转发请求
* @param form
* @param request 请求响应
* @param response 响应结果
* @return
* @throws AppException 应用程序异常
*/
public ActionForward addStudent(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
StudentForm stuform=(StudentForm)form;
ActionForward forward=mapping.getInputForward();
StudentDTO sdto=new StudentDTO();
Boolean flag=false;
try {
BeanUtils.copyProperties(sdto, stuform);
sdto.filer();
sdto.setStuState("0");
service.setDataSource(this.getDataSource(request,"mydatasource"));
flag= (Boolean) service.create(sdto);
if(flag){
forward=mapping.findForward("addsuccess");
}
} catch (AppException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return forward;
}
/**
* 增加学生信息(用Excel导入)
* @author 吴朋
* @param mapping 响应转发请求
* @param form
* @param request 请求响应
* @param response 响应结果
* @return
* @throws AppException 应用程序异常
*/
public ActionForward addExcel(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ActionForward forward=mapping.getInputForward();
StudentForm stuform=(StudentForm)form;
FormFile formFile=stuform.getStufile();
String fileName=formFile.getFileName();
System.out.print(fileName);
//Configuration conf=new Configuration();
fileName=fileName.substring(fileName.lastIndexOf("."));
fileName=System.currentTimeMillis()+fileName;
//String savePath=conf.getProperty("uploadSavePath");
fileName="c:\\"+fileName;
System.out.println(fileName+"******************************************");
try {
FileOutputStream fout=new FileOutputStream(fileName);
BufferedInputStream fin=new BufferedInputStream(formFile.getInputStream());
//FileInputStream fin=formFile.getInputStream();
byte[] bytes=new byte[1024*2];
int i=0;
while((i=fin.read(bytes, 0, bytes.length))!=-1){
fout.write(bytes, 0, i);
}
fout.flush();
fout.close();
fin.close();
//读文件并采集数据,装入到List集合中;
//插入到数据库中,
StudentExcel excel = new StudentExcel();
ArrayList list = excel.readExcel(fileName);// 把excel文件解析出来封装在ArrayList里
// System.out.println(list.size() + "list.size()");
StudentDTO dto = null;
for (int j = 0; j < list.size(); j++) {
dto = (StudentDTO) list.get(j);
boolean flag=false;
try {
service.setDataSource(getDataSource(request,"mydatasource"));
flag = (Boolean) service.create(dto);
} catch (AppException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}// 把数据插入到数据库的表excel里
if (flag) {
forward=mapping.findForward("addsuccess");
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return forward;
}
/**
* 查看学生信息
* @author 吴朋
* @param mapping 响应转发请求
* @param form
* @param request 请求响应
* @param response 响应结果
* @return
* @throws AppException 应用程序异常
*/
public ActionForward queryStudent(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
StudentForm stuform=(StudentForm)form;
ActionForward forward=mapping.getInputForward();
String type = request.getParameter("type");
String currentPage=request.getParameter("currentPage");
ArrayList arraylist=null;
Object[] objs=null;
try {
PaginationVO pvo=new PaginationVO();
StudentDTO sdto=new StudentDTO();;
BeanUtils.copyProperties(sdto, stuform);
service.setDataSource(this.getDataSource(request,"mydatasource"));
if(stuform.getStuName()!=null&&"".equals(sdto.getStuName())){
sdto.setStuName(URLDecoder.decode(stuform.getStuName()));
}
if(type==null||type.equals("")||type.equals("0")){
currentPage="1";
}else if(type.equals("1")){
currentPage=String.valueOf((Integer.parseInt(sdto.getCurrentPage())-1));
}else if(type.equals("2")){
currentPage=String.valueOf((Integer.parseInt(sdto.getCurrentPage())+1));
}else if(type.equals("3")){
currentPage=request.getParameter("allPage");
}
pvo.setCurrentPage(Integer.parseInt(currentPage));
sdto.setRowPerPage("9");
sdto.setCurrentPage(String.valueOf(pvo.getCurrentPage()));
objs=(Object[])service.alllist(sdto);
int count= ((Integer)objs[1]).intValue();
pvo.setResultsNumber(count);
pvo.setRowsPerPage(9);
pvo.setAllPages((count/pvo.getRowsPerPage())+(count%pvo.getRowsPerPage()>0?1:0));
arraylist=(ArrayList) objs[0];
if(arraylist.size()!=0){
request.setAttribute("arraylist", arraylist);
request.setAttribute("pvo", pvo);
request.setAttribute("sdto", sdto);
forward=mapping.findForward("queryStudent");
}else{
request.setAttribute("arraylist", arraylist);
request.setAttribute("pvo", pvo);
request.setAttribute("sdto", sdto);
forward=mapping.findForward("error");
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (AppException e) {
e.printStackTrace();
}
return forward;
}
/**
* 批量删除学生信息
* @author 吴朋
* @param mapping 响应转发请求
* @param form
* @param request 请求响应
* @param response 响应结果
* @return
* @throws AppException 应用程序异常
*/
public ActionForward delStudent(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ActionForward forward=mapping.getInputForward();
Object []id=request.getParameterValues("sid");
boolean flag=false;
try {
service.setDataSource(this.getDataSource(request,"mydatasource"));
flag=(Boolean) service.remove(id);
if(flag){
forward=new ActionForward("/admins/core/student.html?method=queryStudent");
}
} catch (AppException e) {
e.printStackTrace();
}
return forward;
}
/**
* 根据ID号查看学生信息
* @author 吴朋
* @param mapping 响应转发请求
* @param form
* @param request 请求响应
* @param response 响应结果
* @return
* @throws AppException 应用程序异常
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -