因为这是只学习项目,所以我没有使用数据库了,直接在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.getId()){
lists.remove(i);
lists.add(this.dept);
System.out.println("找到");
break;
}
System.out.println(dept3.getId());
}
return this.execute();
} else {
this.msg = "数据记录不存在!";
}
}
return SUCCESS;
}
//-----------以下为Setter/getter方法--------------------------------------------------------------------------
public List<Dept> getDepts() {
return depts;
}
public void setDepts(List<Dept> depts) {
this.depts = depts;
}
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
public Map<Integer, Dept> getDeptsMap() {
return deptsMap;
}
public void setDeptsMap(Map<Integer, Dept> deptsMap) {
this.deptsMap = deptsMap;
}
public int getTotalProperty() {
return totalProperty;
}
public void setTotalProperty(int totalProperty) {
this.totalProperty = totalProperty;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public static List<Dept> getLists() {
return lists;
}
public static Map<Integer, Dept> getMaps() {
return maps;
}
}
4、UserAction
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 org.apache.struts2.json.annotations.JSON;
import com.opensymphony.xwork2.ActionSupport;
import com.wjt276.extjs.model.Dept;
import com.wjt276.extjs.model.User;
@SuppressWarnings("serial")
public class UserAction extends ActionSupport {
static SecureRandom random = new SecureRandom();
private String ids;
public String getIds() {
return ids;
}
public void setIds(String ids) {
this.ids = ids;
}
private String msg;
private Dept dept;
private boolean success;
private int totalProperty;
private User user;
private List<User> users = new ArrayList<User>();
private Map<Integer,User> usersMap = new HashMap<Integer,User>();
private static List<User> lists = new ArrayList<User>();
private static Map<Integer,User> maps = new HashMap<Integer,User>();
static {
User temp = new User(random.nextInt(100),
"wjt276",
"吴俊涛",
"19831213",
"05513238815",
"18955113096",
"http://wjt276.iteye.com",
"wjt276@126.com",
"安徽合肥淝河南路",
true,
"我是一名技术员"
);
temp.setDept(DeptAction.getLists().get(0));
lists.add(temp);
maps.put(temp.getId(), temp);
}
@Override
public String execute() throws Exception {
this.users = lists;
this.usersMap = maps;
this.success = true;
this.totalProperty = this.users.size();
return SUCCESS;
}
public String add() throws Exception{
System.out.println(this.user.getUsername() + "," + this.user.getName());
this.user.setId(random.nextInt(100));
this.user.setDept(DeptAction.getMaps().get(this.dept.getId()));
lists.add(this.user);
maps.put(this.user.getId(),this.user);
this.success = true;
return this.execute();
}
public String findUserById(){
this.user = maps.get(this.user.getId());
this.success = this.user != null;
return SUCCESS;
}
public String delete() throws Exception{
if(this.ids != null && !"".equals(this.ids.trim())){
String[] id_s = this.ids.split(",");
for(int i = 0; i < id_s.length; i++){
int id = Integer.parseInt(id_s[i]);
System.out.println(id);
for(int j = 0; j < lists.size(); j++){
User user3 = (User)lists.get(j);
if(id == user3.getId()){
lists.remove(j);
maps.remove(id);
break;
}
}
}
}
return this.execute();
}
public String modify() throws Exception{
if(this.user == null){
this.success = false;
this.msg = "数据不合法……";
} else {
User tempUser = maps.get(this.user.getId());
this.success = tempUser != null;
if(this.success){
maps.put(this.user.getId(), this.user);
for(int i = 0; i < lists.size(); i++){
User user3 = (User)lists.get(i);
if(this.user.getId() == user3.getId()){
lists.remove(i);
this.user.setDept(DeptAction.getMaps().get(this.dept.getId()));
lists.add(this.user);
System.out.println("找到");
break;
}
System.out.println(user3.getId());
}
return this.execute();
} else {
this.msg = "数据记录不存在!";
}
}
return SUCCESS;
}
//-------------以下为getter/setter方法------------------------------------------------------------------
public static SecureRandom getRandom() {
return random;
}
public static void setRandom(SecureRandom random) {
UserAction.random = random;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public int getTotalProperty() {
return totalProperty;
}
public void setTotalProperty(int totalProperty) {
this.totalProperty = totalProperty;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
public Map<Integer, User> getUsersMap() {
return usersMap;
}
public void setUsersMap(Map<Integer, User> usersMap) {
this.usersMap = usersMap;
}
}