2013-11-27 111 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