下面是JAVA WEB项目中,学生在线考试,考完试后,提交试卷,自动评分。有几行代码看不懂,帮忙根据Action代码和JSP代码详细说说。
ACTION代码如下:
public class TestpaperAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
TestpaperDAO dao = new TestpaperDAO();
Testpaper testpaper = new Testpaper();
String id = request.getParameter("testsetId");
Testset testset = (Testset)dao.getSession().load(Testset.class, Long.valueOf(id));
testpaper.setTestset(testset);
Student student = (Student)request.getSession().getAttribute("user");
testpaper.setStudent(student);
int count1 = 0;
int count2 = 0;
int count3 = 0;
int count5 = 0;
List list = new ArrayList();
List alist = new ArrayList();
Enumeration paramNames = request.getParameterNames();
while(paramNames.hasMoreElements())//取出请求的参数有哪些?
{
Answers1 answers1 = new Answers1();
String paramName = (String)paramNames.nextElement();
String str = paramName.substring(0, 7);//截取0-7给str有什么用,代表了什么?
//单项选择题页面
if(!paramName.equals("testsetId")&&str.equals("xanswer"))
{
String[] paramValues = request.getParameterValues(paramName);
String[] index = paramName.split("_");//为什么要用-分割paramName,有什么用
Question1 question1 = (Question1)dao.getSession().load(Question1.class, Long.valueOf(index[1]));//index[1]表示什么?1又表示什么?
String isRight = "0";
if(question1.getResult().equals(paramValues[0]))
{
count1 += Integer.parseInt(testset.getMark1().toString());
isRight = "1";
}
else
isRight = "0";
answers1.setQid(Long.valueOf(index[1]));
answers1.setType("1");
answers1.setAnswer(paramValues[0]);
answers1.setIsRight(isRight);
alist.add(answers1);
}
//接下来是多项选择题,是非题,填空题,简答题,页面的代码跟上面的差不多,在此不列举了
下面是JSP页面单项选择题代码如下:
<c:set var="index" value="0" />
<c:forEach items="${list1}" var="item">
<c:if test="${index!=0}">
<div id="div${index}" style="display:none; ">
</c:if>
<c:if test="${index==0}">
<div id="div${index}">
</c:if>
<p align="left">第${index+1}题:${item.title} (${testset.mark1}分)(单选题)</p>
<p align="left">A:${item.a1}</p>
<p align="left">B:${item.a2}</p>
<p align="left">C:${item.a3}</p>
<p align="left">D:${item.a4}</p>
<p align="left">
选择答案:
A:<input type="radio" name="xanswer${index+1}_${item.id}" value="A">