弹簧形式错误

时间:2015-11-11 08:44:53

标签: spring spring-mvc error-handling

我使用spring form登录时遇到问题,我知道问题是什么,但我无法解决。 这是我的代码:

My Login.java
    package com.fsoft.entity;

import org.hibernate.validator.constraints.NotEmpty;

public class Login {
    @NotEmpty(message = "Please enter your username")
    private String username;
    @NotEmpty(message = "Please enter your password")
    private String password;

    public String getUsername() {
        return username;
    }

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

    public String getPassword() {
        return password;
    }

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

My Student.java

package com.fsoft.entity;

import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.Email;

public class Student {
    @NotNull(message = "Please enter student username")
    private String username;
    @NotNull(message = "Please enter student password")
    private String password;
    @NotNull(message = "Please enter student name")
    private String name;
    @NotNull(message = "Please enter student birth date")
    private String dob;
    @NotNull(message = "Please enter student email")
    @Email(message = "Email is invalid")
    private String email;
    @NotNull(message = "Please enter student age")
    private String age;

    public String getUsername() {
        return username;
    }

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

    public String getPassword() {
        return password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDob() {
        return dob;
    }

    public void setDob(String dob) {
        this.dob = dob;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

}

这是控制器:

package com.fsoft.springmvc;

import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
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.bind.annotation.RequestParam;

import com.fsoft.entity.Login;
import com.fsoft.entity.Student;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
    private static final String LOGIN = "Login";
    private static final String SUBMIT = "Submit";

    /**
     * Simply selects the home view to render by returning its name.
     */
    @RequestMapping(method = RequestMethod.GET)
    public String home(@ModelAttribute("Login") Login login) {
        return "Login";
    }

    @RequestMapping(value = "Login", method = RequestMethod.POST)
    public String Login(@ModelAttribute("Login") @Valid Login login, BindingResult result,
            @RequestParam("action") String action, ModelMap model) {
        String returnpage = "Register";
        if (LOGIN.equals(action)) {
            if (result.hasErrors()) {
                returnpage = "Login";
            }
        }
        return returnpage;
    }

    @RequestMapping(value = "/gotoregister", method = RequestMethod.GET)
    public String registerpage(@ModelAttribute("registration") Student student) {
        return "Register";
    }

    @RequestMapping(value = "register", method = RequestMethod.POST)
    public String regis(@ModelAttribute("registration") @Valid Student student, @RequestParam("action") String action,
            BindingResult result) {
        String returnpage = "/Register";
        if (SUBMIT.equals(action)) {
            if (result.hasErrors()) {
            }
        }
        return returnpage;
    }
}

我的登录页面

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ page session="false"%>
<html>


<head>
<title>Login</title>
</head>
<body>
    <form:form action="Login" method="post" modelAttribute="Login">
        <div class='form-horizontal well' style="width: 300px">
            <div class="form-group">
                <label>User Name</label>
                <form:input path="username" type="text" class="form-control"
                    id="username" name="username" placeholder="User Name"></form:input>
                <form:errors path="username" cssClass="error" />
            </div>
            <div class="form-group">
                <label>Password</label>
                <form:input path="password" type="password" class="form-control"
                    id="password" placeholder="Password" name="password"></form:input>
                <form:errors path="password" cssClass="error" />
            </div>
            <button type="submit" class="btn btn-default" name="action"
                value="Login">Login</button>
        </div>
    </form:form>
</body>
<link rel="stylesheet"
    href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">

<!-- jQuery library -->
<script
    src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script
    src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</html>

注册页面

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ page session="false"%>
<html>
<link rel="stylesheet"
    href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">

<!-- jQuery library -->
<script
    src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script
    src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

<head>
<title>Registration</title>
</head>
<body>

    <form:form action="register" method="post"
        modelAttribute="registration">
        <div class='form-horizontal well' style="width: 800px">
            <h3>Hello : ${Login.username}</h3>
            <h3>Registration Page</h3>
            <div class="form-group">
                <label>User Name</label>
                <form:input path="username" type="text" class="form-control"
                    id="username" name="username" placeholder="User Name"></form:input>
                <form:errors path="username" cssClass="error" />
            </div>
            <div class="form-group">
                <label>Password</label>
                <form:input path="password" type="password" class="form-control"
                    id="password" placeholder="Password" name="Password"></form:input>
                <form:errors path="password" cssClass="error" />
            </div>
            <div class="form-group">
                <label>Email</label>
                <form:input path="email" type="email" class="form-control"
                    id="email" name="email" placeholder="Email"></form:input>
                <form:errors path="email" cssClass="error" />
            </div>
            <div class="form-group">
                <label>Name</label>
                <form:input path="name" type="text" class="form-control" id="name"
                    placeholder="Name" name="name"></form:input>
                <form:errors path="name" cssClass="error" />
            </div>
            <div class="form-group">
                <label>Date of Birth</label>
                <form:input path="dob" type="text" class="form-control" id="dob"
                    name="dob" placeholder="Date of Birth"></form:input>
                <form:errors path="dob" cssClass="error" />
            </div>
            <div class="form-group">
                <label>Age</label>
                <form:input path="age" type="number" class="form-control" id="age"
                    placeholder="Age" name="age"></form:input>
                <form:errors path="age" cssClass="error" />

            </div>
            <button type="submit" class="btn btn-default" value="Submit">Submit</button>
        </div>
    </form:form>

</body>
</html>

问题是当用户登录成功时,我调用了注册页面,但未调用model属性,因此会显示以下错误:

 Neither BindingResult nor plain target object for bean name 'registration' available as request attribute

2 个答案:

答案 0 :(得分:1)

在您的控制器方法中,添加:

  @RequestMapping(value = "register", method = RequestMethod.POST)
    public String regis(@ModelAttribute("registration") @Valid Student student, @RequestParam("action") String action,
            BindingResult result, Model model) {

// Below line will fix it for you, dont forget I have also added Model in //the method
model.addAttribute("registration", new Student());
// Try to use lower-case URL's. Also, your web-flow is improper
        //String returnpage = "/Register";
        if (SUBMIT.equals(action)) {
            if (result.hasErrors()) {
            }
        }
        return "redirect:/register";
    }

Lemme知道你是否理解。

另外,请添加&#39; /&#39;在您的个人控制器映射中,

更改自:

  @RequestMapping(value = "register", method = RequestMethod.POST)

致:

  @RequestMapping(value = "/register", method = RequestMethod.POST)

答案 1 :(得分:0)

您需要在成功登录注册页面后重定向用户,如下所示:

@RequestMapping(value = "Login", method = RequestMethod.POST)
public String Login(@ModelAttribute("Login") @Valid Login login, BindingResult result, @RequestParam("action") String action, ModelMap model) {
    if (LOGIN.equals(action)) {
        if (result.hasErrors()) {
            return "Login";
        }
    }
    return "redirect:/gotoregister";
}

目前,当有人登录时,您告诉Spring显示“Register”视图,这需要一个名为“registration”的bean。但是在login方法中,没有将这样的bean添加到模型中。

在方法“registerpage”中,bean通过@ModelAttribute注释自动添加到模型中。