Issue Details (XML | Word | Printable)

Key: SPR-3266
Type: New Feature New Feature
Status: Open Open
Priority: Major Major
Assignee: Unassigned
Reporter: Brian Lewis
Votes: 9
Watchers: 8
Operations

If you were logged in you would be able to see more operations.
Spring Framework

StringTemplate view support

Created: 15/Mar/07 08:35 AM   Updated: 16/Aug/07 10:53 AM
Component/s: SpringCORE
Affects Version/s: None
Fix Version/s: None

Time Tracking:
Not Specified

File Attachments: 1. File stringtemplate_example.tgz (7 kB)



 Description  « Hide

org.springframework.web.servlet.view.stringtemplate to support StringTemplate ( http://www.stringtemplate.org/ ).



Brian Lewis added a comment - 29/Mar/07 06:04 PM

Here is a maven project that demonstrates some new classes related to using StringTemplate in Spring. Hopefully it will demonstrate to some degree how powerful StringTemplate is and how desirable it would be to have view stuff for it in Spring.

Run it via
mvn clean compile war:war jetty:run

You may need to add the following to your .m2/settings.xml:
<settings>
<pluginGroups>
<pluginGroup>org.mortbay.jetty</pluginGroup>
</pluginGroups>
<mirrors>
<mirror>
<id>lsu.edu</id>
<url>http://ibiblio.lsu.edu/main/pub/packages/maven2</url>
<mirrorOf>central</mirrorOf>
<!-- United States, Louisiana -->
</mirror>
<mirror>
<id>ibiblio.net</id>
<url>http://www.ibiblio.net/pub/packages/maven2</url>
<mirrorOf>central</mirrorOf>
<!-- United States, North Carolina -->
</mirror>
</mirrors>
</settings>

StringTemplate strictly enforces model-view separation. Not much actual programming or computation is allowed in the StringTemplate templates.

For example, we cannot use special tags like the JSP form:* tags to bind to get error messages inside the view. Instead, we put all the form error messages in a map before showing the view.

We have a property editor for Date. We don't know exactly how to work between Spring and StringTemplate to get strings back out of objects through property editors. For example, in our example application, "12/12" gets converted to a Date, but displays as "Sat Dec 01 00:00:00 CST 2012". There doesn't currently seem to be a way in StringTemplate to know the full path of something, which is needed to bind. We are willing to coordinate with the StringTemplate people to get new functionality added if needed, but we don't know what to ask for.

Although it is not demonstrated in this web application, things like drop down list boxes (the "select" HTML tag) are somewhat difficult because the previously selected option must be marked with a selected="selected" HTML attribute, but StringTemplate (by design) cannot check for equality. For example, in a drop down list box for "state", we might know that "CA" was the previously selected option, but that does us no good in the view. We would have to put a data structure in the model with tuples like ["CA", "California", selected=true] so that the template would be able to generate the options and add the selected="selected" to the correct option.


Brian Lewis added a comment - 29/Mar/07 08:21 PM

It is probably also very important to do the equivalent of c:out to all values being sent into templates, because that is not being done in StringTemplate itself.


Robert Varga added a comment - 16/Aug/07 10:47 AM

The equality checking can be hacked together:

1. You can use any object implementing Map as a parent of a property, where the property name would be the key.
2. You can specify property names indirectly.

Now, the following class will check equality for you:

public class EqualityCheckerTool extends DummyMap {
public static final EqualityCheckerTool INSTANCE = new EqualityCheckerTool();
private static class WithValueComparer extends DummyMap {
private Object valueToCompare;
private WithValueComparer(Object valueToCompare) { super(); this.valueToCompare = valueToCompare; }
public Object get(Object key) { return valueToCompare.equals(key) ? Boolean.TRUE : Boolean.FALSE; }
}
public Object get(Object key) { return new WithValueComparer(key); }
private EqualityCheckerTool() {}
}

DummyMap is an abstract base class, which throws UnsupportedOperationException for everything except it does not define get(Object) and returns true for containsKey(), and returns false for isEmpty().

Now, you should be able to use it this way in a string template:

$equalityCheckerTool.(selectedKey).(listElement.key)$

where the selectedKey attribute should be set to the selected combo box key, and the "listElement" should be replaced with the attribute name iterating the entry-set.

Also the equalityCheckerTool attribute should be set to EqualityCheckerTool.INSTANCE.

I think this would work for equality checking, although it is an ugly hack and generates garbage.

Possibly there are other approaches to it.


Robert Varga added a comment - 16/Aug/07 10:51 AM

Forgot to mention, that $equalityCheckerTool.(selectedKey).(listElement.key)$ should be used as the condition in the $if, so the full usage would be:

$if(equalityCheckerTool.(selectedKey).(listElement.key))$
....
$else$
...
$endif$

Best regards,

Robert


Robert Varga added a comment - 16/Aug/07 10:53 AM

Also forgot to mention that DummyMap also implements java.util.Map, although it was deducible...

BR,

Robert