本程序为解决公司日常使用,功能较为单一,题目已经固定,无法随机抽取题目
首先是前端页面代码
<!--成绩直接回显到页面,在最上边生成提示-->
</div>
<c:if test="${testscore == 100}">
<div class="alert alert-success" >
您的得分为:${testscore}分,测试通过。
</div>
</c:if>
<c:if test="${testscore < 100}">
<div class="alert alert-danger" >
您的得分为:${testscore}分,测试未通过,请再试一次。
</div>
</c:if>
action="${prefix}/sections/onlinetest/test">
<!--使用fields组件来设置表格,避免使用talbe、tr、td这些标签-->
<span>
<form:label path="id" for="id" cssErrorClass="text-error">编号</form:label>
<form:input path="id" placeholder="您的编号" cssClass="input-mini" cssErrorClass="input-mini" maxlength="4"/>
<form:errors path="id" cssClass="text-error"/>
</span>
<span>
<form:label path="name" for="name" cssErrorClass="text-error">姓名</form:label>
<form:input path="name" placeholder="您的姓名" cssClass="input-small" cssErrorClass="input-small" maxlength="30"/>
<form:errors path="name" cssClass="text-error"/>
</span>
<span style="float:right;margin-right:0">
<form:label path="org" for="org" cssErrorClass="text-error">部门/机构</form:label>
<form:input path="org" placeholder="您的部门" cssClass="input-medium" cssErrorClass="input-medium" maxlength="50"/>
<form:errors path="org" cssClass="text-error"/>
</span>
</fieldset>
<ol class="questions"><c:forEach items="${questions}" var="question"
varStatus="loopStatus">
<li>
<c:set var="errclass" value=""/>
<c:if test="${results[loopStatus.index]!=null and results[loopStatus.index]==false}">
<c:set var="errclass" value="text-error"/>
</c:if>
<!--在实体类中,将结果的集合置为空,因此判断结果如果是错的,将题目显示成红色-->
<p class="${errclass}">${question.content}</p>
<!--单选题和多选题-->
<ol class="options inline">
<form:radiobuttons path="choices[${loopStatus.index}]"
items="${options[loopStatus.index]}" element="li" />
</ol>
</c:if>
<c:if test="${not question.single}">
<ol class="chkbox">
<form:checkboxes path="choices[${loopStatus.index}]"
items="${options[loopStatus.index]}" element="li" />
</ol>
</c:if>
</li>
</c:forEach>
</ol>
<hr>
<div class="text-center">
<button class="btn btn-large btn-primary" type="submit" onclick="scoreResult">提 交</button>
</div>
</form:form>
</div>
/**
* 在线答题。Controller类
*/
@RequestMapping(value="/sections/onlinetest/test", method = RequestMethod.GET)
public void getTest(Model model) {
model.addAttribute("questions", OnlineTest.getQuestions());
model.addAttribute("options", OnlineTest.getOptions());
model.addAttribute(new OnlineTest());
}
@RequestMapping(value="/sections/onlinetest/test", method = RequestMethod.POST)
public void postTest(HttpSession session, Model model,
@Valid OnlineTest onlineTest, BindingResult result) {
model.addAttribute("questions", OnlineTest.getQuestions());
model.addAttribute("options", OnlineTest.getOptions());
if (result.hasErrors()) {
return ;
}
try {
int point = onlineTest.doTest();
//将分数放入model中,用于回显
model.addAttribute("testscore", point);model.addAttribute("results",onlineTest.getResult());
logger.debug(point);
String testNo = "TEST";
portalService.doScore(onlineTest.getId(), onlineTest.getName(),
onlineTest.getOrg(), point, testNo);
if (point < 100) {
ObjectError objErr = new ObjectError("onlineTest",
"测试未通过,请修改错误答案后重新提交。");
result.addError(objErr);
}
}catch (RuntimeException e) {
ObjectError objErr = new ObjectError("test", e.getMessage());
result.addError(objErr);
}
}
因为项目比较小,因此,未设置Dao层。使用了联合主键。首先查询答题者是否已经答过题目,如果已经有答题记录,直接覆盖,如果没有的话,直接将答题者的信息插入。系统只会保存最后一次答题分数。
@Override
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
public void doScore(String id, String name, String org, int point,
String testNo) {
String sql = "select count(1) from onlinetestscore "
+ "where id = ? and name = ? and testno = ?";
try {
int queryCount = jdbcTemplate.queryForInt(sql, new Object[] {id, name, testNo});
logger.debug(queryCount);
if(queryCount!=0){
sql = "update onlinetestscore set score = ? "
+ "where id = ? and name = ? and testno = ?";
jdbcTemplate.update(sql,new Object[] {point,id,name,testNo});
logger.debug(sql);
}else{
sql = "insert into onlinetestscore values(?,?,?,?,?)";
jdbcTemplate.update(sql,new Object[] {testNo,id,name,org,point});
logger.debug(sql);
}
}catch (DataAccessException e){
e.printStackTrace();
}
}