65021

Integrate Spring Beans and JSP Tags

Question:

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?

Answer1:

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 <em>all</em> 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> <hr />

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.

Recommend

  • D3.js x-axis time scale
  • struts 2 Undefined attribute name “var”
  • send xml file to http using python
  • I got the error “HtmlEncoderTag cannot be resolved to a type” on defining a custom tag?
  • JSTL version 1.2 declared but 1.1 delivered from Maven Repository
  • How to match the 2nd occurence of a string with regex?
  • No input file specified - apache and php-fastcgi
  • JSP Compilation Issue
  • Python raw_input with forced TLD?
  • Spring WS Client — Authentication using KeyStore/TrustStore and Credentials (Basic Auth)
  • how to create divs dynamically in Jquery?
  • Embed BIRT Report Viewer inside the html page
  • Convert addresses from name to phpmailer format
  • what is the difference between uri and tagdir in JSP taglib
  • How to run PHP exec() as root?
  • How to hide RSS page from everyone except FeedBurner
  • Java package naming and non .com domains
  • Socket.IO client library gives “welcome to socket.io” message
  • C# to Mono GetConsoleWindow Exception
  • How to resize a pixmap with XLib?
  • dynamic server time
  • How to provide hyperlink in email pointing to a specific method inside gwt app (but not main page)
  • Trouble linking libusb using Cmake
  • Type mismatch: cannot convert from Connection to Connection
  • SQL Server Integrated Security from an Azure Web Site
  • Conditional render in tagfile depending on whether the attribute is specified or not
  • Spring MVC redirect with custom http headers
  • Angular5 Service Worker update(SWUpdate) not detecting on firefox. Working on chrome
  • Deploying pre-encrypted configuration files to a production environment
  • Instanciate service on startup in Angular2
  • get passwords from chrome
  • Is it mandatory to have a doGet or doPost method?
  • NRefactory: How do I access unresolved Named Arguments on a Property Attribute?
  • MVC - @Html.CheckBoxFor
  • JPA flush vs commit
  • Elasticsearch script query involving root and nested values
  • Why use database factory in asp.net mvc?
  • How do I configure context broker accept post requests from my remote sensor?
  • Javascript Callbacks with Object constructor
  • Observable and ngFor in Angular 2