AddEmployeeController:
AddEmployeeController .java
package com.spring.jagan.addemployee;
import org.springframework.web.servlet.mvc.*;
public class AddEmployeeController extends SimpleFormController
{
EmployeeServices employeeServices;
public AddEmployeeController(EmployeeServices es)
{
employeeServices=es;
}
@Override
public void doSubmitAction(Object commnad)
{
employeeServices.create((EmpDetails)commnad);
}
}
EmpDetails .java
package com.spring.jagan.addemployee;
public class EmpDetails {
public void EmpDetails()
{
System.out.println("In EmpDetails Constructor ,Constructing a new Instance for EmpDetails");
}
private int empno,deptno,mgr;
private String name,job;
private double sal,comm;
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public int getMgr() {
return mgr;
}
public void setMgr(int mgr) {
this.mgr = mgr;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
public double getComm() {
return comm;
}
public void setComm(double comm) {
this.comm = comm;
}
}//class
EmployeeServices.java
package com.spring.jagan.addemployee;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
public class EmployeeServices {
private JdbcTemplate jdbcTemplate;
public EmployeeServices(JdbcTemplate jt)
{
jdbcTemplate =jt;
}
public void create( final EmpDetails user)
{
// TODO Auto-generated method stub
jdbcTemplate.update("insert into emp values(?,?,?,?,?,?,?,?)",new PreparedStatementSetter(){
public void setValues(PreparedStatement ps)throws SQLException
{
ps.setInt(1, user.getEmpno());
ps.setString(2, user.getName());
ps.setString(3, user.getJob());
ps.setInt(4, user.getMgr());
ps.setDate(5, new Date(System.currentTimeMillis()));
ps.setDouble(6, user.getSal());
ps.setDouble(7, user.getComm());
ps.setInt(8, user.getDeptno());
}
});
}
}
EmpVaidator.java
package com.spring.jagan.addemployee;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
/**
*
* @author Jagan moha paspula
*
*/
public class EmpVaidator implements Validator
{
/**
* @param Class
*/
public boolean supports(Class c) {
// TODO Auto-generated method stub
boolean flag =c.equals(EmpDetails.class);
return flag;
}
/**
* @param target
* @param Errors
*/
public void validate(Object target, Errors errors)
{
// TODO Auto-generated method stub
EmpDetails ud=(EmpDetails)target;
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "field.required","The user filed cant be empty");
if(ud.getDeptno()<10)
{
errors.rejectValue("deptno","field.minValue",new Object[]{Integer.valueOf(9)},"The deptno should greater than 9");
}
else if(ud.getDeptno()<10)
{
errors.rejectValue("deptno","field.maxValue",new Object[]{Integer.valueOf(99)},"The deptno should less than 99");
}
}
}
Addvariablepay.xml
<p> <?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- Configuring DataSource -->
<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@localhost:1521:orcl</value>
</property>
<property name="username">
<value>scott</value>
</property>
<property name="password">
<value>tiger</value>
</property>
</bean>
<bean id="jdbctemp" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg>
<ref local="datasource"/>
</constructor-arg>
</bean>
<bean id="employeeService" class="com.spring.jagan.addemployee.EmployeeServices">
<constructor-arg>
<ref local="jdbctemp"/>
</constructor-arg>
</bean>
<bean name ="/addEmployee.spring" class="com.spring.jagan.addemployee.AddEmployeeController">
<constructor-arg>
<ref local="employeeService"/>
</constructor-arg>
<property name="commandClass">
<value type="java.lang.Class">com.spring.jagan.addemployee.EmpDetails</value>
</property>
<property name="commandName">
<value>
EmpDetails
</value>
</property>
<property name="validator">
<bean class="com.spring.jagan.addemployee.EmpVaidator"/>
</property>
<!--property name="bindOnNewForm" value ="true"></property-->
<property name="sessionForm" value="false"></property>
<property name="formView"><value>/AddEmployee.jsp</value></property>
<property name="successView"><value>/AddemployeeSucess.jsp</value></property>
</bean>
<!--<bean id="myurlmapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key = "/login.spring">logincnt</prop>
</props>
</property>
</bean>
--></beans>
</p>
AddEmployeeController .java
package com.spring.jagan.addemployee;
import org.springframework.web.servlet.mvc.*;
public class AddEmployeeController extends SimpleFormController
{
EmployeeServices employeeServices;
public AddEmployeeController(EmployeeServices es)
{
employeeServices=es;
}
@Override
public void doSubmitAction(Object commnad)
{
employeeServices.create((EmpDetails)commnad);
}
}
EmpDetails .java
package com.spring.jagan.addemployee;
public class EmpDetails {
public void EmpDetails()
{
System.out.println("In EmpDetails Constructor ,Constructing a new Instance for EmpDetails");
}
private int empno,deptno,mgr;
private String name,job;
private double sal,comm;
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public int getMgr() {
return mgr;
}
public void setMgr(int mgr) {
this.mgr = mgr;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
public double getComm() {
return comm;
}
public void setComm(double comm) {
this.comm = comm;
}
}//class
EmployeeServices.java
package com.spring.jagan.addemployee;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
public class EmployeeServices {
private JdbcTemplate jdbcTemplate;
public EmployeeServices(JdbcTemplate jt)
{
jdbcTemplate =jt;
}
public void create( final EmpDetails user)
{
// TODO Auto-generated method stub
jdbcTemplate.update("insert into emp values(?,?,?,?,?,?,?,?)",new PreparedStatementSetter(){
public void setValues(PreparedStatement ps)throws SQLException
{
ps.setInt(1, user.getEmpno());
ps.setString(2, user.getName());
ps.setString(3, user.getJob());
ps.setInt(4, user.getMgr());
ps.setDate(5, new Date(System.currentTimeMillis()));
ps.setDouble(6, user.getSal());
ps.setDouble(7, user.getComm());
ps.setInt(8, user.getDeptno());
}
});
}
}
EmpVaidator.java
package com.spring.jagan.addemployee;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
/**
*
* @author Jagan moha paspula
*
*/
public class EmpVaidator implements Validator
{
/**
* @param Class
*/
public boolean supports(Class c) {
// TODO Auto-generated method stub
boolean flag =c.equals(EmpDetails.class);
return flag;
}
/**
* @param target
* @param Errors
*/
public void validate(Object target, Errors errors)
{
// TODO Auto-generated method stub
EmpDetails ud=(EmpDetails)target;
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "field.required","The user filed cant be empty");
if(ud.getDeptno()<10)
{
errors.rejectValue("deptno","field.minValue",new Object[]{Integer.valueOf(9)},"The deptno should greater than 9");
}
else if(ud.getDeptno()<10)
{
errors.rejectValue("deptno","field.maxValue",new Object[]{Integer.valueOf(99)},"The deptno should less than 99");
}
}
}
Addvariablepay.xml
<p> <?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- Configuring DataSource -->
<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@localhost:1521:orcl</value>
</property>
<property name="username">
<value>scott</value>
</property>
<property name="password">
<value>tiger</value>
</property>
</bean>
<bean id="jdbctemp" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg>
<ref local="datasource"/>
</constructor-arg>
</bean>
<bean id="employeeService" class="com.spring.jagan.addemployee.EmployeeServices">
<constructor-arg>
<ref local="jdbctemp"/>
</constructor-arg>
</bean>
<bean name ="/addEmployee.spring" class="com.spring.jagan.addemployee.AddEmployeeController">
<constructor-arg>
<ref local="employeeService"/>
</constructor-arg>
<property name="commandClass">
<value type="java.lang.Class">com.spring.jagan.addemployee.EmpDetails</value>
</property>
<property name="commandName">
<value>
EmpDetails
</value>
</property>
<property name="validator">
<bean class="com.spring.jagan.addemployee.EmpVaidator"/>
</property>
<!--property name="bindOnNewForm" value ="true"></property-->
<property name="sessionForm" value="false"></property>
<property name="formView"><value>/AddEmployee.jsp</value></property>
<property name="successView"><value>/AddemployeeSucess.jsp</value></property>
</bean>
<!--<bean id="myurlmapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key = "/login.spring">logincnt</prop>
</props>
</property>
</bean>
--></beans>
</p>