View Full Version : jsp:useBean
lime
Mar 4th, 2005, 10:46 AM
Hello,
I have to integrate some very old style jsp with my springframework.
These jsp use beans with <jsp:useBean ... > tag: how can I get the beans initialized by Spring in jsp?
Alef Arendsen
Mar 8th, 2005, 01:23 PM
I don't really know what you mean by 'initialized'. I'll ramble on a bit, maybe this helps:
jsp:useBean respects the scope attribute (defaults to page). The model of a Spring controller is merge into the HttpServletRequest, thus attributes passed in there have request scope. I've ported a number of applications still using JSP only and basically searched all links, changed the .jsp extensions to .html and used the InternalResourceViewResolver and the UrlFilenameViewController as follows:
<bean id="viewResolver" class="org.spr.web.servlet.view.InternalResourceViewResol ver">
<property name="prefix"><value>/</value></property>
<property name="suffix"><value>.jsp</property>
<property name="viewClass"><value>org.spr.web.servlet.view.InternalResourceView</value></property>
</bean>
<bean name="/*.html" class="org.spr.web.servlet.controller.UrlFilenameViewCont roller/>
If now you want to have beans initialized by Spring, you can for example export them to the request using a HandlerInterceptor, something like this:
<bean id="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUr lHandlerMapping">
<property name="interceptors">
<list>
<bean class="bla.bla.JspUseBeanExporter">
<property name="beans">
<list>
<ref bean="beanOne"/>
<ref bean="beanTwo"/>
</list>
</property>
</bean>
</list>
</property>
</bean>
in the postHandle method of the interceptor (extend HandlerInterecptorAdapter) you should then export the beans to the request (using for example the bean name or id, corresponding to the jsp:useBean id mentioned in the JSPs). The only thing left to do is either change the scope of all jsp:useBean lines to request or somehow export the beans to the page scope using a custom tag or something.
It's all not really that nice, but I've used this approach once for a large application with loads of JSPs and it works. The nice thing is that all of a sudden your applicaiton is 'springified' and you fully take advantage of all the other features of Spring.
Hope this helps a bit,
regards,
Alef Arendsen
Powered by vBulletin® Version 4.2.1 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.