vm.ant.web.action.vm Maven / Gradle / Ivy
package $!packageName;
#set($startsWord = $!tablePdmXml.className.substring(0,1).toLowerCase())
#set($otherWords = $!tablePdmXml.className.substring(1))
#set($hasDictionary = false)
#foreach($columnPdmXml in $tablePdmXml.columnPdmXmlList)
#if($columnPdmXml.isPK)
#set($pkColumnObject = $columnObject)
#end
#if($columnPdmXml.dictionary)
#set($hasDictionary = true)
#end
#end
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
#if($hasDictionary)
import java.util.List;
#end
import java.sql.Timestamp;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.dev.Constants;
import com.dev.common.QueryObject;
import com.dev.common.QueryParam;
import $!{viewPdmXml.packageCode}.model.$!tablePdmXml.className;
import com.dev.json.JsonUtil;
import $!{viewPdmXml.packageCode}.service.$!{tablePdmXml.className}Service;
import $!{viewPdmXml.packageCode}.web.form.$!{tablePdmXml.className}Form;
import com.dev.util.ParseData;
import com.dev.web.action.BaseAction;
#if($!{tablePdmXml.className} != "SysUser")
import com.dev.sys.model.SysUser;
#end
#if($hasDictionary && $!{tablePdmXml.className} != "SysCode")
import com.dev.sys.service.SysCodeService;
#end
/**
* Title:
*
* Description:
*
* Copyright: Copyright (c) $!nowYear
*
* Company:
*
* @author $!userName
* @version 1.0
*
* ************************** struts-config ****************************
*
* @struts.action name="$startsWord${otherWords}Form" path="/$startsWord${otherWords}/$startsWord${otherWords}Action" scope="request"
*
* @struts.action-forward name="LIST" path="/WEB-INF/pages/$packageFolder/$startsWord${otherWords}/$startsWord${otherWords}.jsp"
*
*/
public class $!{tablePdmXml.className}Action extends BaseAction {
public ActionForward doExecute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
String action = request.getParameter("action");
if (log.isDebugEnabled()) {
log.debug("action:" + action);
}
if (action == null || LIST.equals(action)) {
#if($hasDictionary)
setDataAttribute(request);
#end
return mapping.findForward(LIST);
}
if (SAVE.equalsIgnoreCase(action)) {
save$!{tablePdmXml.className}(form, request, response);
} else if (EDIT.equalsIgnoreCase(action)) {
edit$!{tablePdmXml.className}(form, request, response);
} else if (UPDATE.equalsIgnoreCase(action)) {
update$!{tablePdmXml.className}(form, request, response);
} else if (DELETE.equalsIgnoreCase(action)) {
delete$!{tablePdmXml.className}(form, request, response);
}
else if (SEARCH.equalsIgnoreCase(action)) {
search$!{tablePdmXml.className}(form, request, response);
}
return null;
}
/**
* 获取编辑页面的所需的值
* @param form
* @param request
* @param response
* @throws Exception
*/
public void edit$!{tablePdmXml.className}(ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
if (log.isDebugEnabled()) {
log.debug("Entering 'edit$!{tablePdmXml.className}' method");
}
String id = request.getParameter("id");
if (id == null || id.trim().length() == 0) {
JsonUtil.jsonFailResponse("非法进入该路径!", request, response);
} else {
$!{tablePdmXml.className}Service $startsWord${otherWords}Service = ($!{tablePdmXml.className}Service) getBean("$startsWord${otherWords}Service");
String[] ids = { request.getParameter("id") };
$!{tablePdmXml.className} $startsWord${otherWords} = ($!{tablePdmXml.className}) $startsWord${otherWords}Service.query$!{tablePdmXml.className}(parseId(ids)[0]);
Map $startsWord${otherWords}Map = ParseData.getMapFromObject($startsWord${otherWords});
JsonUtil.jsonFormalResponse($startsWord${otherWords}Map, request, response);
}
}
/**
* 新增对象
* @param form
* @param request
* @param response
* @throws Exception
*/
public void save$!{tablePdmXml.className}(ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
if (log.isDebugEnabled()) {
log.debug("Entering 'save$!{tablePdmXml.className}' method");
}
try {
$!{tablePdmXml.className}Form $startsWord${otherWords}Form = ($!{tablePdmXml.className}Form) form;
if (!validate($startsWord${otherWords}Form)) {
JsonUtil.jsonFailResponse("缺少必填项,请重新填写!", request, response);
} else {
$!{tablePdmXml.className}Service $startsWord${otherWords}Service = ($!{tablePdmXml.className}Service) getBean("$startsWord${otherWords}Service");
$!{tablePdmXml.className} $startsWord${otherWords} = new $!{tablePdmXml.className}();
convert($startsWord${otherWords}, $startsWord${otherWords}Form);
HttpSession htsession = request.getSession();
SysUser sysUser = (SysUser) htsession.getAttribute(Constants.LOGIN_USER);
$startsWord${otherWords}.setCreateUser(sysUser.getId());
$startsWord${otherWords}.setCreateTime(new Timestamp(System.currentTimeMillis()));
$startsWord${otherWords}.setModUser(sysUser.getId());
$startsWord${otherWords}.setModTime(new Timestamp(System.currentTimeMillis()));
$startsWord${otherWords}Service.create$!{tablePdmXml.className}($startsWord${otherWords});
JsonUtil.jsonOkResponse("新建操作成功!", request, response);
}
} catch (Exception e) {
e.printStackTrace();
JsonUtil.jsonFailResponse("新建操作失败!" + getErrorMsg(e, ERROR_MSG_SIZE), request, response);
}
}
/**
* 更新对象
* @param form
* @param request
* @param response
* @throws Exception
*/
public void update$!{tablePdmXml.className}(ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
if (log.isDebugEnabled()) {
log.debug("Entering 'update$!{tablePdmXml.className}' method");
}
try {
$!{tablePdmXml.className}Form $startsWord${otherWords}Form = ($!{tablePdmXml.className}Form) form;
if (!validate($startsWord${otherWords}Form)) {
JsonUtil.jsonFailResponse("缺少必填项,请重新填写!", request, response);
} else {
$!{tablePdmXml.className}Service $startsWord${otherWords}Service = ($!{tablePdmXml.className}Service) getBean("$startsWord${otherWords}Service");
$!{tablePdmXml.className} $startsWord${otherWords} = new $!{tablePdmXml.className}();
convert($startsWord${otherWords}, $startsWord${otherWords}Form);
HttpSession htsession = request.getSession();
SysUser sysUser = (SysUser) htsession.getAttribute(Constants.LOGIN_USER);
$startsWord${otherWords}.setModUser(sysUser.getId());
$startsWord${otherWords}.setModTime(new Timestamp(System.currentTimeMillis()));
$startsWord${otherWords}Service.update$!{tablePdmXml.className}($startsWord${otherWords});
JsonUtil.jsonOkResponse("更新操作成功!", request, response);
}
} catch (Exception e) {
e.printStackTrace();
JsonUtil.jsonFailResponse("更新操作失败!" + getErrorMsg(e, ERROR_MSG_SIZE), request, response);
}
}
/**
* 删除对象
* @param form
* @param request
* @param response
* @throws Exception
*/
public void delete$!{tablePdmXml.className}(ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
if (log.isDebugEnabled()) {
log.debug("Entering 'delete$!{tablePdmXml.className}' method");
}
try {
$!{tablePdmXml.className}Service $startsWord${otherWords}Service = ($!{tablePdmXml.className}Service) getBean("$startsWord${otherWords}Service");
String del_ids = request.getParameter("del_ids");
if (del_ids != null && del_ids.trim().length() > 0) {
$startsWord${otherWords}Service.remove$!{tablePdmXml.className}s(parseId(del_ids.split(",")));
}
JsonUtil.jsonOkResponse("删除操作成功!", request, response);
} catch (Exception e) {
e.printStackTrace();
JsonUtil.jsonFailResponse("删除操作失败!" + getErrorMsg(e, ERROR_MSG_SIZE), request, response);
}
}
/**
* 通用查询
* @param form
* @param request
* @param response
* @throws Exception
*/
public void search$!{tablePdmXml.className}(ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
if (log.isDebugEnabled()) {
log.debug("Entering 'search$!{tablePdmXml.className}' method");
}
$!{tablePdmXml.className}Form $startsWord${otherWords}Form = ($!{tablePdmXml.className}Form) form;
$!{tablePdmXml.className}Service $startsWord${otherWords}Service = ($!{tablePdmXml.className}Service) getBean("$startsWord${otherWords}Service");
QueryObject queryObject = new QueryObject();
convert(queryObject, $startsWord${otherWords}Form);
/** 添加自己的查询条件 * */
addParams($startsWord${otherWords}Form, queryObject);
queryObject = $startsWord${otherWords}Service.query$!{tablePdmXml.className}(queryObject);
JsonUtil.jsonResponse(queryObject, request, response);
}
/**
* 添加查询条件
* @param $startsWord${otherWords}Form
* @param queryObject
*/
private void addParams($!{tablePdmXml.className}Form $startsWord${otherWords}Form, QueryObject queryObject) {
Collection params = new ArrayList();
#foreach($columnPdmXml in $tablePdmXml.columnPdmXmlList)
#if(!$columnPdmXml.isPK && "Date" != $!columnPdmXml.dataType && "Timestamp" != $!columnPdmXml.dataType)
#set($columnStartsWord = $!columnPdmXml.code.substring(0,1).toUpperCase())
#set($columnOtherWords = $!columnPdmXml.code.substring(1))
if($startsWord${otherWords}Form.get$columnStartsWord${columnOtherWords}()!=null && $startsWord${otherWords}Form.get$columnStartsWord${columnOtherWords}().trim().length() > 0){
QueryParam qp = new QueryParam() ;
qp.setKey($!{tablePdmXml.className}.C_$!{columnPdmXml.code.toUpperCase()}) ;
#if("String" == $!columnPdmXml.dataType)
qp.setLogicOper("LIKE") ;
qp.setValue("'%" + $startsWord${otherWords}Form.get$columnStartsWord${columnOtherWords}() + "%'") ;
#else
qp.setLogicOper("=") ;
qp.setValue($startsWord${otherWords}Form.get$columnStartsWord${columnOtherWords}()) ;
#end
params.add(qp) ;
}
#end
#end
queryObject.setParameters(params);
}
/**
* 序列化$pkColumnPdmXml.code组
* @param ids
* @return
* @throws Exception
*/
private java.io.Serializable[] parseId(String[] ${pkColumnPdmXml.code}s) throws Exception {
if (${pkColumnPdmXml.code}s == null || ${pkColumnPdmXml.code}s.length == 0) {
throw new Exception("非法进入编辑页!");
}
java.io.Serializable ${pkColumnPdmXml.code}[] = null;
try {
${pkColumnPdmXml.code} = new Integer[${pkColumnPdmXml.code}s.length];
for (int i = 0; i < ${pkColumnPdmXml.code}s.length; i++) {
${pkColumnPdmXml.code}[i] = new Integer(${pkColumnPdmXml.code}s[i]);
}
} catch (Exception e) {
${pkColumnPdmXml.code} = ${pkColumnPdmXml.code}s;
}
return ${pkColumnPdmXml.code};
}
/**
* 校验提交表单
* @param form
* @return
* @throws Exception
*/
private boolean validate($!{tablePdmXml.className}Form form) throws Exception {
#foreach($columnPdmXml in $tablePdmXml.columnPdmXmlList)
#if($!columnPdmXml.mandatory && !$columnPdmXml.isPK && "createUser" != $columnPdmXml.code && "createTime" != $columnPdmXml.code)
#set($startsWord = $!columnPdmXml.code.substring(0,1).toUpperCase())
#set($otherWords = $!columnPdmXml.code.substring(1))
if(form.get$startsWord${otherWords}()==null || "".equals(form.get$startsWord${otherWords}().trim())){
return false ;
}
#end
#end
return true;
}
#if($hasDictionary)
/**
* 获取字典数据
* @param request
* @throws Exception
*/
private void setDataAttribute(HttpServletRequest request) throws Exception {
SysCodeService sysCodeService = (SysCodeService) getBean("sysCodeService");
#set($isFrist = true)
#foreach($columnPdmXml in $tablePdmXml.columnPdmXmlList)
#if($columnPdmXml.dictionary)
//region $!{columnPdmXml.name}[LIST_$!{columnPdmXml.code.toUpperCase()}]
#if($isFrist)
List