2013-11-27 58 views
0

春天的新手(和HTML /表單等)並且長期陷入這個問題。上次提交後提交jsp表單頁面(java spring)

所以我想有一個首頁,你輸入用戶名。然後單擊提交,並將您帶到儀表板(最終,會有一個包含已連接用戶列表的頁面,一個用於發送預定義消息的提交按鈕負載 - 使用qpid jms)。我只是點擊提交,然後我得到一個錯誤(java.lang.IllegalStateException:既沒有BindingResult,也沒有bean名稱的'dashboardModel'作爲請求屬性提供的普通目標對象) 這隻會發生,如果我有dashboard.jsp中的form:form。我從字面上不知道如何解決這個問題,並嘗試了所有我能找到的東西。我的代碼是簡單,只是從一個教程修改,所以在這裏它是:

的login.jsp

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
<html> 
<head> 
<title>Spring MVC Form Handling</title> 
</head> 
<body> 
<h2>Login Page:</h2> 
<form:form modelAttribute="loginModel" method="POST" 
    action="/HelloWeb/dashboard"> 
    <table> 
     <tr> 
      <td><form:label path="username">Name</form:label></td> 
      <td><form:input path="username" /></td> 
     </tr> 
     <tr> 
      <td colspan="2"><input type="submit" value="Submit" /></td> 
     </tr> 
    </table> 
</form:form> 
</body> 
</html> 

Login.java

package com.tutorialspoint; 
public class Login { 
private String username; 

public String getUsername() { 
    return username; 
} 

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

LoginController.java

package com.tutorialspoint; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.ModelAttribute; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.servlet.ModelAndView; 
import org.springframework.ui.ModelMap; 

@Controller 
public class LoginController { 

@RequestMapping(value = "/login", method = RequestMethod.GET) 
public ModelAndView login() { 
    return new ModelAndView("login", "loginModel", new Login()); 
} 

@RequestMapping(value = "/loggedIn", method = RequestMethod.POST) //never actually used 
public String loggedIn(@ModelAttribute("Login") Login login, ModelMap model) { 
    model.addAttribute("username", login.getUsername()); 

    return "dashboard"; 
} 
} 

dashboard.jsp

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
<html> 
<head> 
<title>DASHBOARD</title> 
</head> 
<body> 
<table> 
    <tr> 
     <td>Dashboard</td> 
     <td>DASHBOARD</td> 
    </tr> 
    <tr> 
     <td>Username</td> 
     <td>${username}</td> 
    </tr> 
    <tr> 
     <td>variable</td> 
     <td>${variable}</td> 
    </tr> 
</table> 

<form:form modelAttribute="dashboardModel" method="POST" 
    action="/HelloWeb/dashboard"> 
    <table> 
     <tr> 
      <td><form:label path="variable">Name</form:label></td> 
      <td><form:input path="variable" /></td> 
     </tr> 
     <tr> 
      <td colspan="2"><input type="submit" value="Submit" /></td> 
     </tr> 
    </table> 
</form:form> 
</body> 
</html> 

Dashboard.java

package com.tutorialspoint; 

public class Dashboard { 
private String variable; 

public String getVariable() { 
    return variable; 
} 
public void setVariable(String variable) { 
    this.variable = variable; 
} 
} 

DashboardController.java

package com.tutorialspoint; 

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.ModelAttribute; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.servlet.ModelAndView; 
import org.springframework.ui.ModelMap; 

@Controller 
public class DashboardController { 
@RequestMapping(value = "/dashboard", method = RequestMethod.GET) 
public ModelAndView dashboard() { 
    return new ModelAndView("dashboard", "dashboardModel", new Dashboard()); 
} 

@RequestMapping(value = "/dashboard", method = RequestMethod.POST) 
public String addVariable(@ModelAttribute("SpringWeb") Dashboard dashboard, 
     ModelMap model) { 
    model.addAttribute("variable", dashboard.getVariable()); 
    return "dashboard"; 
} 
} 

感謝您的時間。

回答

0

我認爲這個問題是在這裏:

<form:form modelAttribute="loginModel" method="POST" action="/HelloWeb/dashboard"> 
          ^^^^^^^^^^ 

這裏:

@RequestMapping(value = "/loggedIn", method = RequestMethod.POST) //never actually used 
public String loggedIn(@ModelAttribute("Login") Login login, ModelMap model) { 
             ^^^^^ 

modelAttributeform:form元素和@ModelAttribute說法應該是相同的。

我的意思是:

@RequestMapping(value = "/loggedIn", method = RequestMethod.POST) //never actually used 
public String loggedIn(@ModelAttribute("loginModel") Login login, ModelMap model) { 
             ^^^^^^^^^^ 

編輯:

而且,組成部分應該是這樣的:

<form:form modelAttribute="dashboardModel" method="POST" action="/loggedIn.htm"> 
    <table> 
     <tr> 
     <td>Name</td> 
     <td><form:input path="username" /></td> 
     </tr> 
     <tr> 
     <td colspan="2"><input type="submit" value="Submit" /></td> 
     </tr> 
    </table> 
</form:form> 
+0

實際上它並不似乎無論什麼價值有是 - 我不知道爲什麼。例如。你可以從原始教程中看到(http://www.tutorialspoint.com/spring/spring_mvc_form_handling_example.htm)已經有了「SpringWeb」作爲@ModelAttribute,但是它只是另一個的默認「命令」,所以他們不會' t匹配。 – Phil

+0

對不起,我錯過了一些東西。檢出編輯。 – nashuald

+0

對不起,哪種形式? login.jsp或dashboard.jsp中的一個? – Phil