一个简单的在线答题程序

本文介绍了一个用于公司日常使用的简单在线答题程序,程序包括前端页面和后端Controller处理。前端根据用户答题情况显示得分提示,后端实现了答题逻辑,包括检查用户答题记录并更新或插入分数。程序使用了Spring MVC,联合主键,并且没有单独的Dao层。

    本程序为解决公司日常使用,功能较为单一,题目已经固定,无法随机抽取题目

    首先是前端页面代码

<!--成绩直接回显到页面,在最上边生成提示-->

</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>


     <form:form modelAttribute="onlineTest" method="post"

       action="${prefix}/sections/onlinetest/test">

            <!--使用fields组件来设置表格,避免使用talbe、tr、td这些标签-->

       <fieldset class="form-inline">
       <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>

                                                        <!--单选题和多选题-->

<c:if test="${question.single}">
<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">提&nbsp;交</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();
}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值