问题描述
我想在spring框架中编写我的标记(扩展 TagSupport
)。在我的标签类中,将使用一些应该在春天自动注入的服务。但我总是得到null,似乎spring无法在我的标记类中注入服务实例。
I want to write my tag (extends TagSupport
) in my spring framework. In my tag class, will use some service which should auto inject by spring. But I always get null, seems spring can't inject service instance in my tag class.
代码如下:
public class FetchTagNameTag extends TagSupport {
@Autowired
private TaskService taskService;
...
taskService
始终为空。
我该如何解决这个问题?
谢谢。
How can I resolve this?Thanks.
推荐答案
JSP标记对象不由Spring管理,它们由servlet容器管理。因此,您无法将内容自动装入标签。
JSP tag objects are not managed by Spring, they are managed by the servlet container. As a result, you cannot autowire stuff into your tags.
如果您需要从spring appcontext获取bean,那么您的Spring MVC控制器需要设置bean作为请求属性(使用 request.setAttribute()
),以便标记对象可以获取它。
If you need to get hold of beans from the spring appcontext, then your Spring MVC controller needs to set the bean as a request attribute (using request.setAttribute()
), so that the tag object can get hold of it.
这篇关于如何在我的春季项目中编写标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!