Springmvc-2 (rest-style additions and deletions)-code (1)

Source: Internet
Author: User

Departmentdao.java:

Package Com.springmvc.Dao;

Import java.util.Collection;
Import Java.util.HashMap;
Import Java.util.Map;

Import com.springmvc.pojo.Department;

public class Departmentdao {
private static map<integer,department> depts;
static {
Depts=new hashmap<integer,department> ();
Depts.put (1, New Department (1, "saledepartment"));
Depts.put (2, New Department (2, "HR"));
}
public static collection<department>getalldepts () {
return Depts.values ();
}
public static Department Getdeptbyid (Integer ID) {
return Depts.get (ID);
}

}

--------------------------------------------------------------------------------------------------------------- ---------------------------

Employeedao.java:

Package Com.springmvc.Dao;

Import java.util.Collection;
Import Java.util.HashMap;
Import Java.util.Map;

Import Com.springmvc.pojo.Employee;

public class EmployeeDAO {
private static Map<integer, employee> Emps;
static {
Emps = new Hashmap<integer, employee> ();
Emps.put (101, New Employee (101, "Wantao", "[email protected]", 0, Departmentdao.getdeptbyid (2));
Emps.put (102, New Employee (102, "Wanxiaofei", "[email protected]", 1, Departmentdao.getdeptbyid (1));
Emps.put (103, New Employee (103, "Wanchao", "[email protected]", 0, Departmentdao.getdeptbyid (1));
Emps.put (104, New Employee (104, "Gaohan", "[email protected]", 0, Departmentdao.getdeptbyid (1));
Emps.put, New Employee ("Lihan", "[email protected]", 0, Departmentdao.getdeptbyid (1));

}

Get information about all employees, gather through the map
Traversing map,<%%> JSTL foreach in success.jsp
Map conversion to Collection
public static collection<employee> GetAllEmployees () {
return Emps.values ();
}

public static Employee Getempbyid (Integer ID) {
return Emps.get (ID);

}

private static Integer key = 106;

public static void Save (Employee emp) {
Emp.setid (key);
Emp.setdept (Departmentdao.getdeptbyid (Emp.getdept (). Getdeptid ()));
Emps.put (key++, EMP);

}

public static void Delete (Integer id) {
Emps.remove (ID);
}
public static void Update (Integer id,employee EMP) {
Emp.setdept (Departmentdao.getdeptbyid (Emp.getdept (). Getdeptid ()));
Emps.put (ID, EMP);
}

}

--------------------------------------------------------------------------------------------------------------- ----------------------------------

Employeehandler.java:

Package Com.springmvc.handler;

Import Java.util.HashMap;
Import Java.util.Map;

Import Org.springframework.stereotype.Controller;
Import Org.springframework.web.bind.annotation.ModelAttribute;
Import org.springframework.web.bind.annotation.PathVariable;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RequestMethod;

Import Com.springmvc.Dao.DepartmentDao;
Import Com.springmvc.Dao.EmployeeDao;
Import Com.springmvc.pojo.Employee;

@Controller
public class Employeehandler {
Private final String SUCCESS = "SUCCESS";
Private final String add = "Add";

@RequestMapping (value = "/showemp", method = Requestmethod.get)
Public String showemp (map<string, object> Map) {
Map.put ("Emps", Employeedao.getallemployees ());

return SUCCESS;
}

Addemp method, complete the user input function jump: 1. Add users 2. change user
@RequestMapping (value = "/addemp/{id}", method = Requestmethod.get)
Public String addemp (@PathVariable ("id") Integer ID, map<string, object> Map) {
Get all the Sex
Get all the Department information
Map<integer, object> genders = new Hashmap<integer, object> ();
Employee Employee = new Employee ();
Genders.put (0, "MALE");
Genders.put (1, "FEMALE");
Map.put ("genders", genders);
if (id! = 0) {
Employee = Employeedao.getempbyid (ID);
}

Map.put ("Depts", departmentdao.getalldepts ());
Map.put ("command", employee);

return ADD;
}

@RequestMapping (value = "/emp", method = Requestmethod.post)
Public String Add (Employee emp) {
Employeedao.save (EMP);
return "Redirect:/showemp";
}

@RequestMapping (value = "/emp/{id}", method = Requestmethod.delete)
Public String Delete (@PathVariable ("id") Integer ID) {
Employeedao.delete (ID);
return "Redirect:/showemp";
}
@ModelAttribute
public void Sex (Integer id,map<string,object> Map) {

Employee Employee=employeedao.getempbyid (ID);
Map.put ("Employee", employee);


}
@RequestMapping (value= "/emp", Method=requestmethod.put)
Public String Update (Employee EMP) {
Employeedao.update (Emp.getid (), EMP);
return "Redirect:/showemp";
}

}

--------------------------------------------------------------------------------------------------------------- -------

Department.java:

Package Com.springmvc.pojo;

public class Department {
Private Integer DeptID;
Private String Deptname;
Public Department () {

}
Public Department (Integer deptid, String deptname) {
Super ();
This.deptid = DeptID;
This.deptname = Deptname;
}
@Override
Public String toString () {
Return "Department [deptid=" + DeptID + ", deptname=" + Deptname + "]";
}
Public Integer Getdeptid () {
return deptid;
}
public void Setdeptid (Integer deptid) {
This.deptid = DeptID;
}
Public String Getdeptname () {
return deptname;
}
public void Setdeptname (String deptname) {
This.deptname = Deptname;
}

}

--------------------------------------------------------------------------------------------------------------- -----------

Employee.java:

Package Com.springmvc.pojo;

public class Employee {
private int id;
private String name;
Private String Mail;
private int gender;
Private Department Dept;
Public Employee () {

}
public Employee (int ID, string name, string mail, int gender, Department dept) {
Super ();
This.id = ID;
THIS.name = name;
this.mail = mail;
This.gender = gender;
this.dept = Dept;
}
@Override
Public String toString () {
Return "Employee [id=] + ID +", name= "+ name +", mail= "+ Mail +", gender= "+ Gender +", dept= "+ Dept
+ "]";
}
public int getId () {
return ID;
}
public void setId (int id) {
This.id = ID;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
Public String Getmail () {
return mail;
}
public void Setmail (String mail) {
this.mail = mail;
}
public int Getgender () {
return gender;
}
public void Setgender (int gender) {
This.gender = gender;
}
Public Department getdept () {
return dept;
}
public void Setdept (Department dept) {
this.dept = Dept;
}

}

Springmvc-2 (rest-style additions and deletions)-code (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.