package com.erp.action;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.erp.bean.Person;
import com.erp.dao.PersonDao;
import com.erp.utils.PageModel;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("unchecked")
@Controller("personAction")
@Scope("prototype")
public class PersonAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private Person person;
PageModel pageModel = new PageModel();
public PageModel getPageModel() {
return pageModel;
}
private int total;
private String ajaxResult;
public String getAjaxResult() {
return ajaxResult;
}
public void setAjaxResult(String ajaxResult) {
this.ajaxResult = ajaxResult;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
@Resource
private PersonDao personDao;
private List list;
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public String execute() {
return "success";
}
public String save() {
try {
if (personDao.save(person)) {
ajaxResult = "success";
return "success";
} else {
return "error";
}
} catch (Exception e) {
this.addActionError(e.getMessage());
ajaxResult = "创建新联系人失败!" + e.getMessage();
return "error";
}
}
public String list() {
ActionContext act = ActionContext.getContext();
Map maps = ActionContext.getContext().getParameters();
String[] pages = (String[]) maps.get("page");
String page = pages[0];// 取得当 前页数
pageModel.setPage(Integer.parseInt(page));
String[] rows = (String[]) maps.get("rows"); // 取得每页显示行数
String row = rows[0];
pageModel.setRows(Integer.parseInt(row));
pageModel.setRecord(this.getResultSize());
pageModel.setTotal();
act.getSession().put("pageModel", pageModel);
total = pageModel.getTotal();
list = personDao.getPersons(pageModel.getPage(), pageModel.getRows());
return "list";
}
public String del() {
try {
Map maps = ActionContext.getContext().getParameters();
String[] userids = (String[]) maps.get("person.userid");
if (personDao.del(Integer.parseInt(userids[0]))) {
ajaxResult = "success";
return "list";
} else {
return "error";
}
} catch (NumberFormatException e) {
e.printStackTrace();
ajaxResult = "删除联系人失败!" + e.getMessage();
return "error";
}
}
public String find() {
try {
Map maps = ActionContext.getContext().getParameters();
String[] userids = (String[]) maps.get("person.userid");
person = personDao.getPerson(Integer.parseInt(userids[0]));
if (person != null) {
ajaxResult = "success";
} else {
ajaxResult = "没有记录!";
return "error";
}
} catch (Exception e) {
e.printStackTrace();
ajaxResult = "加载联系人失败!" + e.getMessage();
return "error";
}
return "success";
}
public String findByQueryString() {
Map maps = ActionContext.getContext().getParameters();
String userid = ((String[]) maps.get("person.userid"))[0];
String username = ((String[]) maps.get("person.username"))[0];
String queryString = "FROM Person AS p";
if (!"".equals(userid) && !"".equals(username)) {
queryString += " WHERE p.userid=" + userid
+ " AND p.username LIKE '%" + username + "%'";
} else {
if ("".equals(userid)) {
queryString += " WHERE p.username LIKE '%" + username + "%'";
}
if ("".equals(username)) {
queryString += " WHERE p.userid=" + userid;
}
}
person = personDao.findPerson(queryString);
if (person != null) {
ajaxResult = "success";
} else {
ajaxResult = "error!";
}
return "success";
}
public String update() {
String[] userids;
try {
Map maps = ActionContext.getContext().getParameters();
userids = (String[]) maps.get("person.userid");
String[] username = (String[]) maps.get("person.username");
String[] password = (String[]) maps.get("person.password");
person = personDao.getPerson(Integer.parseInt(userids[0]));
person.setUsername(username[0]);
person.setPassword(password[0]);
if (personDao.update(person)) {
person = personDao.getPerson(Integer.parseInt(userids[0]));
ajaxResult = "success";
} else {
return "error";
}
} catch (Exception e) {
e.printStackTrace();
ajaxResult = "加载联系人失败!" + e.getMessage();
return "error";
}
return "success";
}
public int getResultSize() {
return personDao.count();
}
}
评论15