I have a dropdown which consists of options retrieved from a method. The user should be able to select one of these options, and press a submit button. Upon pressing the submit button, the button executes a method that takes the selected option and stores it in some other variable.
Upon pressing submit however, it returns this error:
No mapping found for HTTP request with URI [/sendTest] in DispatcherServlet with name 'dispatcherServlet'
My drop-down form looks like this currenty:
sb.append("<p>"
+ "<div style='height:200px;width:500px;border:1px solid #ccc;font:16px/26px Georgia, Garamond, Serif;overflow:auto;'>"
+ "<form action='/sendTest' method='get'>"
+ "<input type='submit' value='Submit' action='/sendTest' method='post'>"
+ "<a>Current Test for students: " + testcont.getActiveTest() + "</a>"
+ "<fieldset><p>"
+ "<label>Select test</label>"
+ "<select name = 'selection'>"
+ currentTestOptions() // input
+ "</select></p></fieldset>"
+ "</form>"
+ "</div>"
+ "</p>");
and the method the
<input type='submit' value='Submit' action='/sendTest' method='post'>"
button should execute is:
@PostMapping("/sendTest")
@ResponseBody
public void sendTest(@RequestParam(name = "selection") HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException{
for(Test test : testcont.showAllTests()){
if(test.getName().equals(request.getParameter("selection"))){
testcont.SetActiveTest(test);
System.out.println(testcont.getActiveTest());
}
}
}
EDIT: The whole controller class
package project.answers.teacher;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Paths;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import org.springframework.aop.target.ThreadLocalTargetSourceStats;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import junit.extensions.TestSetup;
import project.answers.customExceptions.MultiFileNameException;
import project.answers.customExceptions.MultiTestNameException;
import project.answers.tests.Test;
import project.answers.tests.TestController;
// Teacher webpage
@MultipartConfig
@RestController
@RequestMapping(value = "/Teacher", produces = "text/html;charset=UTF-8")
public class Teacher {
TestController testcont = TestController.getInstance();
@GetMapping("")
@PostMapping("")
@ResponseBody
public String homePage(HttpServletRequest request, HttpServletResponse response) {
StringBuilder sb = new StringBuilder();
sb.append("<p> <a href='/Teacher/NewTest'>New Test upload</a></p>\n");
sb.append("<p>"
+ "<div style='height:200px;width:500px;border:1px solid #ccc;font:16px/26px Georgia, Garamond, Serif;overflow:auto;'>"
+ "<form action='/sendTest' method='post'>"
+ "<input type='submit' value='Submit' action='/sendTest' method='post'>"
+ "<a>Current Test for students: " + testcont.getActiveTest() + "</a>"
+ "<fieldset><p>"
+ "<label>Select test</label>"
+ "<select name = 'selection'>"
+ currentTestOptions() // input
+ "</select></p></fieldset>"
+ "</form>"
+ "</div>"
+ "</p>");
sb.append(
"<p>All available tests on server:<div style='height:200px;width:400px;border:1px solid #ccc;font:16px/26px Georgia, Garamond, Serif;overflow:auto;'>"
+ availableTestList() + "</div></p>"
);
return sb.toString();
}
@PostMapping
@RequestMapping("/NewTest")
@ResponseBody
public String newTestUpload(HttpServletRequest request, HttpServletResponse response) {
StringBuilder sb = new StringBuilder();
// irrelevant method
}
@PostMapping("/sendTest")
@ResponseBody
public String sendTest(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException{
for(Test test : testcont.showAllTests()){
if(test.getName().equals(request.getParameter("selection"))){
testcont.SetActiveTest(test);
System.out.println(testcont.getActiveTest());
}
}
return "<a href='/Teacher'>Back</a>";
}
@PostMapping("/resetCurrentTest")
public void resetCurrentTest(){
testcont.SetActiveTest(null);
}
public String currentTestOptions() {
StringBuilder sb = new StringBuilder();
for(Test test : testcont.showAllTests()){
sb.append("<option value = '" + test.getName() + "'>" + test.getName() + " - " + test.getFile().getName() + "</option>");
}
return sb.toString();
}
public String availableTestList(){
StringBuilder sb = new StringBuilder();
for(Test test : testcont.showAllTests()){
sb.append("<p>" + test.getName() + " - " + test.getFile().getName() +"</p>");
}
return sb.toString();
}
}
It's semply because you send your form to [GET] /sendTest
but your spring controller is mapped on [POST] /Teacher/sendTest
.
Change your html form to send as post :
sb.append("<p>"
+ "<div style='height:200px;width:500px;border:1px solid #ccc;font:16px/26px Georgia, Garamond, Serif;overflow:auto;'>"
+ "<form action='/Teacher/sendTest' method='POST'>"
+ "<input type='submit' value='Submit' action='/sendTest' method='post'>"
+ "<a>Current Test for students: " + testcont.getActiveTest() + "</a>"
+ "<fieldset><p>"
+ "<label>Select test</label>"
+ "<select name = 'selection'>"
+ currentTestOptions() // input
+ "</select></p></fieldset>"
+ "</form>"
+ "</div>"
+ "</p>");
您需要将操作值从action='/sendTest'
更改为action='/Teacher/sendTest'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.