0

I have a problem with using spring form to login, I know what is the problem but I can not fix it. This is my code:

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;
    }

}

This is the Controller:

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;
    }
}

My Login Page

<%@ 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>

Registration Page

<%@ 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>

The problem is when user login success, I call the registration page but the model attribute is not called so this error will show:

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

2 Answers 2

1

In your controller method, add this :

  @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 know if you don't understand.

Also, please add a '/' in your individual controller mappings,

Change from :

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

To :

  @RequestMapping(value = "/register", method = RequestMethod.POST)
Sign up to request clarification or add additional context in comments.

1 Comment

prefer lower-case JSP names as well, causes a great deal of nuisance later otherwise.
0

You need to redirect the user after successful login to the registration page like this:

@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";
}

Currently, when someone logs in, you tell Spring to show the view "Register", which requires a bean named "registration". But in the login method, no such bean is added to the model.

In the method "registerpage" that bean is automatically added to the model by Spring through the @ModelAttribute annotation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.