gpt4 book ai didi

java - 使用 if 语句将结果发送回 jsp

转载 作者:太空宇宙 更新时间:2023-11-04 14:23:47 25 4
gpt4 key购买 nike

我想要一种用户通过一系列单选按钮输入详细信息的方法。然后,java 类通过查看响应并根据响应向用户返回分数(即不同的响应可以提供不同的反馈)来完成一些工作。我已经编写了 jsps 的代码,现在需要某种方式可以链接到包含 if 语句的 java 类,遍历 if 语句,然后返回结果。

选择单选按钮的第一个 jsp

<form:form action="/HelloSpring/questionTwo" method="post">

<p> What is the correct wrapper class for the primitive int? </p>
<input type="radio" name="radios1" value="Int" path="types" >Int<br>
<input type="radio" name="radios1" value="Enum" path="types">Enum<br>
<input type="radio" name="radios1" value="integer" path="types" >integer<br>
<input type="radio" name="radios1" value="Integer" path="types" >Integer<br>

<input type="submit" value="Next" >

</form:form >

应该读取结果然后执行 if 语句的 Controller

@RequestMapping(value = "/results", method = RequestMethod.POST)
public String results(Model model, HttpServletRequest request,
HttpServletResponse response){


String radio = (String)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;
}
else{
total = total + 0;
}


System.out.println(radio);

return "results";

}

应该发布他们得到的结果的jsp。 session.getAttribute 位不起作用 ${total}

<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: ${total} /4</p>

最佳答案

将表单提交给 Controller ,调用 if 语句并将结果放入 Controller 返回的 ModelAndView 中。

@RequestMapping(value = "/results", method = RequestMethod.POST)
public ModelAndView results(Model model, HttpServletRequest request, HttpServletResponse response){

ModelAndView mav = new ModelAndView();
// set name of next view ...
mav.setViewName("results");

// handle if...

// add results to next view ...
mav.addObject("anyNameYouWant", yourObject);
return mav;

}

关于java - 使用 if 语句将结果发送回 jsp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26889940/

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