20071205 Wednesday December 05, 2007

Spring MVC, JstlView and exposeContextBeansAsAttributes Did you know that Spring MVC's JstlView has a exposeContextBeansAsAttributes property you can use to expose all your Spring beans to JSTL? I didn't. To configure it, you configure your viewResolver as follows:

<bean id="viewResolver" 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="exposeContextBeansAsAttributes" value="true"/>
    <property name="prefix" value="/"/>
    <property name="suffix" value=".jsp"/>
</bean>

After doing this, any Spring bean can get referenced in JSTL with:

${beanId.getterMethodWithoutTheGetPrefix}

If you're using Spring 2.5a annotations and <context:component-scan>, you'll need to specify a "value" attribute on your annotations in order to reference them in JSTL. For example:

@Controller(value = "beanId")
@RequestMapping("/foo.html")
public class MyController extends SimpleFormController

...

@Component(value="testClass")
public class TestClass {

Pretty cool stuff. It'd be a lot more useful if you could call methods with parameters. Hopefully JUEL will solve that problem. JSTL's functions work, but I'd rather write ${foo.method('arg')} rather than ${taglib:callMethod(foo, 'method', 'arg')}. Posted in Java at Dec 05 2007, 06:34:41 PM MST 5 Comments

Comments:

Matt, why don't you just download JBoss EL-- it has varargs, method invoke, and projections

Posted by Jacob Hookom on December 05, 2007 at 10:23 PM MST #

Jacob - I'd be more than happy to, especially if I can configure it as the default EL in Tomcat. To do this with JUEL, changes need to be made to Tomcat.

BTW, does JBoss EL allow HTML escaping by default?

Posted by Matt Raible on December 05, 2007 at 11:05 PM MST #

Could it be used as custom tags replacement ?

Posted by Pascal Alberty on December 06, 2007 at 01:24 AM MST #

Even if the newInstance stuff was added late, it still wouldn't correct the fact that the Jasper compiler-- pre compiles all nodes/expressions separate from the Expression factory. To change that would be a huge gutting of the Jasper implementation.

Posted by Jacob Hookom on December 06, 2007 at 12:24 PM MST #

It appears you are correct Jacob. I tried implementing the Tomcat patch and plugging in JUEL. No dice. Doesn't seem to work on GlassFish either. Full details on JUEL's forums.

Posted by Matt Raible on December 06, 2007 at 04:06 PM MST #

Post a Comment:
  • HTML Syntax: Allowed
Click me to subscribe
Matt Raible is a Web Architecture Consultant specializing in open source frameworks.
« May 2012
SunMonTueWedThuFriSat
  
1
2
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
23
24
25
26
27
28
29
30
31
  
       
Today

Recent Entries

Tag Cloud