对于来自另一个文件的JSTL中的每个循环



我想要一种方法,让用户回答一系列问题,然后在最后告诉他们他们的分数。

到目前为止,我有这个

许多.jsp,每个问题都写在里面。每个页面都有一个下一个按钮,将它们带到下一个问题。最后一个问题链接到一个名为results.jsp的页面,该页面需要确定他们给出的答案是对是错,然后告诉他们自己的分数。

这是qOne.jsp

<form action="/HelloSpring/results" method="post">
    <p> What is the correct wrapper class for the primitive int? </p>
    <input type="radio" name="radios" value="Int">Int<br>
    <input type="radio" name="radios" value="Enum">Enum<br>
    <input type="radio" name="radios" value="integer1">integer<br>
    <input type="radio" name="radios" value="Integer2">Integer<br>
    <input type="text" name="elc" autocomplete="off" >name<br>
             <input type="submit" value="Next" >
</form>
<FORM><INPUT Type="button" VALUE="Back" onClick="history.go(-1);return true;"></FORM>
</body>
</html>

点击后,您将进入results.jsp

<body>
<h2>Result</h2>
<p>Good day ${elc}, you scored ${a} </p>
<c:forEach items="${results}" var="total" >
    ${total};
    ${Score};
    ${score};
</c:forEach>
<section>
<p>Total score: ${total}</p>
</section>
</body>
</html>

这是WebController.java文件,它应该处理最后一个问题的结果(如上所示)。它运行一个if语句,然后根据哪个if"运行",它将向hashmap添加0或1。在某个时候,我会打印出哈希图的总数,以显示他们的分数,并添加其他问题的请求映射。我只想让第一个工作。

 @RequestMapping(value = "/results", method = RequestMethod.POST)
        public String results(Model model, HttpSession session, HttpServletRequest request, @RequestParam String elc) {
                Object map2 = session.getAttribute("map");
                HashMap<String, Integer> map = (HashMap<String, Integer>) map2;
                request.getSession().setAttribute("score", "total");
                if(request.getParameter("radios") != null) {
                if(request.getParameter("radios").equals("Int")) {
                    model.addAttribute("Score", 0);
                    System.out.println("Int selected as your answer");
                }
                else if(request.getParameter("radios").equals("Enum")) {
                    model.addAttribute("Score", 0);
                    System.out.println("Enum selected as your answer");
                }
                else if(request.getParameter("radios").equals("integer1")) {
                    model.addAttribute("Score", 0);
                    System.out.println("integer was selected as your answer");
                }
                else {
                    System.out.println("Integer was selected as your answer");
                    model.addAttribute("Score", 1);
                }
            }
                   return "results";
        }

这里没有真正的问题,但如果你想这样做,我建议你在向导的前端完成所有问题http://www.jquery-steps.com/Examples.

如果您在前端制作了一个向导,您只需要检查控制器中最后的所有问题,并将结果发送到另一个jsp。如果您不想有一个更大的question.jsp文件,您仍然可以保留您的jsp并将其包含在每个向导部分中。

在这种情况下,下一个和上一个按钮将很容易。分数计算也很容易。

最新更新