views:

40

answers:

2

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.

The code is like the following:

public class FetchTagNameTag extends TagSupport {

   @Autowired
   private TaskService taskService;
   ...

taskService is always null.

How can I resolve this? Thanks.

A: 

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.

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.

skaffman
Thanks, you answered my question.
Tom
A: 

Check out these spring packages in the spring reference docs and in the spring source:

  • org.springframework.web.servlet.tags
  • org.springframework.web.servlet.tags.form
  • If nothing else, those will show you how the spring developers wrote the spring tags.

    dwb
    Thanks,I will check it out later.
    Tom