2016-01-26 81 views
0

问题是我想使用对象AdminLogin来验证管理员身份。Thymeleaf form with spring 4:Etat HTTP 405 - 不支持请求方法'GET'

这里是后台管理类

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

和控制器方法

@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"; 
} 

的问题是,形式与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> 

我已经添加了GET来确认我的thaughts,也因为我见过有关如何在登录表单中使用对象的示例。

请帮我解决这个问题,或者请你给一个更清洁的方式来实现我的目标。

谢谢!

回答

2

尝试在您的表单中删除method=" POST"中的空格。这是case-insensitive,但可接受的值是GETPOST。默认方法是GET

+0

非常感谢。我还没有看到那个空间...... – Sofiane

相关问题