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

}
马丁·乔林

这很简单,因为您将表单发送到,[GET] /sendTest但您的弹簧控制器映射到[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>");

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

未找到带有 URI Spring MVC 4 的 HTTP 请求的映射

Spring MVC和图块中的部署失败,未找到带有URI的HTTP请求的映射

Spring MVC4使用maven未找到带有URI的HTTP请求的映射

在名称为“ HorarioLivre”的DispatcherServlet中,未找到带有URI [/HorarioLivre/login.html]的HTTP请求的映射。

在 Spring Boot 1.5.3v 中未找到名称为“dispatcherServlet”的 DispatcherServlet 中带有 URI [/welcome] 的 HTTP 请求的映射

春季错误:在DispatcherServlet中,名称为“ appServlet”的URI [/ myproject /]未找到HTTP请求的映射

在DispatcherServlet中,名称为'的URI []未找到HTTP请求的映射

org.springframework.web.servlet.PageNotFound noHandlerFound未找到具有URI的HTTP请求的映射

org.springframework.web.servlet.PageNotFound noHandlerFound-未找到具有URI的HTTP请求的映射

Spring mvc - REST webservice - 一个 api 是可调用的,另一个报告“未找到带有 URI 的 HTTP 请求的映射”

Web Api错误:“未找到与请求URI匹配的HTTP资源”

找不到带有URI的HTTP请求的映射

Spring MVC。找不到带有URI的HTTP请求的映射

Spring ViewResolver:找不到带有URI的HTTP请求的映射

找不到带有URI [/ webstore]的HTTP请求的映射

春季-找不到带有URI的HTTP请求的映射

使用Spring Boot和View注解配置ViewResolver并没有为带有URI错误的HTTP请求找到映射

为什么即时得到这个错误没有映射发现与URI HTTP请求

错误:在没有xml的URI spring mvc中找不到HTTP请求的映射

Spring Boot Web App错误:在名称为'dispatcherServlet'的DispatcherServlet中找不到带有URI [/]的HTTP请求的映射

Spring:在 Dispatcher Servlet 中找不到带有 URI [uri] 的 HTTP 请求的映射

Spring Dispatcher servlet无法找到index.html。在DispatcherServlet中找不到带有URI []的HTTP请求的映射

Spring Data JPA xml配置:“带有创建下拉菜单的表未找到”

警告:在名称为“dispatcherservlet”的 DispatcherServlet 中找不到带有 URI [/mvc/add] 的 HTTP 请求的映射

Spring MockMvc失败:找不到带有URI的HTTP请求的映射

PageNotFound:1136 - 在 DispatcherServlet 中找不到名称为“spring”的带有 URI [] 的 HTTP 请求的映射

警告:在名称为'dispatcher'的DispatcherServlet中找不到带有URI [/ SpringMVCpractice /]的HTTP请求的映射

PageNotFound-在名称为'dispatcher'的DispatcherServlet中找不到带有URI []的HTTP请求的映射

在名称为'dispatcher'的DispatcherServlet中找不到带有URI的HTTP请求的映射”“