1

I new in Spring, I`m attempting to create simple form example, and i have a problem

When I click on submit and receive input values and valid them, in controller:

@Valid Student student

I have always null values... I try to change mapping, read post Spring MVC form input value is always null but nothing helps :(

My controller:

@Controller
@RequestMapping({"/rejestracja"})
public class RejestracjaController {

@Autowired
private StudentService studentService;

@RequestMapping(method=RequestMethod.GET)
public String rejestracjaSetup (Model model) {
    model.addAttribute("student",new Student());
    return "rejestracjaPage";
}


@RequestMapping(method=RequestMethod.POST)
public String dodajStuenta (@Valid Student student, BindingResult bindingResult) {
    System.out.println("------");
    System.out.print(student.getFirstname());
    System.out.print("=====");
    System.out.print(student.getLastname());
    System.out.print("=====");
    System.out.print(student.getYearLevel());
    System.out.print("=====");
    if (bindingResult.hasErrors()) {
        return "rejestracjaPage";
    }
    return "redirect:/list";
}
}

My .jsp:

<form:form method="POST" commandName="student">
<table>
    <tr>
        <td>First name</td>
        <td>
            <form:input path="firstname"/>
            <form:errors path="firstname"/>
        </td>
    </tr>

    <tr>
        <td>Last name</td>
        <td>
            <form:input path="lastname"/>
            <form:errors path="lastname"/>
        </td>
    </tr>

    <tr>
        <td>yearLevel</td>
        <td>
            <form:input path="yearLevel"/>
            <form:errors path="yearLevel"/>
        </td>
    </tr>

    <tr>
        <td>&nbsp;</td>
        <td><input type="submit" value="Register"></td>
    </tr>
</table>
</form:form>

And Model:

@Entity
public class Student {

@Id
@Column
@GeneratedValue(strategy = GenerationType.AUTO)
private int studentId;

@Column
@NotNull
    @Size(min = 6)
private String firstname;

@Column
@Length(min=3, max=50, message="Example message1")
private String lastname;

@Column
@Min(value = 1, message = "Example message2")
private int yearLevel;

public Student() {
}

   ....
    getters and setters
   .... 
}

What am I doing wrong?

5
  • Can you update your question with the output of your dodajStuenta() controller method? Commented Dec 30, 2013 at 10:27
  • Does it actually look like ------null=====null=====null=====? Commented Dec 30, 2013 at 10:37
  • Yes this like looks "------null=====null=====null=====" Commented Dec 30, 2013 at 10:55
  • Did you declare the tag lib in your JSP? Commented Dec 30, 2013 at 17:54
  • 1
    do you have hibernate-validator in your classpath ? Commented Jan 2, 2014 at 14:19

1 Answer 1

0

Validation is not (yet) your problem. Your problem is that empty Student bean. Try to bind it with @ModelAttribute:

@RequestMapping(method=RequestMethod.POST)
public String dodajStuenta (
        @Valid @ModelAttribute("student") Student student,
        BindingResult bindingResult) {

}
Sign up to request clarification or add additional context in comments.

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.