基于javaweb+mysql的ssm在线考试系统(java+ssm+jsp+js+mysql)
私信源码获取及调试交流
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于javaweb的SSM在线考试系统(java+ssm+jsp+js+mysql)
管理员:
admin 123456
老师
1001 123456
1002 123456
学生
2019001 123456
2019002 123456
2019003 123456
Arrays.sort(c1);
Arrays.sort(c2);
for (int i = 0; i < c1.length; i++) {
if (c1[i] != c2[i])
return false;
}
return true;
}
@RequestMapping(value = "/anlExamScore.do")
public String anlExamScore(Integer uid) {
try {
List list = courseService.selectAll(Course.class, MapUtil.createMap("teacherId", getTeacher().getId()));
putRequestValue("list", list);
if (uid == null) {
return "anlExamScore";
}
Course course = courseService.get(uid);
putRequestValue("course", course);
Long count = studentExamService.getScoreCount(uid, 0, 60);
putRequestValue("count1", count = count == null ? 0 : count);
count = studentExamService.getScoreCount(uid, 60, 70);
putRequestValue("count2", count = count == null ? 0 : count);
count = studentExamService.getScoreCount(uid, 70, 90);
putRequestValue("count3", count = count == null ? 0 : count);
count = studentExamService.getScoreCount(uid, 90, 100);
putRequestValue("count4", count = count == null ? 0 : count);
} catch (Exception e) {
e.printStackTrace();
}
return "anlExamScore";
}
@RequestMapping(value = "/finishExam.do")
public String finishExam(int uid) {
try {
Paper paper1 = (Paper) getSessionValue("paper");
if (paper1 == null) {
getHttpSession().setAttribute("VoteErrorMessage", "提交答卷失败");
public String add(Course bean) {
try {
service.add(bean);
MessageUtil.addMessage(request, "添加成功.");
return SUCCESS;
} catch (Exception e) {
e.printStackTrace();
MessageUtil.addMessage(request, "添加失败.");
return ERROR;
}
}
@RequestMapping(value = "/updateCourse.do")
public String update(Course bean) {
try {
service.update(bean);
MessageUtil.addMessage(request, "更新成功.");
return SUCCESS;
} catch (Exception e) {
e.printStackTrace();
MessageUtil.addMessage(request, "更新失败.");
return ERROR;
}
}
@RequestMapping(value = "/queryCourse.do")
public String query() {
try {
// 字段名称集合
LinkedList<String> parmnames = new LinkedList<String>();
// 字段值集合
LinkedList<Object> parmvalues = new LinkedList<Object>();
Page p = FieldUtil.createPage(request, Course.class, parmnames, parmvalues);
Page page = service.selectPage(p, Course.class);
session.setAttribute(Constant.SESSION_PAGE, page);
request.setAttribute("actionname", actionname);
request.setAttribute("actionclass", actionclass);
}
}
}
@Controller
@RequestMapping("/sys")
public class StudentExamAction extends BaseAction {
private String actionname = "考试";
private String actionclass = "StudentExam";
@Autowired
private StudentExamService service;
@RequestMapping(value = "/add2StudentExam.do")
public String add2() {
request.setAttribute("actionname", actionname);
request.setAttribute("actionclass", actionclass);
return "addStudentExam";
}
@RequestMapping(value = "/deleteStudentExam.do")
public String delete(String ids) {
try {
service.deleteAll(ids);
MessageUtil.addRelMessage(request, "操作成功.", "mainquery");
return SUCCESS;
} catch (Exception e) {
e.printStackTrace();
MessageUtil.addMessage(request, "操作失败.");
return ERROR;
}
}
}
if (getSessionUser() instanceof SimpleUser) {
MenuBean m1 = new MenuBean("考试管理","star");
m1.addChild(new SubMenu("navtab" + (index++), "我的科目", "navtab", request.getContextPath() + "/sys/queryExamCourse.do"));
m1.addChild(new SubMenu("navtab" + (index++), "我的考试", "navtab", request.getContextPath() + "/sys/queryStudentExam.do"));
list.add(m1);
}
return list;
}
@RequestMapping(value = "/login.do")
public String login(String username, String password, String loginType, String checkcode) throws Exception {
log.info(username + " " + password + " " + loginType);
String errorMessage = "登录失败,请稍候重试";
try {
String code = (String) session.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
if (code == null) {
request.getSession(false).invalidate();
return "redirect:/index.jsp";
} else {
}
checkcode = StringUtil.stringVerification(checkcode);
do {
if (!code.toLowerCase().equals(checkcode.toLowerCase())) {
errorMessage = "验证码错误";
break;
}
username = StringUtil.stringVerification(username).toLowerCase();
MD5 md = new MD5();
password = md.getMD5ofStr(password);
Object bean = sysUserService.login(username, password, loginType);
if (bean == null) {
errorMessage = "账号或者密码错误";
break;
}
SessionBean sb = new SessionBean();
sb.setUser(bean);
session.setAttribute(Constant.SESSION_BEAN, sb);
if (loginType.equals(SimpleUser.class.getSimpleName())) {
return "main";
}
return "main";
} while (false);
} catch (Exception e) {
e.printStackTrace();
}
session.setAttribute("signErrorMessage", errorMessage);
return "redirect:/index.jsp";
colWith = cellWith;
}
}
// 设置单元格的宽度为最宽宽度+额外宽度
ws.setColumnView(i, colWith + extraWith);
}
}
/**
* @MethodName : fillSheet
* @Description : 向工作表中填充数据
* @param sheet
* 工作表
* @param list
* 数据源
* @param fieldMap
* 中英文字段对应关系的Map
* @param firstIndex
* 开始索引
* @param lastIndex
* 结束索引
*/
private static <T> void fillSheet(WritableSheet sheet, List<T> list, LinkedHashMap<String, String> fieldMap, int firstIndex,
int lastIndex) throws Exception {
// 定义存放英文字段名和中文字段名的数组
String[] enFields = new String[fieldMap.size()];
String[] cnFields = new String[fieldMap.size()];
// 填充数组
int count = 0;
for (Entry<String, String> entry : fieldMap.entrySet()) {
enFields[count] = entry.getKey();
cnFields[count] = entry.getValue();
count++;
}
// 填充表头
for (int i = 0; i < cnFields.length; i++) {
Label label = new Label(i, 0, cnFields[i]);
sheet.addCell(label);
}
// 填充内容
int rowNo = 1;
for (int index = firstIndex; index <= lastIndex; index++) {
// 获取单个对象
T item = list.get(index);
for (int i = 0; i < enFields.length; i++) {
Object objValue = getFieldValueByNameSequence(enFields[i], item);
String fieldValue = objValue == null ? "" : objValue.toString();
Label label = new Label(i, rowNo, fieldValue);
sheet.addCell(label);
}
}
@RequestMapping(value = "/queryPaper.do")
public String query() {
try {
// 字段名称集合
LinkedList<String> parmnames = new LinkedList<String>();
// 字段值集合
LinkedList<Object> parmvalues = new LinkedList<Object>();
Page p = FieldUtil.createPage(request, Paper.class, parmnames, parmvalues);
parmnames.add("teacherId");
parmvalues.add(getTeacher().getId());
Page page = service.selectPage(p, Paper.class);
session.setAttribute(Constant.SESSION_PAGE, page);
request.setAttribute("actionname", actionname);
request.setAttribute("actionclass", actionclass);
return "listPaper";
} catch (Exception e) {
e.printStackTrace();
return ERROR;
}
}
@RequestMapping(value = "/queryPaperAll.do")
public String queryPaperAll() {
try {
// 字段名称集合
LinkedList<String> parmnames = new LinkedList<String>();
// 字段值集合
LinkedList<Object> parmvalues = new LinkedList<Object>();
Page p = FieldUtil.createPage(request, Paper.class, parmnames, parmvalues);
Page page = service.selectPage(p, Paper.class);
session.setAttribute(Constant.SESSION_PAGE, page);
request.setAttribute("actionname", actionname);
request.setAttribute("actionclass", actionclass);
return "queryPaperAll";
} catch (Exception e) {
e.printStackTrace();
return ERROR;
}
}
}
public String getStudentExamSelf(int uid) {
try {
StudentExam temp = studentExamService.get(uid);
putRequestValue("modifybean", temp);
if (!"开启".equals(temp.getPaper().getEnded())) {
putSessionValue("VoteErrorMessage", "当前试卷还不能查看,请等待教师开启学生查阅功能后浏览");
return "forward:/qiantai/studentExamSelf.jsp";
}
List<StudentExamQuestion> allqList = studentExamQuestionService.selectAll(StudentExamQuestion.class, MapUtil.createMap("examId", uid));
List<StudentExamQuestion> danxuanlist = new ArrayList<StudentExamQuestion>();
List<StudentExamQuestion> duoxuanlist = new ArrayList<StudentExamQuestion>();
List<StudentExamQuestion> panduanlist = new ArrayList<StudentExamQuestion>();
List<StudentExamQuestion> tiankonglist = new ArrayList<StudentExamQuestion>();
List<StudentExamQuestion> jiandalist = new ArrayList<StudentExamQuestion>();
for (StudentExamQuestion p : allqList) {
switch (p.getQuestion().getQuesType()) {
case "单选题":
danxuanlist.add(p);
break;
case "多选题":
duoxuanlist.add(p);
break;
case "判断题":
panduanlist.add(p);
break;
case "填空题":
tiankonglist.add(p);
break;
case "简答题":
jiandalist.add(p);
break;
}
}
putRequestValue("danxuanlist", danxuanlist);
putRequestValue("duoxuanlist", duoxuanlist);
putRequestValue("panduanlist", panduanlist);
putRequestValue("tiankonglist", tiankonglist);
putRequestValue("jiandalist", jiandalist);
} catch (Exception e) {
e.printStackTrace();
}
return "forward:/qiantai/studentExamSelf.jsp";
}
@SuppressWarnings("unchecked")
@RequestMapping(value = "/updateStudentExam.do")
public String updateStudentExam(int uid) {
try {
StudentExam temp = studentExamService.get(uid);
putRequestValue("modifybean", temp);
* 类的英文属性和Excel中的中文列名的对应关系
* @param out
* 导出流
* @throws ExcelException
*/
public static <T> void listToExcel(List<T> list, LinkedHashMap<String, String> fieldMap, String sheetName, OutputStream out)
throws ExcelException {
try {
listToExcel(list, fieldMap, sheetName, 65535, out);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* @MethodName : listToExcel
* @Description : 导出Excel(导出到浏览器,可以自定义工作表的大小)
* @param list
* 数据源
* @param fieldMap
* 类的英文属性和Excel中的中文列名的对应关系
* @param sheetSize
* 每个工作表中记录的最大个数
* @param response
* 使用response可以导出到浏览器
* @throws ExcelException
*/
public static <T> void listToExcel(List<T> list, LinkedHashMap<String, String> fieldMap, String sheetName, int sheetSize,
HttpServletResponse response) throws ExcelException {
// 设置默认文件名为当前时间:年月日时分秒
String fileName = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()).toString();
// 设置response头信息
response.reset();
p.setConditonObject(sbean);
} else {
p.setCurrentPageNumber(pageNum);
}
return p;
}
}
@Controller
@RequestMapping("/sys")
public class PaperQuestionAction extends BaseAction {
private String actionname = "试卷题目";
private String actionclass = "PaperQuestion";
@Autowired
private PaperQuestionService service;
@RequestMapping(value = "/add2PaperQuestion.do")
public String add2() {
request.setAttribute("actionname", actionname);
request.setAttribute("actionclass", actionclass);
return "addPaperQuestion";
}
@RequestMapping(value = "/getPaperQuestion.do")
public String get(int uid) {
try {
* 类的英文属性和Excel中的中文列名的对应关系 如果需要的是引用对象的属性,则英文属性使用类似于EL表达式的格式 如:list中存放的都是student,student中又有college属性,而我们需要学院名称,则可以这样写 fieldMap.put("college.collegeName","学院名称")
* @param sheetName
* 工作表的名称
* @param sheetSize
* 每个工作表中记录的最大个数
* @param out
* 导出流
* @throws ExcelException
*/
public static <T> void listToExcel(List<T> list, LinkedHashMap<String, String> fieldMap, String sheetName, int sheetSize,
OutputStream out) throws ExcelException {
if (list.size() == 0 || list == null) {
//throw new ExcelException("数据源中没有任何数据");
// 创建工作簿并发送到OutputStream指定的地方
WritableWorkbook wwb;
try {
wwb = Workbook.createWorkbook(out);
// 因为2003的Excel一个工作表最多可以有65536条记录,除去列头剩下65535条
// 所以如果记录太多,需要放到多个工作表中,其实就是个分页的过程
// 1.计算一共有多少个工作表
WritableSheet sheet = wwb.createSheet(sheetName, 0);
Label label = new Label(0, 0, "没有数据");
sheet.addCell(label);
wwb.write();
wwb.close();
} catch (Exception e) {
e.printStackTrace();
// 如果是ExcelException,则直接抛出
if (e instanceof ExcelException) {
throw (ExcelException) e;
// 否则将其它异常包装成ExcelException再抛出
} else {
throw new ExcelException("导出Excel失败");
}
extensions: 'jpg,png',
mimeTypes: '.jpg,.png'
}
}">
</div>
<br />
*/
/**
<br />
<label class="row-label">内容编辑</label>
<div class="row-input">
<textarea name="content" id="j_form_content" class="j-content" style="width: 700px;" data-toggle="kindeditor"
data-items="fontname, fontsize, |, forecolor, hilitecolor, bold, italic, underline, removeformat, |, justifyleft, justifycenter, justifyright, insertorderedlist, insertunorderedlist, |, emoticons, image, link"
data-minheight="200px">${modifybean.content}</textarea>
</div>
*/
}
@Controller
@RequestMapping("/sys")
public class QuestionAction extends BaseAction {
private String actionname = "题目";
private String actionclass = "Question";
@Autowired
private QuestionService service;
}
// 2.从指定列中寻找重复行
for (int i = 1; i < realRows; i++) {
int nullCols = 0;
for (int j = 0; j < uniqueFields.length; j++) {
String currentContent = uniqueCells[j][i].getContents();
Cell sameCell = sheet.findCell(currentContent, uniqueCells[j][i].getColumn(), uniqueCells[j][i].getRow() + 1,
uniqueCells[j][i].getColumn(), uniqueCells[j][realRows - 1].getRow(), true);
if (sameCell != null) {
nullCols++;
}
}
if (nullCols == uniqueFields.length) {
throw new ExcelException("Excel中有重复行,请检查");
}
}
}
// 将sheet转换为list
for (int i = 1; i < realRows; i++) {
// 新建要转换的对象
T entity = entityClass.newInstance();
// 给对象中的字段赋值
for (Entry<String, String> entry : fieldMap.entrySet()) {
// 获取中文字段名
String cnNormalName = entry.getKey();
// 获取英文字段名
String enNormalName = entry.getValue();
// 根据中文字段名获取列号
int col = colMap.get(cnNormalName);
// 获取当前单元格中的内容
String content = sheet.getCell(col, i).getContents().toString().trim();
// 给对象赋值
setFieldValueByName(enNormalName, content, entity);
}
resultList.add(entity);
}
} catch (Exception e) {
e.printStackTrace();
// 如果是ExcelException,则直接抛出
Random r = new Random();
int rannum = (int) (r.nextDouble() * (99999 - 10000 + 1)) + 10000;
sDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
nowTimeStr = sDateFormat.format(new Date());
//fileName = fileupload.getOriginalFilename();
// 获取文件的后缀
extName = getExt(fileupload.getOriginalFilename());
newFileName = nowTimeStr + rannum + extName;
filePath = savePath + newFileName;
fileupload.transferTo(new File(filePath));
return newFileName;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 下载文件
*
* @param request
* @param response
* @param storeName
* @param contentType
* @param realName
* @throws Exception
*/
public static void download(HttpServletRequest request, HttpServletResponse response, String storeName, String contentType,
String realName) throws Exception {
request.setCharacterEncoding("UTF-8");
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
String downLoadPath = storeName;
long fileLength = new File(downLoadPath).length();
response.setContentType(contentType);
response.setHeader("Content-disposition", "attachment; filename=" + new String(realName.getBytes("utf-8"), "ISO8859-1"));
response.setHeader("Content-Length", String.valueOf(fileLength));
bis = new BufferedInputStream(new FileInputStream(downLoadPath));
String t = request.getParameter("pageCurrent");
if (StringUtil.notEmpty(t)) {
pageNum = Integer.valueOf(t);
}
Page p = (Page) request.getSession().getAttribute(sessionName);
String newpage = request.getParameter("newpage");
if (StringUtils.isBlank(newpage) || p == null) {
pageNum = 0;
p = new Page();
p.setCurrentPageNumber(pageNum);
p.setTotalNumber(0l);
p.setItemsPerPage(Constant.SESSION_PAGE_NUMBER);
// Map<String, String> textmap = new HashMap<String, String>();
// 页面参数集合
Set valueset = request.getParameterMap().entrySet();
for (Object o : valueset) {
Entry<String, Object> e = (Entry<String, Object>) o;
String name = e.getKey();// 页面字段名称
if (name.startsWith("s_")) {
String fieldValue = request.getParameter(name);// 页面字段值
if (StringUtil.notEmpty(fieldValue)) {
name = name.substring(2, name.length());// 实体字段名称
parmnames.add(name);
parmvalues.add(FieldUtil.format(c, name, fieldValue));
// textmap.put(name.replaceAll("\\.", ""), fieldValue);
}
}
}
SearchParamBean sbean = new SearchParamBean();
sbean.setParmnames(parmnames);
sbean.setParmvalues(parmvalues);
p.setConditonObject(sbean);
} else {
p.setCurrentPageNumber(pageNum);
}
return p;
}
LinkedList<String> parmnames = new LinkedList<String>();
// 字段值集合
LinkedList<Object> parmvalues = new LinkedList<Object>();
Page p = FieldUtil.createPage(request, SimpleUser.class, parmnames, parmvalues);
// if (parmnames.contains("type")) {
// actionname1 = (String) parmvalues.get(parmnames.indexOf("type"));
// }
Page page = service.selectPage(p, SimpleUser.class);
session.setAttribute(Constant.SESSION_PAGE, page);
request.setAttribute("actionname", actionname);
request.setAttribute("actionclass", actionclass);
return "listSimpleUser";
} catch (Exception e) {
e.printStackTrace();
return ERROR;
}
}
}
basePath: '',
upunit: '个文件',
accept: {
title: '文件',
extensions: 'jpg,png',
mimeTypes: '.jpg,.png'
}
}">
</div>
<br />
*/
/**
<br />
<label class="row-label">内容编辑</label>
<div class="row-input">
<textarea name="content" id="j_form_content" class="j-content" style="width: 700px;" data-toggle="kindeditor"
data-items="fontname, fontsize, |, forecolor, hilitecolor, bold, italic, underline, removeformat, |, justifyleft, justifycenter, justifyright, insertorderedlist, insertunorderedlist, |, emoticons, image, link"
data-minheight="200px">${modifybean.content}</textarea>
</div>
*/
}
@Controller
@RequestMapping("/sys")
public class QuestionAction extends BaseAction {
private String actionname = "题目";
* 工作表
* @param list
* 数据源
* @param fieldMap
* 中英文字段对应关系的Map
* @param firstIndex
* 开始索引
* @param lastIndex
* 结束索引
*/
private static <T> void fillSheet(WritableSheet sheet, List<T> list, LinkedHashMap<String, String> fieldMap, int firstIndex,
int lastIndex) throws Exception {
// 定义存放英文字段名和中文字段名的数组
String[] enFields = new String[fieldMap.size()];
String[] cnFields = new String[fieldMap.size()];
// 填充数组
int count = 0;
for (Entry<String, String> entry : fieldMap.entrySet()) {
enFields[count] = entry.getKey();
cnFields[count] = entry.getValue();
count++;
}
// 填充表头
for (int i = 0; i < cnFields.length; i++) {
Label label = new Label(i, 0, cnFields[i]);
sheet.addCell(label);
}
// 填充内容
int rowNo = 1;
for (int index = firstIndex; index <= lastIndex; index++) {
// 获取单个对象
T item = list.get(index);
for (int i = 0; i < enFields.length; i++) {
Object objValue = getFieldValueByNameSequence(enFields[i], item);
String fieldValue = objValue == null ? "" : objValue.toString();
Label label = new Label(i, rowNo, fieldValue);
sheet.addCell(label);
}
rowNo++;
}