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.
The spring:message tag provides you with internationalization support using Spring's MessageSource concept. The MessageSource is an interface providing functionality for retrieving messages. It closely resembles JSTL's fmt:message -tag, however, the MessageSource classes can be integrated with the Spring context.
The spring:bind tag provides you with support for evaluation of the status of a certain bean or bean property.
@Tag is a repeatable annotation that is used to declare a tag for the annotated test class or test method. Tags are used to filter which tests are executed for a given test plan. For example, a development team may tag tests with values such as "fast" , "slow" , "ci-server" , etc.
Have a try by utilizing RequestContextAwareTag
. It will offer you methods to obtain RequestContext and then WebApplicaitonContext. Have a look at here.
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.
Annotate your Tag-Implementation with @Configurable
and add <context:component-scan base-package="your.webapp">
to your Spring-Configuration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With