Integrate Spring Beans and JSP Tags

1.6k views Asked by At

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?

1

There are 1 answers

1
Ken Bekov On BEST ANSWER

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 you dispatcher-servlet.xml next way:

   ...
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       <property name="prefix" value="/WEB-INF/views/"/>
       <property name="suffix" value=".jsp"/>
       <property name="exposedContextBeanNames">
           <list>
               <value>someBean</value>
           </list>
       </property>
   </bean>
   ...

where someBean is the name of bean you would like to export to jsp context. You can even expose all spring beans:

   ...
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       <property name="prefix" value="/WEB-INF/views/"/>
       <property name="suffix" value=".jsp"/>
       <property name="exposeContextBeansAsAttributes" value="true"/>
   </bean>
   ...

This allows you access to spring beans from jsp context by bean's name.

Lets say your tag's handler looks like this:

package com.mytags;
public class SomeTag extends SimpleTagSupport {

    private SomeBean bean;

    @Override
    public void doTag() throws JspException, IOException {
        getJspContext().getOut().write(bean.doSomething());
    }

    public SomeBean getBean(){...}
    public void setBean(SomeBean bean){...}
}

Then in tld your tag will be described next way:

...
<tag>
    <name>someTag</name>
    <tag-class>com.mytags.SomeTag</tag-class>
    <body-content>empty</body-content>
    <attribute>
        <name>bean</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
        <type>com.mybeans.SomeBean</type>
    </attribute>
</tag>
...

Note, that you have to specify rtexprvalue as true, because you will pass bean into the tag as EL expression. Now you can use this tag in jsp:

<%@ taglib prefix="mytags" uri="http://mytags.com" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title></title>
  </head>
  <body>
    <mytags:someTag bean="${someBean}"/>
  </body>
</html>

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.