Saturday, September 12, 2009

Hands on with SimpleFormController and DisplayTag

Recently, I got to work on SimpleFormController (Spring Framework class for handling web Form) with DisplayTag . It took me a while to get used to SimpleFormController. I should also mention that at first look it might seem lot of mess instead of trying to solve your problem, hey won't you get that feeling whenever you are trying to use any new technology??

I had to use debugger to understand the flow of control from various method. But you can follow this cheat sheet for complete flow diagram for SimpleFormController. You can inherit lot of methods based on your requirements, however following methods are the usual ones.

protected Object formBackingObject(HttpServletRequest request) throws ServletException;

public ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception;

protected boolean isFormSubmission(HttpServletRequest request);

protected Map referenceData(HttpServletRequest request) throws Exception;

formBackingObject : will be called by createCommand method from SimpleFormController. On form request, if sessionForm is set to true the model object created by formBackingObject is put in session. On form submit, the object is taken from session and not created once again.

Sample code:
protected Object formBackingObject(HttpServletRequest request) throws ServletException {
SearchForm command = new SearchForm();
return command;

onSubmit: All form submit logic have to go into this. I would suggest you to go through javadoc for all onSubmit() variant.

public ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response,
Object command, BindException errors) throws Exception {
SearchForm searchParam = (SearchForm) command;
System.out.println("your input is ... " + searchParam);

ModelAndView mv = null;
mv = showForm(request, errors, getSuccessView());
String pName = searchParam.getName();
if (pName == null || pName.length() == 0) {
pName = "Default_"; 
ProductSearch st = new ProductSearch();
st.setProducts(Util.getTableData(pName, 50));
HttpSession session = request.getSession();
session.setAttribute("TABLE_DATA", st.getAllProducts());
session.setAttribute("pageSize", searchParam.getPageSize());
System.out.println("done search ..... displaying results");
return mv;

Here, you can also include a check to see if the request is for DisplayTag pagination or actual form submission and you don't have to form submit logic for pagination requests.

isFormSubmission: Default implementation of this method returns true when request method is "POST". You can override this to change its behavior. You would have to do this when you want to use DisplayTag pagination. I have given the sample code below.

protected boolean isFormSubmission(HttpServletRequest request) {
return !request.getParameterMap().isEmpty();

referenceData: I didn't have to implement this method for my hands on.


DispalyTag provides lot of feature for high-level web presentation patterns like paging, cropping, sorting and exporting etc. The implementation with spring is pretty neat. All you have to do is add below dependency in your pom.xml


Or add displaytag-1.1.jar jar file into WEB-INF/lib. You are all set. For other configuration options, go through DisplayTag documentation.

I found this useful article on on Displaytag.

For my hands on , the JSP looks like the one below with DisplayTag code.

<form  name="product_search" action="formpage.html" method="POST">    
Name: <spring:bind path="">
<input type="text" name="${status.expression}" 
id="name" value="${status.value}" />

Type: <spring:bind path="command.type">
<input type="text" name="${status.expression}" 
id="address" value="${status.value}" />

<input type="submit" value="Submit"/>
<input type="hidden" value="${pageSize}" name="pageSize" />


<c:set var="searchResultPageSizeOptions" value="10,25,50" />
<c:set var="selectedValue" value="10" />
<spring:bind path="command.pageSize">

<c:if test="${!empty pageSize}">
<c:set var="selectedValue" value="${pageSize}" />
<select name="<c:out value="${status.expression}"/>" id="pageval" onchange="javascript:changePageSize();">
<c:forTokens items="${searchResultPageSizeOptions}" delims=","
var="localPageSize" varStatus="status">
<option value="${localPageSize}" <c:if test="${selectedValue == localPageSize }">
</c:if> >${localPageSize}

<display:table name="sessionScope.TABLE_DATA" pagesize="${pageSize}" requestURI="formpage.html" class="results-table">
<display:column property="id" title="ID" sortable="true" />
<display:column property="name" title="Name" sortable="true" />
<display:column property="type" title="Type" sortable="true" />


I hope this would be helpful. Please post your comments.