日期:2014-05-16 浏览次数:20478 次
因为这是只学习项目,所以我没有使用数据库了,直接在Action模式几条数据了。如果使用数据库,那代码量就大了,而这个项目只是学习整合Struts2,所以不用,大家自己确定,
?
数据我是使用static来完成,只要服务器不重新启动,数据就会在,哈哈,方便呀……
?
如何大家真不想自己输入代码,就下载吧。见附件。
?
实际后台代码也是非常的简单,只有几个类User/Dept/UserAction/DeptAction
大家自己建立相应的包
?
?
代码如下:
?
1、Dept类
package com.wjt276.extjs.model;
public class Dept {
private int id;
private String name;
private String description;
public Dept(int id, String name, String description) {
super();
this.id = id;
this.name = name;
this.description = description;
}
public Dept() {
super();
}
//……我省了getter/setter方法,在项目中大家自己生成------
}
?2、User
?
package com.wjt276.extjs.model;
public class User {
private int id;
private String username;
private String name;
private String password;
private boolean sex;
private String phone;
private String tel;
private String url;
private String email;
private Dept dept;
private String address;
private String description;
public User() { }
public User(int id, String username, String name, String password,
String phone, String tel, String url, String email,
String address, boolean sex, String description) {
super();
this.id = id;
this.username = username;
this.name = name;
this.password = password;
this.phone = phone;
this.tel = tel;
this.url = url;
this.email = email;
this.address = address;
this.sex = sex;
this.description = description;
}
//……我省了getter/setter方法,在项目中大家自己生成------
}
?
3、DeptAction
数据也是在这里生成的,我多加了几个对象,是为了在前台观查数据,可以不是太好,大家不要管它
package com.wjt276.extjs.action;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.opensymphony.xwork2.ActionSupport;
import com.wjt276.extjs.model.Dept;
@SuppressWarnings("serial")
public class DeptAction extends ActionSupport {
static SecureRandom random = new SecureRandom();
private Dept dept;
private String msg;
private boolean success;
private int totalProperty;
private List<Dept> depts = new ArrayList<Dept>();
private Map<Integer, Dept> deptsMap = new HashMap<Integer, Dept>();
private static List<Dept> lists = new ArrayList<Dept>();
private static Map<Integer, Dept> maps = new HashMap<Integer, Dept>();
static {
Dept temp = new Dept(random.nextInt(100), "技术部", "技术……");
Dept temp1 = new Dept(random.nextInt(100), "技术部5", "技术…55…");
lists.add(temp);
lists.add(temp1);
maps.put(temp.getId(), temp);
maps.put(temp1.getId(), temp1);
}
@Override
public String execute() throws Exception {
this.depts = lists;
this.deptsMap = maps;
this.success = true;
this.totalProperty = this.depts.size();
return SUCCESS;
}
public String add() throws Exception {
System.out.println(dept.getName() + "," + dept.getDescription());
Dept deptTemp = new Dept(random.nextInt(100),dept.getName(),dept.getDescription());
lists.add(deptTemp);
maps.put(deptTemp.getId(),deptTemp);
this.success = true;
return this.execute();
}
public String findDeptById(){
this.dept = maps.get(dept.getId());
this.success = this.dept != null;
return SUCCESS;
}
public String delete() {
return SUCCESS;
}
public String modify() throws Exception {
if(this.dept == null){
this.success = false;
this.msg = "数据不合法……";
} else {
Dept tempDept = maps.get(dept.getId());
this.success = tempDept != null;
if(this.success){
maps.put(this.dept.getId(), this.dept);
for(int i = 0; i < lists.size(); i++){
Dept dept3 = (Dept)lists.get(i);
if(this.dept.getId() == dept3.