Interview Questions

Spring - Drop Down List Using Simple Form Controller

In this section, we will see how to add a drop down list with values in a JSP form using Spring SimpleFormController.

Create a Registration controller as follows,

1. The user bean should be created prior to using it in our custom controller
as given in the Spring - Simple Form Controller article.

2. Override the referenceData() method as follows. It contains a list of values to be displayed in the drop down list.

RegistrationController Class

/**
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package in.techdive.spring.examples;

public class RegistrationController extends SimpleFormController
{
    private ModelMap modelMap = new ModelMap();

    public RegistrationController()
    {
        setCommandClass(User.class);
        setCommandName("user");
    }

    protected Map referenceData(HttpServletRequest request) throws Exception
    {
        List countries = new ArrayList();
        countries.add("INDIA");
        countries.add("US");
        countries.add("AUSTRALIA");

        request.setAttribute("listCountries", countries);

        return modelMap;
    }
}

The List is added in to HttpSession (or request) object so that it can be retrieved in jsp.

The following is the register.jsp file with list of fields along with the Countries drop down list. It uses the Spring tag library tag to display the list.

register.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>User Registration</title>
    </head>

    <body>
        <form:form method="POST" commandName="user" name="registerForm">
        <table>
            <tr>
                <td>First Name :</td>
                <td><form:input path="firstName" ></form:input></td>
            </tr>
           
            <tr>
                <td>Last Name :</td>
                <td><form:input path="lastName" ></form:input></td>
            </tr>
           
            <tr>
                <td>User Name :</td>
                <td><form:input path="userName" ></form:input></td>
            </tr>

            <tr>
                <td>Password :</td>
                <td><form:password path="passWord" /></td>
            </tr>
           
            <tr>
                <td>Country :</td>
                <td><form:select path="country">
                        <form:option value="0">Select</form:option>
                        <form:options items="${listCountries}" />
                    </form:select>
                </td>
            </tr>

            <tr>
                <td>email :</td>
                <td><form:input path="email" /></td>
            </tr>
        </table>
        </form:form>
    </body>
</html>

The Spring configuration file is same as the one shown in Spring - Simple Form Controller article.