【发布时间】:2015-01-07 21:41:26
【问题描述】:
我有一个 .jsp 文件,应该告诉用户他们的考试结果。
这是一个 WebController.java 类,它计算出一个人给出的答案,然后根据他们的答案是错还是正确添加 0 或 1。
@RequestMapping(value = "/results", method = RequestMethod.POST)
public String results(Model model, HttpSession session, HttpServletRequest request,
HttpServletResponse response){
String radio = request.getParameter("radios1");
request.setAttribute("total", total);
model.addAttribute("total", total);
if (radio.equals("int")){
total = total + 0;
}
else if (radio.equals("Enum")){
total = total + 0;
}
else if (radio.equals("integer")){
total = total + 0;
}
else if (radio.equals("Integer")){
total = total + 1;
}
return "results";
}
我想将“total”变量打印到 jsp 页面上,以便它显示在浏览器中。 所以这个想法是用户将能够立即看到他们在测试中得到了什么。
这也可以反过来以同样的方式工作吗?例如在上面的代码中,java类可以获取位于jsp类的表单内的参数“radios1”吗?
<p>Good day <%= session.getAttribute("uname") %> </p>
<p>For question 1 you chose <%= session.getAttribute("q1") %> </p>
<p>For question 2 you chose <%= session.getAttribute("q2") %> </p>
<p>For question 3 you chose <%= session.getAttribute("q3") %> </p>
<p>For question 4 you chose <%= session.getAttribute("q4") %> </p>
<section>
<p>Total score: ${requestScope.total}</p>
<p>Total score: ${total}</p>
</section>
【问题讨论】:
标签: java jsp variables spring-mvc