gpt4 book ai didi

java - 如何打印 .jsp 文件中来自 Java 类的变量?

转载 作者:行者123 更新时间:2023-12-01 22:35:56 25 4
gpt4 key购买 nike

我有一个 .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>

最佳答案

将总计添加到模型中并在 html 上显示为

model.addAttribute("total",total);

并在 html 上显示为

${total}

关于java - 如何打印 .jsp 文件中来自 Java 类的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26863231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com