on, JsonMappingException, IOException{
Json j = new Json();
UserDTO u = new UserDTO();
BeanUtils.copyProperties(this, u);
u.setCreateTime(getCurrentTime());
u.setUpdateTime(getCurrentTime());
boolean addStatu = userService.addUser(u);
if(addStatu){
j.setMsg("Add User [" + u.getUserName() + "] Success !");
j.setSuccess(addStatu);
}else{
j.setMsg("Add User [" + u.getUserName() + "] fail !");
j.setSuccess(addStatu);
}
pwObj(getJson(j));
return null;
}
/**
* 更新用户信息
*/
public String updateUser() throws JsonGenerationException, JsonMappingException, IOException{
Json j = new Json();
UserDTO u = new UserDTO();
BeanUtils.copyProperties(this, u);
u.setCreateTime(createTime);
u.setUpdateTime(updateTime);
boolean addStatu = userService.updateUser(u);
if(addStatu){
j.setMsg("Update User [" + u.getUserName() + "] Success !");
j.setSuccess(addStatu);
}else{
j.setMsg("Update User [" + u.getUserName() + "] fail !");
j.setSuccess(addStatu);
}
pwObj(getJson(j));
return null;
}
/**
* 删除用户信息
*/
public String delUsers() throws JsonGenerationException, JsonMappingException, IOException{
Json j = new Json();
try {
j.setMsg("Delete users success !");
j.setSuccess(true);
userService.delUsers(ids);
} catch (Exception e) {
j.setMsg("Delete users fail !");
e.printStackTrace();
}
pwObj(getJson(j));
return null;
}
/**
* 初始化datagrid
*/
public String getUserJson() throws IOException{
List
userList = userService.getUsers(page, rows);
int total = userService.getCountUsers();
Map
m = new HashMap
(); m.put("total", total); m.put("rows", userList); pwObj(getJson(m)); return null; } private String getCurrentTime(){ SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); return sDateFormat.format(new java.util.Date()); } private String getJson(Object o) throws IOException, JsonGenerationException, JsonMappingException { ObjectMapper om = new ObjectMapper(); StringWriter sw = new StringWriter(); JsonGenerator jg = new JsonFactory().createJsonGenerator(sw); om.writeva lue(jg, o); jg.close(); return sw.toString(); } private void pwObj(Object obj) throws IOException { PrintWriter pw = ServletActionContext.getResponse().getWriter(); pw.print(obj); pw.flush(); pw.close(); }
b) 使用到的Json工具类:
package com.chy.ssh.utils;
public class Json {
private boolean success = false; //操作是否成功
private String msg; //提示信息
private Object o; //其他信息
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getO() {
return o;
}
public void setO(Object o) {
this.o = o;
}
}
c) UserDTO:
package com.chy.ssh.business.bean;
import java.io.Serializable;
@SuppressWarnings("serial")
public class UserDTO implements Serializable{
private int id;
private String userName;
private String passWord;
private String createTime;
private String updateTime;
public UserDTO() {
super();
}
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
public St