Is it possible to integrate Spring beans and JSP tags somehow in Spring. That is use a Spring bean as a JSP Tag? And if so is this a good/terrible idea? So something like this would be possible:
@Component
public class SomeBean extends SimpleTagSupport {
@Autowired
SomeBean someBean;
@Override
public void doTag() throws JspException, IOException {
getJspContext().getOut().write(someBean.doSomething());
}
}
What would I do in the tags.tld to get it to use the Spring bean instead of creating a new instance that would not have someBean injected? Or is it something else?
InternalResourceViewResolver
allows you use exposition for export beans into jsp context. For example, if you would like to expose list of beans, you should configure view resolver in youdispatcher-servlet.xml
next way:where
someBean
is the name of bean you would like to export to jsp context. You can even expose all spring beans:This allows you access to spring beans from jsp context by bean's name.
Lets say your tag's handler looks like this:
Then in tld your tag will be described next way:
Note, that you have to specify
rtexprvalue
astrue
, because you will pass bean into the tag as EL expression. Now you can use this tag in jsp:But actually it is not good idea to access spring bean right from tag's handler directly. Tag have to know how to show data, but should be abstracted from how to obtain this data. Better prepare all data you would like to show in controller and pass it as model to the jsp.