<?xml version=\"1.0\" encoding=\"UTF-8\" ?> <%@ page language=\"java\" contentTy
ID: 3730147 • Letter: #
Question
<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Next Link</title>
</head>
<body>
<p>${info.get(index).getDescription()}</p>
<p>1:${info.get(index).getAnswerA()}</p>
<p>2:${info.get(index).getAnswerB()}</p>
<p>3:${info.get(index).getAnswerC()}</p>
<p>Correct Answer: ${info.get(index).getCorrectAnswer()}</p>
This is how my code used to be. It would just iterate through a txt and display the questions. But now im supposed to use mvc structure jstl tags to allow the user to submit an answer and check to see if its right. I started it below, ill make it down below.
<p><a href="DrivingTestBrowser?index=${index}">Next</a></p>
</body>
</html>
So this is where im trying to update the old program to be able to take a user radio button entry and determine whether the answer is correct and then move on to the next question. Very confused on how to do it.
<form action='Question' method='post'>
<p>${info.get(index).getDescription()}</p>
<p>1:${info.get(index).getAnswerA()}<input name="ans" type="radio"/></p>
<p>2:${info.get(index).getAnswerB()}<input name="ans" type="radio"/></p>
<p>3:${info.get(index).getAnswerC()}<input name="ans" type="radio"/></p>
<p>Correct Answer: ${info.get(index).getCorrectAnswer()}</p>
<%--<c:if test="${info.get(index).getCorrectAnswer() == ans }">Correct Answer!!</c:if> --%>
<p>
<a href="DrivingTestBrowser?index=${index}">Next</a>
</p>
</form>
Explanation / Answer
Answer:
1. Create a model class first in mvc:
public class Answer{
private int QuestionId;
private String Answer;
// standard getters and setters
}
public QuestionAnswer
{
//define question and answer model properties
// define method to return qusetion and answers option based on index
}
2 . on page define form like below:
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<form:form method="POST" action="/spring-mvc-xml/checkAnswer" modelAttribute="Answer">
<p>${info.get(index).getDescription()}</p>
<form:hidden path="secretValue" />
<table>
<tr>
<td> 1: </td>
<td>
<form:radiobutton path="ans" value="${info.get(index).getAnswerA()”/>
</td>
</tr>
<tr>
<td><input type="submit" value="Submit"/></td>
</tr>
</table>
3 Define controller:
@Controller
public class QuestionAnswerController {
@RequestMapping(value = "/QuestionAnswer", method = RequestMethod.GET)
public ModelAndView showForm() {
QuestionAnswer model = new QuestionAnswer ();
return new ModelAndView("html page for displaying question and answer", "QuestionAnswer", model );
}
public class AnswerController {
@RequestMapping(value = "/xml/checkAnswer" ", method = RequestMethod.POST)
public ModelAndView submit(@Valid @ModelAttribute("Answer")Answer answer,BindingResult result, ModelMap model) {
if (result.hasErrors()) {
return "error";
}
//check answer here
return return new ModelAndView("showForm");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.