Dashboard > Spring Discuss > Working with Spring MVC
  Spring Discuss Log In View a printable version of the current page.  
  Working with Spring MVC
Added by Seth Ladd, last edited by Matt Parker on Jul 05, 2005  (view change)
Labels: 
(None)

I've been doing a lot of work with Spring MVC, and while some of it maps to things I've done with other frameworks like Struts, other things are different. That is, different enough for me to have to read source code, search the forums, and browse Javadocs.

In an effort to share what I've learned (and in hoping to get others to do the same), I'll attempt to catalogue tips and tricks with Spring MVC.

I've found it useful to actually wrap the whole <spring:bind> <input xxx> stuff into a tag that is similar to those found in struts. For example, I have a WEB-INF/tags/form/text.tag file that is the following:

<%@ attribute name="property" required="true" %>
<%@ attribute name="size" required="false" type="java.lang.Integer" %>
<%@ attribute name="max" required="false" type="java.lang.Integer" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="spring" uri="/spring" %>

<c:if test="${size==null}">
  <c:set var="size" value="14"/>
</c:if>

<c:if test="${max==null}">
  <c:set var="max" value="${size*2}"/>
</c:if>

<spring:bind path="${property}">
<input type="text" name="${status.expression}" value="${status.value}" size="${size}" maxlength="${max}" />
</spring:bind>

I have similar tag files for other form elements, as well as for errors like so:

<%@ attribute name="property" required="true" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="spring" uri="/spring" %>

<spring:bind path="${property}">
    <c:if test="${status.errorMessage!=''}">
        <font color="red">${status.errorMessage}</font>
    </c:if>
</spring:bind>

Then, creating a spring bound form is as simple as with Struts:

<%@ taglib tagdir="/WEB-INF/tags/form" prefix="form" %>
<html><body>
<form>
	<form:error property="application.name.first"/><br>
	<form:text property="application.name.first"/><p>

	<form:error property="application.name.last"/><br>
	<form:text property="application.name.last"/><p>
	<etc/>
</form>
</body>
</html>

Hope that helps,
Jon

It does, and we have the same tags over here. Luckily, the <spring:setNestedPath> tag would really help out with your examples, too. We have a select and option tagfile as well. I think those will be the next write up.

I can see several problems with including these types of tags in the framework, principally that they output markup that may be highly application specific.

Something as simple as attempting to use a BR tag for example: do you use the <br> or <br /> syntax? Well it depends entirely on what the DOCTYPE of the final output has been specified as. One would break XHTML validation, the other may break HTML 4.x validation.

Short of passing in parameters allowing every aspect of style to be customized (ie I would never use a 'size="X"' attribute on an input field, always a 'style="width:000px"' instead) I'm unsure of how this becomes generic enough to be frameworked. And if you do take that route, the value of using the tags in the first place becomes substantially reduced in terms of clarity and amount of typing.

Any thoughts?

Actually, if you look at the actual tag examples, no markup is included in the tag lib. Those <br>'s are in the page using the tag lib. The tag itself only ouputs a properly spring bound <input> element. Nothing more, nothing less.

Also, it would be simple enough to add a style or class attribute to the tag as well, which should allow the formatting options for the form element.

With JSP 2.0 tags you can use dynamic attributes to pass arbitrary attributes to the tag. With this feature, the tag developer does not need to think of every attribute that a user may want add to the resulting markup. The syntax is <%@tag dynamic-attributes="varName"%> where varName is the name of the map. Attribute names and values can be accessed inside a <c:forEach> loop by accessing the loop variable's "key" and "value" properties.

Daniel Miller

Posted by Anonymous at Jun 25, 2004 21:40

Exist spring's tags that are similar to those found in struts for creating controls for nested properties? (control[0].property naming style)

Thanks

Hi Seth. You mentioned (way back on May 19th), "We have a select and option tagfile as well. I think those will be the next write up." Did you ever write those up?

I'm currently migrating from struts to spring and finding that a lot of the view side that I took for granted in struts, I have to write myself with spring.

A more complete tld would be great, but right now I'd settle for select/option binding without me having to reinvent the wheel by writing my own tld. Any info you or anyone has would be greatly appreciated. Thanks!

Posted by Anonymous at Oct 09, 2004 22:42

Hmm didn't work for me: Tomcat 5 doesn't much like this:

<%@ taglib tagdir="/WEB-INF/tags/form" prefix="form" %>

Posted by Anonymous at Oct 18, 2004 14:18
Site running on a free Atlassian Confluence Open Source Project License granted to Spring Framework. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.5 Build:#811 Jul 25, 2007) - Bug/feature request - Contact Administrators