📄 countryaction.java
字号:
/**
* Copyright (c) http://www.hao-se.cn Ltd.,2007 All rights reserved.
*/
package cn.haose.action;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.json.me.JSONArray;
import org.json.me.JSONObject;
import cn.haose.model.Country;
import cn.haose.service.CountryService;
import cn.haose.util.JsonMVCUtil;
/**
* 国别的控制类
*
* @author hao-se.cn(好·色)
*/
public class CountryAction extends DispatchAction {
/** 国别Service */
private CountryService countryService;
public void setCountryService(CountryService countryService) {
this.countryService = countryService;
}
/**
* 默认首页动作
*
* @param mapping
* @param form
* @param request
* @param response
* @return
*/
public ActionForward index(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
return mapping.findForward("index");
}
/**
* 列表或删除动作
*
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward listOrDelete(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// 删除动作标识
String del = request.getParameter("del");
if (del != null && del.equals("true")) {
return deleteData(mapping, form, request, response);
} else {
return listData(mapping, form, request, response);
}
}
/**
* 分页显示列表数据动作
*
* @param actionMapping
* @param actionForm
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward listData(ActionMapping actionMapping,
ActionForm actionForm, HttpServletRequest request,
HttpServletResponse response) throws Exception {
// 过滤的字段内容
String filterValue = request.getParameter("filterValue");
String fieldName = request.getParameter("fieldName");
// 分页页数
String start = request.getParameter("start");
// 一页的数据条数
String limit = request.getParameter("limit");
// 取得当前条件下的总记录数
int totalCount = countryService.findTotalCount(fieldName, filterValue);
// 取得分页数据
List<Country> results = countryService.pagingQuery(fieldName,
filterValue, Integer.parseInt(start), Integer.parseInt(limit));
JSONObject jsonData = toJSONObject(totalCount, results);
// push data back to Ajax page
JsonMVCUtil.jsonResponse(jsonData, request, response);
return null;
}
/**
* 删除数据动作
*
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward deleteData(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String delData = request.getParameter("delData");
// 删除动作
if (delData != null) {
String[] ids = toString(delData);
for (String id : ids) {
countryService.deleteCountry(countryService.getCountryById(id));
}
}
// 分页显示
listData(mapping, form, request, response);
return null;
}
/**
* 加载一条数据(用于更新)
*
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward loadData(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// get id first
String countryId = (String) request.getParameter("id").trim();
Country country = countryService.getCountryById(countryId);
// push data back to Ajax page
JsonMVCUtil.jsonResponse(country.toJSONObject(), request, response);
return null;
}
/**
* 创建新数据
*
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward createData(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
Country country = new Country();
country.setCountryName(request.getParameter("countryName"));
country.setCountryCode(request.getParameter("countryCode"));
country.setCreateUser("管理员用户"); // 需要修改
JSONObject errors = new JSONObject();
if (!countryService.findBy("countryName", country.getCountryName())
.isEmpty()) {
errors.put("failure", true);
errors.put("errorInfo", "国别名称已经存在,请选择另外一个!");
} else if (!countryService.findBy("countryCode",
country.getCountryCode()).isEmpty()) {
errors.put("failure", true);
errors.put("errorInfo", "国别代码已经存在,请选择另外一个!");
}
if (errors.length() > 0) {
// set response now with json format
JsonMVCUtil.jsonErrorsResponse(errors, request, response);
} else {
try {
countryService.saveCountry(country);
JsonMVCUtil.jsonOkResponse("成功创建国别数据!", request, response);
} catch (Exception e) {
JsonMVCUtil.jsonFailResponse("创建国别数据失败, 请检查后再试", request, response);
}
}
return null;
}
/**
* 更新数据
*
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward updateData(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
try {
// get id first
String countryId = (String) request.getParameter("id").trim();
Country country = countryService.getCountryById(countryId);
country.setCountryName(request.getParameter("countryName"));
country.setCountryCode(request.getParameter("countryCode"));
country.setValidFlag(request.getParameter("validFlag"));
country.setUpdateUser("管理员更新用户"); // 需要修改
country.setUpdateDate(new Date());
countryService.updateCountry(country);
JsonMVCUtil.jsonOkResponse("成功更新国别数据!", request, response);
} catch (Exception e) {
JsonMVCUtil.jsonFailResponse("更新国别数据失败, 请检查后再试", request, response);
}
return null;
}
/**
* 构建json数据
*
* @param totalCount 总记录数
* @param results 分页数据
* @return
* @throws Exception
*/
private JSONObject toJSONObject(int totalCount, List<Country> results)
throws Exception {
JSONObject json = new JSONObject();
json.put("totalCount", totalCount);
JSONArray jsonItems = new JSONArray();
for (Iterator<Country> iter = results.iterator(); iter.hasNext();) {
jsonItems.put(iter.next().toJSONObject());
}
json.put("results", jsonItems);
return json;
}
/**
* 解析来至客户端的json数据
*
* @param jsonString
* @return
* @throws Exception
*/
private String[] toString(String jsonString) throws Exception {
String[] ids;
JSONArray jsonArray = new JSONArray(jsonString);
ids = new String[jsonArray.length()];
// loop through - get from json and update
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String id = jsonObject.getString("id");
ids[i] = id;
}
return ids;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -