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.1}分)(单选题)</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">//xanswer是自定义的吗?$表示什么?xanswer${index+1}_${item.id}这个又表示什么?
B:<input type="radio" name="xanswer${index+1}_${item.id}" value="B">
C:<input type="radio" name="xanswer${index+1}_${item.id}" value="C">
D:<input type="radio" name="xanswer${index+1}_${item.id}" value="D">
</p>
<c:set var="index" value="${index+1}" />
</c:forEach>
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.1}分)(单选题)</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">//xanswer是自定义的吗?$表示什么?xanswer${index+1}_${item.id}这个又表示什么?
B:<input type="radio" name="xanswer${index+1}_${item.id}" value="B">
C:<input type="radio" name="xanswer${index+1}_${item.id}" value="C">
D:<input type="radio" name="xanswer${index+1}_${item.id}" value="D">
</p>
<c:set var="index" value="${index+1}" />
</c:forEach>
{
//取出的参数应该是你页面上传递到Action的所有参数,像参数name="xanswer${index+1}_${item.id}"Answers1 answers1 = new Answers1();
String paramName = (String)paramNames.nextElement();
String str = paramName.substring(0, 7);//截取0-7给str有什么用,代表了什么?0-7 表示参数中截取出的第1到第7个位置的字符串,下面已经提示了,即为了判断str是否是答案“xanwser”
//单项选择题页面
if(!paramName.equals("testsetId")&&str.equals("xanswer"))
{
String[] paramValues = request.getParameterValues(paramName);
String[] index = paramName.split("_");//为什么要用-分割paramName,有什么用
//因为你取出的参数在此时是答案 即name="xanswer${index+1}[color=#FF0000]_${item.id}"
故用_切割 得到的index[0]即第 ${index+1}题,index[1]是${item.id},其中item是list1中的对象,item.id是该对象中的id数据[/color] Question1 question1 = (Question1)dao.getSession().load(Question1.class, Long.valueOf(index[1]));//index[1]表示什么?1又表示什么?index[1]如上所述 paramName切割后得到两个值,其是第二个值
<input type="radio" name="xanswer${index+1}_${item.id}" value="A">//xanswer是自定义的吗?$表示什么?xanswer${index+1}_${item.id}这个又表示什么?xanswer是自定义的, ${}是取值的 比方说 现在 的index=2,item.id=8,则
xanswer${index+1}_${item.id}表示 xanswer3_8希望能帮到你