I’m looking for a very simple form processing API for Java. Assuming the form input fields correspond to bean properties, and all beans have javax.Validation annotations, ideally the API would:
- Display a bean as an html form
- Populate the bean, including nested objects where applicable, using the request parameters
- Validate the input using Validation annotation
- If there is an error, display errors at top of form, and highlight error fields.
Additionally:
- It would be nice if i didn’t have to buy into a whole application framework, since I am working with a legacy app.
- Allow configuration for more complicated use cases, but by default just use convention.
Bonus:
- generates javascript client side validation too.
Note: If this requires several different libraries, that is fine too.
Update:
Since I never found what I was looking for, and migrating to Spring was not an option, I went ahead and rolled my own solution. It is, affectionately, called java in jails (loosely modeled on rails form processing). It gives you dead simple (and pretty) form creation, client and server side validation, and request parameter to object mapping. No configuration required.
Example Bean:
public class AccountForm {
@NotBlank(groups = RequiredChecks.class)
@Size(min = 2, max = 25)
private String name;
//...
}
Example Form:
<%@ taglib uri="http://org.jails.org/form/taglib" prefix="s" %>
<s:form name="accountForm" action="/jails-demo/jails" label="Your Account Details" style="side">
<s:text name="name" label="Name" size="25" />
<s:text name="accountName" label="Account Name" size="15" />
...
</s:form>
Example Validation and Mapping:
SimpleValidator validator = new SimpleValidator();
if ("submit".equals(request.getParameter("submit"))) {
Map<String, List<String>> errors = validator.validate(AccountForm.class, request.getParameterMap());
if (errors != null) {
AccountForm account = validator.getMapper().toObject(AccountForm.class, request.getParameterMap());
//do something with valid account
} else {
SimpleForm.validateAs(AccountForm.class).inRequest(request).setErrors(errors);
//handle error
}
} else {
SimpleForm.validateAs(AccountForm.class).inRequest(request);
//forward to formPage
}
This is what the form looks like, with client side validation using jQuery (provided by Position Absolute):

I don’t think you will find something that has most of this functionality and is not a framework.
I can recommend Spring MVC – you can plug it in easily in the legacy app. It supports all of the above.
Doing-it-yourself won’t be that hard either:
BeanUtils.populate(bean, request.getParameterMap())to fill your object with the request parametersjavax.validation.*manually – here is how. For each error add request attributes which you can later display as errors.Note that either way you will have to write the html code manually.