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:
@Override
protected Object formBackingObject(HttpServletRequest request) throws ServletException {
SearchForm command = new SearchForm();
command.setName("Default_");
command.setPageSize("10");
return command;
}


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

@Override
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.

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


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


DisplayTag:

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

<dependency>
<groupId>displaytag</groupId>
<artifactId>displaytag</artifactId>
<version>1.1</version>
</dependency>


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 http://devx.com 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="command.name">
<input type="text" name="${status.expression}" 
id="name" value="${status.value}" />
</spring:bind>

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

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


</form>

<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}" />
</c:if>
<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 }">
selected="selected"
</c:if> >${localPageSize}
</option>
</c:forTokens>
</select>
</spring:bind>


<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" />
</display:table>

</div>



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

7 comments:

Divya said...

Hi,

Can u please send me the full coding along with needy jars.. Asap...

Thanks in advance

Thilsen said...

Your posting is very nice, especially your my link, please change the theme it is not visible clearly

roberto.ciuccigiuliani@gmail.com said...

Good article, thanks

John Alert said...

Java Training Institutes Java Training Institutes Java Training in Chennai | Java Training in Chennai | Dot Net Training in Chennai JavaScript Training in CHennai JavaScript Training in CHennai | | Single Page Application Development

John Alert said...

I have read your blog its very attractive and impressive. I like it your blog.

Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

Java Online Training Java Online Training Core Java 8 Training in Chennai Java 8 Training in Chennai JavaEE Training in Chennai Java EE Training in Chennai

John Alert said...

Java Spring Hibernate Training Institutes in Chennai | Hibernate Training Institutes in Chennai Spring Training Institutes in Chennai Spring Training Institutes in Chennai

Tanu Chauhan said...

Valuable information. I am delighted to read this article. Thank you for giving us this useful information. networking training in jalandhar