2017-10-15 74 views
0

我与Spring MVC的工作,我得到了以下错误的纯目标对象Spring MVC的:既不BindingResult也不是为bean名称“用户”可以作为

Caused by: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute 

,当我们都没有通过,通常会出现此错误/将该对象添加到控制器代码内的模型中。 BUT我已经完成了,但仍然出现错误。 我已经通过互联网上的解决方案看完全相同的错误,但他们都指向在控制器中添加新的对象。不知道为什么对我来说它不工作。

不知道我做错了什么。

这是我的login.html形式

<div class="container"> 
      <div class="starter-template"> 
       <h2>Login</h2> 
      </div> 

      <form th:object="${user}" th:method="post" th:action="validateUser" class="form-horizontal"> 
       <table class="table table-striped"> 

        <tr> 
         <td> 
          <div class="control-group"> 
           <label class="control-label">Email</label> 
          </div> 
         </td> 
         <td> 
          <div class="controls"> 
           <input type="text" class="form-control" th:field="*{emailAddress}" /> 
           <label class="control-label"></label> 
          </div> 
         </td> 
        </tr> 
        <tr> 
         <td> 
          <div class="control-group"> 
           <label class="control-label">Password</label> 
          </div> 
         </td> 
         <td> 
          <div class="controls"> 
           <input type="password" class="form-control" th:field="*{password}" /> 
           <label class="control-label"></label> 
          </div> 
         </td> 
        </tr> 
        <tr> 
         <td></td> 
         <td> 
          <div class="form-actions pull-right"> 
           <input type="submit" name="_eventId_validateUser" value="Login" class="btn btn-success" tabindex="5" /> 
           <input type="submit" name="_eventId_cancel" value="Cancel" class="btn btn-danger " tabindex="6" /> 
          </div> 
         </td> 
        </tr> 
       </table> 
      </form> 

我Controller.java

package com.niti.controller; 

import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.ModelAttribute; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 

import com.niti.authentication.service.AuthenticationService; 
import com.niti.bo.UserBO; 
import com.niti.service.exception.ServiceBusinessException; 

@Controller 
public class LoginController { 

    private static final Logger Logger = LoggerFactory.getLogger(LoginController.class); 

    @Autowired 
    private AuthenticationService authenticationService; 

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

    @RequestMapping(value="/validateUser" , method=RequestMethod.POST) 
    public String processLoginInfo(@ModelAttribute UserBO userBO) throws ServiceBusinessException { 
     UserBO user = authenticationService.authenticateUser(userBO.getEmailAddress(), userBO.getPassword()); 

     return "userDetails"; 
    } 


} 

回答

2

在你的HTML表单要绑定

th:object="${user}" // user 

在另一方面你在您的控制器方法processLoginInfo中默认绑定userBO

你的方法应该是像这样

@RequestMapping(value="/validateUser" , method=RequestMethod.POST) 
public String processLoginInfo(@ModelAttribute("user") UserBO userBO) throws ServiceBusinessException { 
    UserBO user = authenticationService.authenticateUser(userBO.getEmailAddress(), userBO.getPassword()); 

    return "userDetails"; 
} 
相关问题