Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write tag in my spring project?

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.

like image 241
Tom Avatar asked Oct 13 '10 14:10

Tom


People also ask

What is spring message tag?

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.

What is spring bind tag?

The spring:bind tag provides you with support for evaluation of the status of a certain bean or bean property.

What is @TAG annotation in spring boot?

@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.


3 Answers

Have a try by utilizing RequestContextAwareTag. It will offer you methods to obtain RequestContext and then WebApplicaitonContext. Have a look at here.

like image 80
Steinway Wu Avatar answered Sep 30 '22 05:09

Steinway Wu


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.

like image 33
skaffman Avatar answered Sep 30 '22 04:09

skaffman


Annotate your Tag-Implementation with @Configurable and add <context:component-scan base-package="your.webapp"> to your Spring-Configuration.

like image 21
Kai Moritz Avatar answered Sep 30 '22 03:09

Kai Moritz