I'm very new to Spring and I'm encountering the following problem.
I've got the following Controller, in which the @Autowired works perfectly (tried debugging and it works fine).
@Controller
@RequestMapping(value = "/registration")
@SessionAttributes("rf")
public class RegistrationController
{
@Autowired
UserJpaDao userDao;
@RequestMapping(method = RequestMethod.GET)
@Transactional
public String setupForm(Model model) throws Exception
{
model.addAttribute("rf", new RegistrationForm());
return "registration";
}
@RequestMapping(method = RequestMethod.POST)
@Transactional
public String submitForm(@ModelAttribute("rf") RegistrationForm rf, Model model) throws Exception
{
// ...
User user = rf.getUser();
userDao.save(user);
// ...
return "registration";
}
}
But when I submit my form, the @Autowired field in my RegistrationForm remains null.
RegistrationForm.java:
@Component
public class RegistrationForm
{
@Autowired
CountryJpaDao countryDao;
// ... fields...
public RegistrationForm()
{
}
@Transactional
public User getUser() throws InvalidUserDataException
{
//...
Country c = countryDao.findByCode("GB"); // Throws java.lang.NullPointerException
// ...
}
// ... getters/setters...
}
Here is the form's HTML/JSTL:
<form:form method="POST" modelAttribute="rf">
...
</form:form>
Can anyone help me?
Thank you.
(inspired by this post on SpringSource forums)