I'm currently working with Spring 3 MVC, and in my JSP templates I'm noticing I have to repeat myself several times for things like labels, errors, layout divs etc. e.g.
<div class="field-row">
<label>Email:</label>
<form:input path="email" />
<form:errors path="email" cssClass="error" />
</div>
In Struts 2, there are themed components that, based on a theme parameter, will generate HTML code based upon the tag used. Is there an equivalent of this in Spring 3, or some alternative plugin that can be used to achieve a similar effect?
For example, in Struts 2, you could have something like
<s:textfield key="user.email" required="true" maxlength="200" theme="someTheme" />
and paired with the appropriate Freemarker Template e.g.
<div class="${rowClass}">
<#include "/${parameters.templateDir}/tgis/control-label.ftl" />
<#-- render the control -->
<input type="text" name="${parameters.name?default("")?html}"
<#if parameters.maxlength?exists>maxlength="${parameters.maxlength?html}"</#if>
.
.
<#include 开发者_如何学Go"/${parameters.templateDir}/simple/common-attributes.ftl" />
/>
<#include "/${parameters.templateDir}/tgis/control-errors.ftl" />
</div>
HTML is automatically generated with all the labels, errors etc
<div class="row">
<label for="user.email">E-mail <span id="user.email_rlabel" class="required">*</span>
</label>
<input type="text" name="user.email" maxlength="200" value="xxx@yyy.com" id="editProfile_user_email" class="inputText" />
</div>
Tag files will work well. The only issue I have found with using tag files is that they generate a ton of whitespace when they are rendered. So if you have a large form then it will generate a ton of extra whitespace within the html document.
There is a general solution in JSP(x). You need a jsp tagx file that describes your template, and then you could use it in your jspx.
If you want a complete example, than have a look at spring roo (1.1.1), it uses this technique heavily.
An example (I have simplified a roo example a bit, but I hope it still does the right thing: even if not, it illustrates what I mean):
tags/form/fields/textara.tagx:
<jsp:root xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:fn="http://java.sun.com/jsp/jstl/functions"
xmlns:spring="http://www.springframework.org/tags"
xmlns:form="http://www.springframework.org/tags/form"
xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0">
<jsp:output omit-xml-declaration="yes" />
<jsp:directive.attribute name="id" type="java.lang.String" required="true" />
<jsp:directive.attribute name="field" type="java.lang.String" required="true" />
<spring:message code="label_${fn:toLowerCase(fn:substringAfter(id,'_'))}" var="label" />
<div id="_${id}_id">
<label for="_${id}_id">
<c:out value="${label}" />
</label>
<form:textarea id="_${id}" path="${field}" />
<form:errors cssClass="errors" id="_${id}_error_id" path="${field}" />
</div>
<br />
</jsp:root>
usage in create.jspx:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<div xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:field="urn:jsptagdir:/WEB-INF/tags/form/fields"
xmlns:form="urn:jsptagdir:/WEB-INF/tags/form"
xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:spring="http://www.springframework.org/tags" version="2.0">
...
<field:textarea field="street" id="c_com_test_roo_domain_Address_street"/>
...
</div>
精彩评论