mumei2018 发表于 2024-4-13 17:26:12

运行工程后打开页面出现下列报错 请问是什么问题 怎么解决

Controller类里代码如下
package com.example.demo.app.inquiry;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
@RequestMapping("/inquiry")
public class InquiryController {

       
        @GetMapping("/form")
        public String form(InquiryForm inquiryForm,Model model) {
                model.addAttribute("title","Inquiry Form");
                return "inquiry/form";
        }
       
        @PostMapping("/form")
        public String formGoBack(InquiryForm inquiryForm, Model model) {
                model.addAttribute("title","Inquiry Form");
                return "Inquiry Form";
        }
       
        @PostMapping("/confirm")
        public String confirm(@Validated BindingResult result,Model model) {
                if(result.hasErrors()) {
                        model.addAttribute("title","Inquiry Form");
                        return "inquiry/confirm";
                }
                model.addAttribute("title","Inquiry Form");
                return "inquiry/confirm";
               
        }
}


运行工程后打开页面出现下列报错 请问是什么问题 怎么解决

This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sat Apr 13 17:23:46 CST 2024
There was an unexpected error (type=Method Not Allowed, status=405).
Method 'GET' is not supported.
org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' is not supported
        at org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.handleNoMatch(RequestMappingInfoHandlerMapping.java:265)
        at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lookupHandlerMethod(AbstractHandlerMethodMapping.java:441)
        at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:382)
        at org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.getHandlerInternal(RequestMappingInfoHandlerMapping.java:126)
        at org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.getHandlerInternal(RequestMappingInfoHandlerMapping.java:68)
        at org.springframework.web.servlet.handler.AbstractHandlerMapping.getHandler(AbstractHandlerMapping.java:507)
        at org.springframework.web.servlet.DispatcherServlet.getHandler(DispatcherServlet.java:1283)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1065)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:979)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014)
        at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:903)
        at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:564)
        at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885)
        at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:658)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:205)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149)
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149)
        at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149)
        at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149)
        at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:167)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:90)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:482)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:115)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:93)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:344)
        at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:391)
        at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63)
        at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:896)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1744)
        at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52)
        at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
        at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:63)
        at java.base/java.lang.Thread.run(Thread.java:842)

FishC 发表于 2024-4-13 17:26:29

这个报错的原因是你在InquiryController类中定义了一个@GetMapping("/form")的方法,但是对应的URL请求方式是GET方法。而你在浏览器中访问该URL时使用的是POST方法,所以导致报错。

要解决这个问题,有两种方法:

1. 将浏览器中的请求方式改为GET方法。在浏览器中直接输入对应的URL,不要使用表单提交请求。

2. 修改InquiryController类中的@PostMapping("/form")方法为@GetMapping("/form"),即将POST请求改为GET请求。

修改后的代码如下:

@GetMapping("/form")
public String formGoBack(InquiryForm inquiryForm, Model model) {
    model.addAttribute("title", "Inquiry Form");
    return "inquiry/form";
}

这样修改之后,在浏览器中访问对应的URL时就可以正常显示页面了。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。

眼前的都是浮云 发表于 2024-4-14 16:38:01

如果实在拿不准请求方式,直接使用@RequestMapping()
页: [1]
查看完整版本: 运行工程后打开页面出现下列报错 请问是什么问题 怎么解决