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> </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?
dodajStuenta()controller method?------null=====null=====null=====?