简体   繁体   English

Spring 4的Thymeleaf表单:Etat HTTP 405-请求方法'GET'不支持

[英]Thymeleaf form with spring 4: Etat HTTP 405 - Request method 'GET' not supported

The issue is that I want to use object AdminLogin to authenticate administrators. 问题是我想使用对象AdminLogin对管理员进行身份验证。

Here is the AdminLogin class 这是AdminLogin类

package com.mintad.admin.beans;

import java.io.Serializable;

import org.springframework.stereotype.Component;
/**
 * 
 * Used to authenticate admin
 *
 */

@Component("adminLogin")
public class AdminLogin implements Serializable {

    private static final long serialVersionUID = -6394045014528037520L;

    private String username;

    private String password;

    public AdminLogin() {
    }

    public AdminLogin(String username, String password) {
        super();
        this.username = username;
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

And controllers methods 和控制器方法

    @ModelAttribute("adminLogin")
        public AdminLogin createModel() {
            return new AdminLogin();
        }

@RequestMapping(value = "/admin/login", method = RequestMethod.POST)
public ModelAndView login(@ModelAttribute AdminLogin adminLogin, HttpServletRequest request,
        @RequestParam(value = "error", required = false) String error, @RequestParam(value = "logout", required = false) String logout) {
    LOGGER.debug("admin login page");
    ModelAndView model = new ModelAndView();
    model.addObject("adminLogin", adminLogin);
    if (error != null) {
        model.addObject("error", "Invalid username and password!");
    }

    if (logout != null) {
        model.addObject("msg", "You've been logged out successfully.");
    }

    model.setViewName("/admin/index");

    LOGGER.debug("returning admin login page");

    return model;

}

@RequestMapping(value = "/admin/login", method = RequestMethod.GET)
public String greetingForm(Model model) {
    model.addAttribute("adminLogin", new AdminLogin());
    return "/admin/login";
}

The issue is that the form is submitted with GET although It's POST like the following ; 问题是表单是用GET提交的,尽管它是POST,如下所示;

<form class="m-t" role="form" th:action="@{/admin/login}" th:object="${adminLogin}" method=" POST" autocomplete="off">
                <div th:if="${param.error}" class="alert alert-danger">Invalid username and password.</div>
                <div th:if="${param.logout}" class="alert alert-success">You have been logged out.</div>
                <div class="form-group">
                    <input type="text" class="form-control" id="username" name="username" th:field="*{username}" placeholder="Username" required="" />
                </div>
                <div class="form-group">
                    <input type="password" class="form-control" id="password" name="password" th:field="*{password}" placeholder="Password" required="" />
                </div>
                <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
                <button type="submit" class="btn btn-primary block full-width m-b">Login</button>
                <a href="#"><small>Forgot password?</small></a>
                <p class="text-muted text-center">
                    <small>Do not have an account?</small>
                </p>
                <a class="btn btn-sm btn-white btn-block" href="register.html">Create an account</a>
            </form>

I have added the GET to confirm my thaughts and also because I've seen examples about how to use objects in login forms. 我添加了GET来确认自己的想法,也因为我已经看到了有关如何在登录表单中使用对象的示例。

Please help me fix this or would you please give a cleaner way to achieve my goals. 请帮助我解决此问题,或者请您提供一种更清洁的方法来实现我的目标。

Thanks ! 谢谢 !

Try removing the space in your method=" POST" in your form. 尝试在表单中的method=" POST"中删除空格。 It's case-insensitive , but acceptable values are GET and POST . 不区分大小写 ,但是可接受的值为GETPOST The default method is GET . 默认方法是GET

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

相关问题 Spring文件上传Etat HTTP 405 - 不支持请求方法&#39;POST&#39; - Spring File Upload Etat HTTP 405 - Request method 'POST' not supported HTTP状态405 - 不支持请求方法“GET” - HTTP Status 405 - Request method 'GET' not supported Thymeleaf的Spring Booth不支持请求方法&#39;GET&#39; - Request method 'GET' not supported Spring Booth with Thymeleaf 405不允许使用方法http方法:spring-security中不支持请求方法&#39;GET&#39; - 405 Method Not Allowed http method being used: Request method 'GET' not supported` in spring-security Thymeleaf 405 不支持请求方法“POST” - Thymeleaf 405 Request method 'POST' not supported 405不允许使用方法http方法:使用Spring MVC时不支持请求方法&#39;GET&#39;。 - 405 Method Not Allowed http method being used: Request method 'GET' not supported` while using Spring MVC? Spring MVC:HTTP 405-发出POST请求时不支持请求方法“ GET” - Spring MVC: HTTP 405 - Request method 'GET' not supported when making POST request 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 请求方法“POST”不支持-&gt; HTTP 405 - Spring MVC Request method 'POST' not supported -> HTTP 405
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM