0

spring 新手(以及 HTML/forms 等)并且长期以来一直被困在这个问题上。

所以我想要一个你输入用户名的首页。然后单击提交,然后将您带到仪表板(最终,将出现一个页面,其中包含已连接用户的列表,大量提交按钮以发送预定义的消息 - 使用 qpid jms)。

首页很好,但是我单击提交并出现错误(java.lang.IllegalStateException:Bean name 'dashboardModel' 的 BindingResult 和普通目标对象都不能用作请求属性)只有当我有一个表单时才会发生这种情况:form in仪表板.jsp。我真的不知道如何解决这个问题,并且已经尝试了我能找到的一切。我的代码很简单,只是根据教程修改的,所以这里是:

登录.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>

登录.java

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

public String getUsername() {
    return username;
}

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

登录控制器.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";
}
}

仪表板.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>

仪表板.java

package com.tutorialspoint;

public class Dashboard {
private String variable;

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

仪表板控制器.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";
}
}

谢谢你的时间。

4

1 回答 1

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) {
                                        ^^^^^

element上的modelAttribute值和参数应该相同。form: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>
于 2013-11-27T22:53:54.460 回答