package com.java1234.action; import java.util.ArrayList; import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.Random; import java.util.Set; import net.sf.json.JSONObject; import org.apache.struts2.ServletActionContext; import com.java1234.dao.PaperDao; import com.java1234.dao.QuestionDao; import com.java1234.model.Paper; import com.java1234.model.Question; import com.java1234.util.ResponseUtil; import com.java1234.util.StringUtil; import com.opensymphony.xwork2.ActionSupport; /** * 试卷Action类 * @author Administrator * */ public class PaperAction extends ActionSupport{ /** * */ private static final long serialVersionUID = 1L; private PaperDao paperDao=new PaperDao(); private QuestionDao questionDao=new QuestionDao(); private String mainPage; private String paperId; private List<Paper> paperList=new ArrayList<Paper>(); private List<Question> squestionList=new ArrayList<Question>(); private List<Question> mquestionList=new ArrayList<Question>(); private String title; // 标题 private Paper paper; public List<Paper> getPaperList() { return paperList; } public void setPaperList(List<Paper> paperList) { this.paperList = paperList; } public List<Question> getSquestionList() { return squestionList; } public void setSquestionList(List<Question> squestionList) { this.squestionList = squestionList; } public List<Question> getMquestionList() { return mquestionList; } public void setMquestionList(List<Question> mquestionList) { this.mquestionList = mquestionList; } public void setPaper(Paper paper) { this.paper = paper; } public Paper getPaper() { return paper; } public String getPaperId() { return paperId; } public void setPaperId(String paperId) { this.paperId = paperId; } public String getMainPage() { return mainPage; } public void setMainPage(String mainPage) { this.mainPage = mainPage; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } /** * 获取所有试卷 * @return * @throws Exception */ public String list()throws Exception{ paperList=paperDao.getPapers(); mainPage="exam/selectPaper.jsp"; return SUCCESS; } /** * 获取所有试卷(管理) * @return * @throws Exception */ public String paperList()throws Exception{ paperList=paperDao.getPapers(); mainPage="paper/paperList.jsp"; return SUCCESS; } /** * 通过id获取试卷实体 * @return * @throws Exception */ public String getPaperById()throws Exception{ paper=paperDao.getPaper(paperId); mainPage="paper/paperSave.jsp"; return SUCCESS; } /** * 保存预操作 * @return * @throws Exception */ public String preSave()throws Exception{ if(StringUtil.isNotEmpty(paperId)){ paper=paperDao.getPaper(paperId); title="修改试卷"; }else{ title="添加试卷"; } mainPage="paper/paperSave.jsp"; return SUCCESS; } /** * 保存试卷 * @return * @throws Exception */ public String savePaper()throws Exception{ if(StringUtil.isNotEmpty(paperId)){ paper.setId(Integer.parseInt(paperId)); }else{ paper.setJoinDate(new Date()); } paperDao.savePaper(paper); return "save"; } /** * 删除试卷 * @return * @throws Exception */ public String deletePaper()throws Exception{ paper=paperDao.getPaper(paperId); JSONObject resultJson=new JSONObject(); if(questionDao.existQuestionByPaperId(paperId)){ resultJson.put("error","试卷下面有题目,不能删除"); }else{ paperDao.paperDelete(paper); resultJson.put("success",true); } ResponseUtil.write(resultJson,ServletActionContext.getResponse()); return null; } /** * 获取指定试卷 * @return * @throws Exception */ public String getDetailPaper()throws Exception{ paper=paperDao.getPaper(paperId); Set<Question> questionList=paper.getQuestions(); Iterator<Question> it=questionList.iterator(); while(it.hasNext()){ Question q=it.next(); if("1".equals(q.getType())){ squestionList.add(q); }else{ mquestionList.add(q); } } squestionList=this.getRandomQuestion(squestionList, 3); mquestionList=this.getRandomQuestion(mquestionList, 2); mainPage="exam/paper.jsp"; return SUCCESS; } /** * 获取随机试题 * @param questionList * @param num * @return */ private List<Question> getRandomQuestion(List<Question> questionList,int num){ List<Question> resultList=new ArrayList<Question>(); Random random=new Random(); if(num>0){ for(int i=1;i<=num;i++){ int n=random.nextInt(questionList.size()); Question q=questionList.get(n); if(resultList.contains(q)){ i--; }else{ resultList.add(questionList.get(n)); } } } return resultList; } }

weilaizhisheng LV21
2023年9月16日
似鹤不是鹤似鹤不是鹤 LV1
2023年5月5日
sswert LV2
2023年5月4日
amour1 LV11
2023年5月3日
tujf13763823976 LV3
2023年3月28日
fu8867 LV2
2023年2月20日
wubinbin LV11
2023年2月11日
shamexidan LV9
2022年5月20日
shibinf LV3
2022年4月8日
and123456 LV11
2022年2月7日

ldzhouyong
4月28日
暂无贡献等级
luhong LV2
3月31日
微信网友_7357533879193600 LV2
1月26日
416696772
2024年10月31日
暂无贡献等级
wangbh1234 LV1
2024年6月7日
pokerf LV5
2024年4月7日
taoshen95 LV16
2024年2月29日
gjw19884260466 LV3
2024年1月8日
WBelong LV8
2023年12月11日
cpla1998 LV8
2023年11月28日