提问者:小点点

Springboot 下拉菜单 - “找不到带有 URI 的 HTTP 请求的映射”错误


我有一个下拉列表,其中包含从方法检索的选项。用户应该能够选择这些选项之一,然后按提交按钮。按下提交按钮后,该按钮将执行一个方法,该方法采用所选选项并将其存储在其他变量中。

但是,在按提交时,它会返回此错误:

在 DispatcherServlet 中找不到名称为“dispatcherServlet”的带有 URI [/sendTest] 的 HTTP 请求的映射

我的下拉表单当前如下所示:

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>");

和方法

<input type='submit' value='Submit' action='/sendTest' method='post'>" 

按钮应执行为:

@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());
            }
        }
    }

编辑:整个控制器类

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();
    }

}

共2个答案

匿名用户

这是因为您将表单发送到 [GET] /sendTest,但您的Spring控制器映射到 [POST] /Teacher/sendTest 上。

更改您的 html 表单以作为帖子发送:

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'