2017-06-02 204 views
-1

我提示以下錯誤:收到錯誤 - java.lang.IllegalStateException:無論BindingResult也不是爲bean名稱「命令」可以作爲請求屬性在JSP頁面

我試圖搜索網,但普通的目標對象非解決方案正在解決這個問題。

請找我的控制器類

@Controller 
public class LoginController { 

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

的login.jsp文件

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<body> 
    <h2>Information</h2> 
    <form:form method="POST"> 
     <table> 
      <tr> 
       <td><form:label path="emailId">Name</form:label></td> 
       <td><form:input path="emailId" /></td> 
      </tr> 
      <tr> 
       <td><form:label path="password">Age</form:label></td> 
       <td><form:input path="password" /></td> 
      </tr> 
      <tr> 
       <td colspan="2"><input type="submit" value="Submit" /></td> 
      </tr> 
     </table> 
    </form:form> 
</body> 
</html> 

LoginBean.java類

public class LoginBean { 
    public LoginBean() { 
    } 
    private String emailId; 
    private String password; 

    public LoginBean(String emailId, String password) { 
     this.emailId = emailId; 
     this.password = password; 
    } 

    public String getEmailId() { 
     return emailId; 
    } 

    public void setEmailId(String emailId) { 
     this.emailId = emailId; 
    } 
    public String getPassword() { 
     return password; 
    } 

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

任何幫助將是非常appreciated.Please幫助。

回答

2

我想你應該在你的形式在JSP中添加下列命令名屬性:

<form:form method="POST" commandName="login"> 
     <table> 
      <tr> 
       <td><form:label path="emailId">Name</form:label></td> 
       <td><form:input path="emailId" /></td> 
      </tr> 
      <tr> 
       <td><form:label path="password">Age</form:label></td> 
       <td><form:input path="password" /></td> 
      </tr> 
      <tr> 
       <td colspan="2"><input type="submit" value="Submit" /></td> 
      </tr> 
     </table> 
    </form:form> 

因爲否則彈簧的形式並不瞭解哪些對象的參考。