Interview Questions


Spring JDBC

In this section, we will discuss about how to perform (CRUD) operations in relational database using Spring JDBC.

For Example, Lets assume there exists a USERS table in a database with columns such as USER_ID, FIRST_NAME,LAST_NAME,USER_NAME, PASSWORD, COUNTRY, EMAIL AND STATE. Obviously USER_ID is the primary key(unique identifier).

Lets see how to perform DB operations in the USER table one by one.

Define a USERDao interface and USER bean(POJO) as follows

UserDao Interface

package in.techdive.spring.examples;

public interface UserDao

Simple Form Controller - Multiple Drop Down List

In this section, we will see how to populate a drop down list based on item selection in previous drop down list box using Spring Simple Form Controller.

For example, consider that there are two drop down lists "Countries" and "States".

Based on the country selected in "Countries" drop down list, the "States" drop down list should be populated with that country's respective states.

Create a Registration controller as follows

RegistrationController class


Spring Simple Form Controller

A Simple Form Controller is a controller class used for handling request submitted from a form in UI. It also contains methods to provide default data to the form, when it is displayed in UI.

The unique feature of SimpleFormController is it allows form views and success view to be handled decoratively compared to AbstractFormController.

Lets see a simple example of creating a User Details Form to use Simple Form Controller. It will contain fields such as User Name, Age and a Submit button. It will also contain current date on top of the page.

Create a custom controller as follows

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