@Autowired为@ModelAttribute [英] @Autowired for @ModelAttribute
问题描述
我是Spring的新手,并且遇到以下问题.
I'm very new to Spring and I'm encountering the following problem.
我有以下控制器,其中@Autowired可以完美地工作(尝试调试并且可以正常工作).
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";
}
}
但是当我提交表单时,我的RegistrationForm中的@Autowired字段仍然为空.
But when I submit my form, the @Autowired field in my RegistrationForm remains null.
RegistrationForm.java:
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...
}
这是表单的HTML/JSTL:
Here is the form's HTML/JSTL:
<form:form method="POST" modelAttribute="rf">
...
</form:form>
有人可以帮助我吗?
谢谢.
(灵感来自SpringSource论坛上的这篇文章)
(inspired by this post on SpringSource forums)
推荐答案
您正在这里混淆概念.对于Spring管理的bean,可以使用@Component
和@Autowired
之类的对象,对于用于绑定表单数据的瞬态,一次性对象,可以使用@ModelAttribute
之类的对象.不应将两者混为一谈.您在RegistrationForm
上的@Component
和@Autowired
批注将被Spring忽略,因为它们不适用于该上下文.
You're mixing up your concepts here. You use the likes of @Component
and @Autowired
for Spring-managed beans, and @ModelAttribute
for transient, throwaway objects that are used to bind form data. The two should not be mixed. Your @Component
and @Autowired
annotations on RegistrationForm
will be ignored by Spring, because they're not appropriate in that context.
像RegistrationForm
这样的类应该表示表单数据,而不能代表其他任何内容.通常,控制器会向RegistrationForm
询问用户ID,然后从DAO本身查看实际的User
对象.如果您希望RegistrationForm
查找User
本身,则当控制器要求提供User
对象时,控制器需要手动将DAO提供给RegistrationForm
.
Classes like RegistrationForm
should represent the form data, and nothing else. Typically, the controller would ask RegistrationForm
for the user ID, and would then look at the actual User
object from the DAO itself. If you want RegistrationForm
to look up the User
itself, then your controller needs to manually supply the DAO to RegistrationForm
when it asks for the User
object.
就Spring论坛上的帖子而言,您会注意到它从未收到答案.这不是从中获取灵感的好来源.
As far as that post on the Spring forum is concerned, you'll notice that it never received an answer. It's not a good source to take inspiration from.
请注意,我并不是说希望将bean自动装配到Form Back对象中不是一个好主意,我只是说Spring不会这样做.
Note that I'm not saying that desiring to autowire beans into a form back object is a bad idea, I'm just saying that Spring doesn't do that.
这篇关于@Autowired为@ModelAttribute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!