REST风格-员工管理系统
首先搭建springmvc框架
1.加入jar包
2.在web.xml中用插件完成dispatcherservlet的配置
3.在src下建立springmv.xml
导入context的命名空间
<context:component-scan base-package=""></context:component-scan>
<bean class=org.springframework.web.servlet.view.InternalResouceViewResolver">
<property name="prefix"value="/"></property>
<property name="suffix"value=".jsp"></property>
</bean>
先分析需要2张表,一张是employee,一张是department表
为了完成ORM
1.需要在src下建立一个包,包中包含2个类,Employee和Department类
Department类中代码包括integer dept id; string deptname;用setter和getter 生成。(有参和无参构造器)
Employee类中代码包括integer id; string name; string mail,integer gender, Department dept 用setter和getter 生成。(有参和无参构造器)
2.需要在src下建立一个包放DAO
建立一个class叫EmployeeDAO和Department DAO,所有的增删改查都放在此类写。
3.Department DAO
public class Department DAO{
private static Map<Integer,Department> depts;
static
{depts=new HashMap<Integer,Department>
depts.put(1,new Department (1,"sales"));
depts.put(2,new Department (2,"HR"));
}
public Map<Integer,Department> getAlldepts(){
return depts;
}
**public static Department getDeptById( Integer id )**{
return depts.get(id );
}
}
EmployeeDAO代码
public class EmployeeDAO{
private static Map<Integer,Employee> emps;
static
{emps=new HashMap<Integer,Employee>
emps.put(101,new Employee (101,"AAA","AA@QQ.COM",0,Department DAO.getDeptById(2));
emps.put(102,new Employee (102,"BB","BB@QQ.COM",0,Department DAO.getDeptById(1));
emps.put(103,new Employee (103,"CC","CC@QQ.COM",0,Department DAO.getDeptById(1));
emps.put(104,new Employee (104,"DD","DD@QQ.COM",0,Department DAO.getDeptById(2));
}
//获取所有的员工,map集合来完成
public static Map<Integer,Employee> getAlldepts(){
return emps;
}
public static Collection <Employee> getAlldepts(){
return emps.values();
}
private static Integer key=106;
public static void save(Emloyee emp)
{ emp.setId(key);
emp.setDept(Department DAO.getDeptById(emp.getDept().geDeptId()));
emps.put(key++,emp);}
在webcontent中新建页面index.jsp
<a href="allEmps">list all employee</a>
在src中建立一个包,其中放入handler,加入一个类叫EmployeeHandler,
@controller
public class EmployeeHandler{
@RequestMapping("/allEmps",method=RequestMethod.GET)
public String getAllEmps( Map<String,Object> map){
map.put("emps",EmployeeDAO.getAllEmps());
return "success";
}
}
在success.jsp页面(JSTL)
<%@ tglib uri="指定包" prefix="c">
<c:if test="${empty requestScope.emps}">
<h3 align="center">NO EMPLOYEES<h3>
</c:if>
<c:if test="${!empty requestScope.emps}">
<h3 align="center">the employees
list<h3>
<table width="500px' border="1",align="center"
<tr>
<td>id</td>
<td>name</td>
<td>maill</td>
<td>gender</td>
<td>dpartment</td>
<td>delete</td>
<td>update</td>
</tr>
<c:foreach items="${reuqestScope.emps}" var="emp">
<tr>
<td>${emp.id}</td>
<td>${emp.name}</td>
<td>${emp.mail}</td>
<td>${emp.gender==0?"male":"femal"}
<td>${emp.dept.deptname}</td>
<td><a>delete</a></td>
<td><a>update</a></td>
</tr>
</table>
</c:if>
员工信息增加页面
gender和department是用户选择,不是随意输入的
在点击增加超级链接不是直接跳转到一个jsp页面,点击超级链接的时候,访问了一个业务方法,帮我们查询出来的所有的gender和department,放在请求范围中,跳转到目标的页面,目标页面完成。
在index.jsp中增加
<a href ="input"> add the employee</a>
在handler类中增加方法
@RequestMapping(value="/input",method=RequestMethod.GET)
public String input(Map<String,Object> map){
//获得所的性别
Map<Integer,Object> genders=new HashMap<Intege,Object>();
genders.put(1,"male');
genders.put(2,"female');
map.put("genders",genders);
**map.put("command",new Employee);**
// **map.put("abc",new Employee);**
map.put("depts",,EmployeeDAO.getAllEmps);
//获取所有的部门信息
return"input";
}
在input.jsp页面中
<%@ tglib uri="指定包" prefix="c">
<form:form action="" method='' "modelAttribute="">
</form:form>
<form >
<table>
<tr>
<td>name</td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td>mail</td>
<td><input type="text" name="mail"/></td>
</tr>
<tr>
<td>gender</td>
<td><input type="radio" name="gender" value="0"/> Male<input type="radio" name="gender" value="1"/><Femal</td>
</tr>
<td>Department</td>
</tr>
</table>
</form>
传统的JSP完成从服务器获取的数据,显得非常复杂,form表的表单标签库,来代替所有的表单标签,
可以完成回显的。怎么完成回显
可以代替JST中的一些复杂操作
使用流程:
1.引入form表单标签库
2.常用的标签
form:form可以完成回显操作,modelAttribute="完成回显,key自动到请求范围中需要key对应的value值,没有指定modelAttribute,springmvc会自动到请求的范围中去寻找一个叫command这样的key对应的对象,如果没有command会报错
<%@ tglib uri="指定包" prefix="c">
<form:form action="" method='' post"modelAttribute="abc">
<table>
<tr>
<td>name</td>
<td>< form:input path="name"/></td>
</tr>
<tr>
<td>mail</td>
<td><form: input path="mail"/></td>
</tr>
<tr>
<td>gender</td>
<td><form:radiobuttons path="gender" items="${requestScope.genders}"</td>
</tr>
<tr>
<td>department</td>
<td><form: select path="dept.deptname "items="${requestScope.depts}" itemlabel="deptname" iteamValue="deptid"/></td>
</tr>
</form:form>
< form:input path=“name”/> 文本框,path为name 属性值
单选按钮组<form:radiobuttons path=“gender” 属性名称items=${requestScope.genders}遍历集合,如果为map类型,map当中key作为每一个单选按钮的value值,map里面的value,对应单选按钮的显示内容
注:form:form:表单的回显,modelAttribute属性指定回显的数据,request范围查找,modelAttribute属性没有设置,去找command,找不到command会报错