?? examaction.java
字號:
package com.myExam.action;
//導入util包
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//導入servlet包
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//導入spring包
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
//導入自身的包
import com.myExam.bean.Exam;
import com.myExam.bean.Shiti;
import com.myExam.bean.ShitiOption;
import com.myExam.domain.SetExam;
import com.myExam.domain.SetShiti;
import com.myExam.domain.SetShitiOption;
import com.myExam.domain.SetShitiType;
//實現多動作處理
public class ExamAction extends MultiActionController{
private String examInfo;
private String exam;
private String examChengji;
private SetExam setExam;
private SetShitiType setShitiType;
private SetShiti setShiti;
private SetShitiOption setShitiOption;
// 封裝調查基本信息
public Exam getExamCommand(HttpServletRequest req, HttpServletResponse res) {
Exam exam = new Exam();
if (req.getParameter("checkbox") != null) {
try {
//判斷是否是新增或者修改還是刪除,如果是新增則不需要id
if (req.getParameter("id" + (String)req.getParameter("checkbox") + "") != null) {
exam.setId(Integer.valueOf(req.getParameter("id" + (String)req.getParameter("checkbox") + "")));
}
//獲取基本信息
exam.setName((String)req.getParameter("name" + (String)req.getParameter("checkbox") + ""));
exam.setShitiTypeId(Integer.valueOf(req.getParameter("shitiTypeId" + (String)req.getParameter("checkbox") + "")));
exam.setCount(Integer.valueOf(req.getParameter("count" + (String)req.getParameter("checkbox") + "")));
exam.setFenshu(Float.valueOf(req.getParameter("fenshu" + (String)req.getParameter("checkbox") + "")));
} catch (Exception ex) {
ex.printStackTrace();
}
} else {
exam = null;
}
return exam;
}
//新增試卷
public ModelAndView insert(HttpServletRequest request, HttpServletResponse response)throws Exception {
//強制轉換成Exam
Exam exam = getExamCommand(request, response);
Map model = new HashMap();
if (exam != null) {
// 新增試卷
getSetExam().insertExam(exam);
}
//獲取試題種類
List shitiTypes = getSetShitiType().queryShitiType();
List exams = getSetExam().queryExam();
//將信息傳入頁面
model.put("exams", exams);
model.put("shitiTypes", shitiTypes);
model.put("msg", getSetExam().getMsg());
//返回到指定頁面
return new ModelAndView(getExamInfo(), model);
}
// 修改試卷
public ModelAndView update(HttpServletRequest request, HttpServletResponse response)throws Exception {
//強制轉換成Exam
Exam exam = getExamCommand(request, response);
Map model = new HashMap();
if (exam != null) {
// 修改試卷
getSetExam().updateExam(exam);
}
// 獲取試題種類
List shitiTypes = getSetShitiType().queryShitiType();
List exams = getSetExam().queryExam();
//將信息傳入頁面
model.put("exams", exams);
model.put("shitiTypes", shitiTypes);
model.put("msg", getSetExam().getMsg());
//返回到指定頁面
return new ModelAndView(getExamInfo(), model);
}
// 刪除試卷
public ModelAndView delete(HttpServletRequest request, HttpServletResponse response)throws Exception {
//強制轉換成Exam
Exam exam = getExamCommand(request, response);
Map model = new HashMap();
if (exam != null) {
// 刪除試卷
getSetExam().deleteExam(exam);
}
// 獲取試題種類
List shitiTypes = getSetShitiType().queryShitiType();
List exams = getSetExam().queryExam();
//將信息傳入頁面
model.put("exams", exams);
model.put("shitiTypes", shitiTypes);
model.put("msg", getSetExam().getMsg());
//返回到指定頁面
return new ModelAndView(getExamInfo(), model);
}
// 生成試卷
public ModelAndView createExam(HttpServletRequest request, HttpServletResponse response)throws Exception {
//強制轉換成Exam
Exam exam = getExamCommand(request, response);
Map model = new HashMap();
List shitis = getSetShiti().getShitiByTypeId(exam.getShitiTypeId());
// 將該試卷的試題傳遞給頁面
model.put("shitis", shitis);
//獲取每個試題的試題項目
Map mapShitiOptions = new HashMap();
for (int i = 0; shitis != null && i < exam.getCount(); i++) {
Shiti shiti = (Shiti)shitis.get(i);
//獲取該試題的試題項目
List listOptions = getSetShitiOption().getShitiOption(shiti.getId());
//將每一個試題項目根據試題的id,存入map
mapShitiOptions.put(shiti.getId(), listOptions);
}
model.put("exam", exam);
model.put("mapShitiOptions", mapShitiOptions);
model.put("msg", getSetExam().getMsg());
//返回到指定頁面
return new ModelAndView(getExam(), model);
}
// 判斷試卷
public ModelAndView countExam(HttpServletRequest request, HttpServletResponse response)throws Exception {
// 強制轉換成examId
int examId = Integer.valueOf((String)request.getParameter("examId"));
Map model = new HashMap();
Exam exam = getSetExam().query(examId);
List shitis = getSetShiti().getShitiByTypeId(exam.getShitiTypeId());
//計算成績
Map mapShitiOptions = new HashMap();
float fenshu = 0L;
for (int i = 0; shitis != null && i < exam.getCount(); i++) {
Shiti shiti = (Shiti)shitis.get(i);
//獲取用戶選擇的試題項目id
int shitiOptionId = Integer.valueOf((String)request.getParameter(shiti.getId().toString()));
ShitiOption shitiOption = getSetShitiOption().getShitiOptionById(shitiOptionId);
//如果該試題項目為正確答案
if (shitiOption.getIsok() == 1) {
fenshu += exam.getFenshu();
}
}
//將信息傳入頁面
model.put("exam", exam);
model.put("fenshu", fenshu);
//返回到指定頁面
return new ModelAndView(getExamChengji(), model);
}
/**返回setExam.
*/
public SetExam getSetExam() {
return setExam;
}
/**設定setExam
*/
public void setSetExam(SetExam setExam) {
this.setExam = setExam;
}
/**返回examInfo.
*/
public String getExamInfo() {
return examInfo;
}
/**設定examInfo
*/
public void setExamInfo(String examInfo) {
this.examInfo = examInfo;
}
/**返回setShitiType.
*/
public SetShitiType getSetShitiType() {
return setShitiType;
}
/**設定setShitiType
*/
public void setSetShitiType(SetShitiType setShitiType) {
this.setShitiType = setShitiType;
}
/**返回setShiti.
*/
public SetShiti getSetShiti() {
return setShiti;
}
/**設定setShiti
*/
public void setSetShiti(SetShiti setShiti) {
this.setShiti = setShiti;
}
/**返回setShitiOption.
*/
public SetShitiOption getSetShitiOption() {
return setShitiOption;
}
/**設定setShitiOption
*/
public void setSetShitiOption(SetShitiOption setShitiOption) {
this.setShitiOption = setShitiOption;
}
/**返回exam.
*/
public String getExam() {
return exam;
}
/**設定exam
*/
public void setExam(String exam) {
this.exam = exam;
}
/**返回examChengji.
*/
public String getExamChengji() {
return examChengji;
}
/**設定examChengji
*/
public void setExamChengji(String examChengji) {
this.examChengji = examChengji;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -