database spring registration form

We are going to discuss about spring registration form. We have created simple spring registration form with many fields. Here we can store all data in database.

database spring registration form

database spring registration form

We are going to discuss about  database spring registration form. We have created simple spring registration form with many fields. Here we can store all data in database. We then create jsp registration form. We have used controller and we are using database connectivity and we have created interface and implementation of class.

Following is the example of simple spring registration form:-

We have created this database which you can copy and use it.

CREATE TABLE `registrationform` (                        
                    `rollNno` int(11) NOT NULL AUTO_INCREMENT,             
                    `name` varchar(30) DEFAULT NULL,                       
                    `userid` varchar(50) DEFAULT NULL,                     
                    `password` varchar(20) DEFAULT NULL,                   
                    `email` varchar(20) DEFAULT NULL,                      
                    `address` varchar(20) DEFAULT NULL,                    
                    `city` varchar(30) DEFAULT NULL,                       
                    `state` varchar(30) DEFAULT NULL,                      
                    `phoneno` varchar(20) DEFAULT NULL,                    
                    PRIMARY KEY (`rollNno`)                                
                  ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1  

 

  1.  We have created model bean class "Form. java" and we are creating many variables field  name, Userid, password, email, address, city, state, phoneNo and we are making setter and getter method all variable fields like as:-
  • Form .java
package model;

public class Form {
	private Integer rollno;
	private String name;
	private String Userid;
	private String password;
	private String email;
	private String address;
	private String city;
	private String State;
	private String phoneNo;

	public Integer getRollno() {
		return rollno;
	}

	public void setRollno(Integer rollno) {
		this.rollno = rollno;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getUserid() {
		return Userid;
	}

	public void setUserid(String userid) {
		Userid = userid;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getState() {
		return State;
	}

	public void setState(String state) {
		State = state;
	}

	public String getPhoneNo() {
		return phoneNo;
	}

	public void setPhoneNo(String phoneNo) {
		this.phoneNo = phoneNo;
	}
}

2  Now we create model bean class "City.java". In this class we are creating two variables name id and name. We have all variables name generate setter and getter method. We use this for class initiation values and set and get values.

  • City.java
package model;

public class City {
	private int id;
	private String name;

	public City() {
	}

	public City(int id, String name) {
		super();
		this.id = id;
		this.name = name;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

Now Next Step

3 We have created controller class "MainController.java" to handle all form and @RequestMapping we used for implementation of all controller interface.

  • MainController.java
package controller;

import java.util.List;

import model.City;
import model.Form;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import Service.AppServiceImp;
import Service.Demo;

@Controller
public class MainController {
@Autowired
private Demo service;

public void setService(AppServiceImp appServiceImp) {
this.service = appServiceImp;
}

@ModelAttribute("city")
public List<City> loadAllCity() {
return service.loadAllCity();
}

@ModelAttribute("state")
public java.util.List<String> loadAllState() {
return service.loadAllState();
}

@RequestMapping("/load-form")
public String loadForm(Model model, Form field) {
model.addAttribute("field", field);
return "rgistration";
}

@RequestMapping("/process-form")
public String processForm(Model model, Form field) {
model.addAttribute("field", field);
service.addStudent(field);
return "Success";
}
}
  •   StudentMapping.java

4 Now We are StudentMapping class we implements RowMapper and we have used resultSet method it mens set of all value  in variable field.

package domain;

import java.sql.SQLException;

import model.Form;

import org.springframework.jdbc.core.RowMapper;

import com.mysql.jdbc.ResultSet;

public abstract class StudentMapping implements RowMapper<Form> {

public Form mapRow(ResultSet resultSet, int rowNum) throws SQLException {
Form student = new Form();
student.setRollno(resultSet.getInt("rollNno"));
student.setName(resultSet.getString("name"));
student.setUserid(resultSet.getString("Userid"));
student.setPassword(resultSet.getString("password"));
student.setEmail(resultSet.getString("email"));
student.setAddress(resultSet.getString("address"));
student.setCity(resultSet.getString("city"));
student.setState(resultSet.getString("State"));
student.setPhoneNo(resultSet.getString("phoneNo"));
return student;
}
}
  • rgistration.jsp

5 We are create user registration jsp form we are many field create Name,UserId, Password, Emailid, Address ,City ,State, and phoneNo and we cac user store all values in database click by submit button.

<%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<head>
<title>Registration Form</title>
</head>
<body>
<h2 align="center">Student Registration Form</h2>
<hr />
<table align="center" cellpadding="5" cellspacing="5">
<form:form commandName="field" method="POST" action="process-form">

<tr>
<td>Name</td>
<td><form:input path="name" />
</tr>

<tr>
<td>UserId</td>
<td><form:input path="Userid" />
</tr>

<tr>
<td>Password</td>
<td><form:password path="password" />
</tr>

<tr>
<td>Email Id</td>
<td><form:input path="email" />
</tr>

<tr>
<td>Address</td>
<td><form:textarea path="address" />
</tr>
<tr>
<td>City</td>
<td><form:select path="city">
<form:option value="" label="Please Select" />
<form:options items="${city}" itemValue="name" itemLabel="name" />
</form:select><br />
<font color="red"><form:errors path="city" /></font></td>
</tr>

<tr>
<td>State</td>
<td><form:select path="state" items="${state}" /><br />
<font color="red"><form:errors path="state" /></font></td>
</tr>
<tr>
<td>PhoneNo</td>
<td><form:input path="phoneNo" />
</tr>
<tr>
<td><input type="submit" name="save" /></td>
</tr>
</form:form>
</table>
</html>
  • dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">


<context:component-scan base-package="*" />

<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/view/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>

<bean id="myAppDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/naulej" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="myAppDataSource" />
</bean>

<bean id="service" class="Service.AppServiceImp">
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>

<bean
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="roseindia.exception.SpringException">
errorPage
</prop>
</props>
</property>
</bean>

</beans>
  • web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>springJDBCExamples</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>

</welcome-file-list>
</web-app>

Download Source Code