Spring - Search Results using Display Tag

Display Tags are used in web applications to display a list of records in UI with pagination. It just takes a collection as input to the tag and displays the items in the list, limiting the records as per the page size. Display tags can be used with any UI framework like spring, struts etc., One common problem faced by developers (using spring framework) while displaying search results in same page is that if the subsequent pages in the paginated list are accessed, the search criteria will be lost. The search criteria will not be stored in session in spring MVC.

To solve this problem, we need the post the request every time when nth page is accessed in paginated list. This can be done by having a intermediate JSP, which will post the request to the same controller. In the spring controller, we can have a flag to check whether the request is coming for search results or request for an nth page.

Have a look at the search page JSP as follows.

userSearch.jsp

<form:form commandName="user" method="post" action="searchUser.htm" id="searchUser" >
        <form:label path="firstName">First Name/Last Name: </form:label>
        <form:input path="firstName" />
        <form:label path="username">User Name:</form:label>
        <form:input path="username" />

        <div class="rAlign">
            <input type="submit" name="Submit" value="<fmt:message key="button.search"/>" class="button">
        </div>

       <input type="hidden" id="formaction" name="formaction" value="customer/searchUser.htm" >
</form:form>

<c:if test="${not empty Results}">
        <display:table
                name="${sessionScope.usersLst}" export="false" id="srchUser"
                pagesize="20" requestURI="/commonDisplayTagRedirect.jsp?paging=true">

                <display:column style="width: 15%" sortable="false"
                        titleKey="userForm.userName" property="username" >
                </display:column>
               
                <display:column style="width: 15%" sortable="false"
                        titleKey="userForm.firstName" property="firstName">
                </display:column>
               
                <display:column style="width: 15%" sortable="false"
                        titleKey="userForm.lastName" property="lastName">
                </display:column>
                               
                <display:column style="width: 15%" sortable="false"
                        titleKey="userForm.emailID" property="email">
                </display:column>
               
                <display:column style="width: 15%" sortable="false"
                        titleKey="group.name"  >
                        <c:out value="${srchUser.group.Name}"/>
                </display:column>
    </display:table>
</c:if>

CommonDisplayTagRedirect.jsp

<script type="text/javascript">
function submitform() {
        document.forms[0].action="${param.formaction}";
        document.forms[0].submit();
}
</script>

<form id="redirectform"  method="post" >
    <c:forEach var="pname" items="${pageContext.request.parameterNames}">  
        <input type="hidden" name="${pname}" value="<c:out value='${param[pname]}'/>" />
    </c:forEach>  
</form>        

<script type="text/javascript">
    submitform();
</script>                      

Have a look at the controller class.

SearchUserController.java

@Controller
@RequestMapping("customer/searchUser")
public class SearchUserController
{
        @ModelAttribute
        protected User showForm(HttpServletRequest request) throws Exception
        {
                User user = new User();
                HttpSession session = request.getSession();
                session.removeAttribute("usersLst");
                return user;
        }
       
        @RequestMapping(method = RequestMethod.POST)
        public ModelAndView onSubmit(User user, BindingResult errors, HttpServletRequest request) throws Exception
        {
                ModelAndView modelView = new ModelAndView("customer/searchUser");
                String paging = (String) request.getParameter("paging");
               
                request.setAttribute("Results", "true");
                if (paging != null)
                {
                        return modelView;
                }

                List<User> usersLst = null; //Method to search for the user list
                HttpSession session = request.getSession();
                session.removeAttribute("usersLst");
                session.setAttribute("usersLst", usersLst);
                return new ModelAndView("customer/searchUser");
        }
}

In the above class, have a look at the onSubmit() method, it returns the request to the same page with out processing, when the paging attribute is set to true. In this way we can make a post request, whenever pages in a paginated list are navigated using display tags.

Technology: 

Search