简体   繁体   English

Spring MVC 请求方法“POST”不支持-> HTTP 405

[英]Spring MVC Request method 'POST' not supported -> HTTP 405

Got stuck for ~4 hours wondering where is the mistake in Spring MVC/thymeleaf app.卡住了大约 4 个小时,想知道 Spring MVC/thymeleaf 应用程序中的错误在哪里。
My local goal is to render admin.html after submitting login/pass form at home page.我的本地目标是在主页提交登录/通过表单后呈现 admin.html。

Controller: Controller:

@Controller
public class HomeController {
        @GetMapping("/")
        public String getHome(Model m) {
            m.addAttribute( "user",new User());
            return "/home";
        }
        @PostMapping("/")
        public String getSubmit(@ModelAttribute User user){

            return "/admin";
        }
}

home.html:主页.html:

  <form action="#" th:action="@{/admin}" th:object="${user}" method="post">
            <p class="txt">Name: <input type="text" th:field="*{name}"/></p>
            <p class="txt">Password: <input type="text" th:field="*{password}"/></p>
            <p><input class="button" type="submit" value="Submit" />
                <input class="button" type="reset" value="Reset" /></p>
        </form >

User class:用户 class:

@Data
@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    private String name;
    private String password;
    private boolean isAdmin;
    private String address;
}

So I have googled loads of ideas, removed spring security from pom.xml,所以我搜索了很多想法,从 pom.xml 中删除了 spring 安全性,
tried to use @RequestedMapping with RequestMethod.PUT, - no way, it doesn't work.尝试将 @RequestedMapping 与 RequestMethod.PUT 一起使用, - 没办法,它不起作用。

Your form references th:action="@{/admin}" as target.您的表单引用th:action="@{/admin}"作为目标。 Your controller does not map /admin but only root / .您的 controller 没有 map /admin但只有 root /

You have to change your target to / .您必须将目标更改为/

If you want to render a template /admin then your return is correct.如果你想渲染一个模板/admin那么你的回报是正确的。 If you want to redirect to /admin so a new controller may handle this, so you have to write redirect:/admin instead.如果你想重定向到/admin ,那么一个新的 controller 可能会处理这个问题,所以你必须改写redirect:/admin

Your controller should be like this:你的 controller 应该是这样的:

@Controller
public class HomeController {
    @GetMapping("/")
    public String getHome(Model m) {
        m.addAttribute("user", new User());
        return "/home";
    }

    @PostMapping("/admin")
    public String getSubmit(User user) {   
        return "/admin";
    }
}

home.html shoud be like: home.html 应该是这样的:

<form action="@{/admin}" th:object="${user}" method="post">
    <p class="txt">Name: <input type="text" th:field="*{name}"/></p>
    <p class="txt">Password: <input type="text" th:field="*{password}"/></p>
    <p><input class="button" type="submit" value="Submit" />
    <input class="button" type="reset" value="Reset"/></p>
</form >

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HTTP状态405-Spring MVC不支持请求方法&#39;POST&#39; - HTTP Status 405 - Request method 'POST' not supported Spring MVC HTTP 状态 405 - 不支持请求方法“POST”(Spring MVC) - HTTP Status 405 - Request method 'POST' not supported (Spring MVC) Spring MVC - HTTP 状态 405 - 不支持请求方法“POST” - Spring MVC - HTTP Status 405 - Request method 'POST' not supported Spring MVC HTTP状态405-不支持请求方法“ POST” - Spring MVC HTTP Status 405 - Request method 'POST' not supported Spring MVC:HTTP 405-发出POST请求时不支持请求方法“ GET” - Spring MVC: HTTP 405 - Request method 'GET' not supported when making POST request HTTP状态405 - 使用Spring Security的Spring MVC中不支持请求方法'POST' - HTTP Status 405 - Request method 'POST' not supported in Spring MVC with Spring Security 具有Spring Security的Spring-mvc获得HTTP状态405-请求方法&#39;POST&#39;不支持 - Spring-mvc with spring security getting HTTP Status 405 - Request method 'POST' not supported Spring MVC的CURL POST请求给出405方法&#39;POST&#39;不支持 - Curl POST request for spring mvc gives 405 Method 'POST' not supported 405 - 不支持请求方法“POST” Spring MVC + Spring 安全 - 405 - Request method 'POST' not supported Spring MVC + Spring Security 405不允许的方法:请求方法&#39;POST&#39;不支持| Ajax / Spring MVC - 405 Method Not Allowed : Request method 'POST' not supported | Ajax/Spring MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM