a. 根据实体类生成对应的表,在父工程中配置相应的pom文件
b. applicationContext.xml中做好相应的配置,注解开发不要忘记包扫描,事物的注解管理
c. 前台页面通过Jquery Easyui编写好相应的代码
d. 后台功能的实现(增加功能,分页查询功能,收派标准修改功能)
e. 选中一行进行修改前台代码的实现
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import cn.bos.domain.base.Standard;
import cn.bos.service.base.StandardService;
@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class StandardAction extends ActionSupport
implements ModelDriven {
// 模型驱动
private Standard standard = new Standard();
@Override
public Standard getModel() {
return standard;
}
// 注入service对象
@Autowired
private StandardService standardService;
@Action(value = "standard_save", results = {
@Result(name = "success", type = "redirect", location = "./pages/base/standard.html") })
public String save() {
System.out.println("添加收派标准");
standardService.save(standard);
return SUCCESS;
}
// 属性驱动
private int page;
private int rows;
public void setPage(int page) {
this.page = page;
}
public void setRows(int rows) {
this.rows = rows;
}
// 分页列表查询
@Action(value = "standard_pageQuery", results = {
@Result(name = "success", type = "json") })
public String pageQuery() {
// 调用业务层,查询数据结果
Pageable pageable = new PageRequest(page - 1, rows);
Page pageData = standardService.findPageData(pageable);
// 返回客户端数据需要total和rows
Map result = new HashMap<>();
result.put("total", pageData.getNumberOfElements());
result.put("rows", pageData.getContent());
// 将map转换为json数据返回,使用struts2-json-plugin插件
ActionContext.getContext().getValueStack().push(result);
return SUCCESS;
}
//查询所有收派标准方法
@Action(value="standard_findAll",
results= {@Result(name="success",type="json")})
public String findAll() {
List standards = standardService.findAll();
ActionContext.getContext().getValueStack().push(standards);;
return SUCCESS;
}
}
id : 'button-edit',
text : '修改',
iconCls : 'icon-edit',
handler : function() {
// 获取当前datagrid选中数据
var rows = $("#grid").datagrid('getSelections');
if (rows.length != 1) {
// 没选 或 多选
$.messager.alert("提示信息", "修改数据时,只能选中一行", "warning");
} else {
// 只选中一行
var row = rows[0];
// 进行表单回显操作
$("#standardForm").form('load', row);
// 显示窗口
$("#standardWindow").window('open');
}
}
}
总结:
1.注解开发注入属性的时候不要忘记写@Autowired
2.Action处理请求时,转发回页面会显示空白,设置Type=”redirect”重定向回页面解决问题
3.datagrid分页时,pageRequest的page页码从0开始Pageable pageable = new PageRequest(page - 1, rows);
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import cn.bos.domain.base.Courier;
import cn.bos.domain.base.Standard;
import cn.bos.service.base.CourierService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class CourierAction extends ActionSupport implements
ModelDriven {
// 模型驱动
private Courier courier = new Courier();
@Override
public Courier getModel() {
return courier;
}
// 注入Service
@Autowired
private CourierService courierService;
// 添加快递员方法
@Action(value = "courier_save", results = { @Result(name = "success", location = "./pages/base/courier.html", type = "redirect") })
public String save() {
courierService.save(courier);
return SUCCESS;
}
// 属性驱动接收客户端分页参数
private int page;
private int rows;
public void setPage(int page) {
this.page = page;
}
public void setRows(int rows) {
this.rows = rows;
}
// 分页查询方法
@Action(value = "courier_pageQuery", results = { @Result(name = "success", type = "json") })
public String pageQuery() {
// 封装Pageable对象
Pageable pageable = new PageRequest(page - 1, rows);
// 封装条件查询对象 Specification
Specification specification = new Specification() {
@Override
// Root 用于获取属性字段,CriteriaQuery可以用于简单条件查询,CriteriaBuilder 用于构造复杂条件查询
public Predicate toPredicate(Root root,
CriteriaQuery> query, CriteriaBuilder cb) {
List list = new ArrayList();
// 简单单表查询
if (StringUtils.isNotBlank(courier.getCourierNum())) {
Predicate p1 = cb.equal(
root.get("courierNum").as(String.class),
courier.getCourierNum());
list.add(p1);
}
if (StringUtils.isNotBlank(courier.getCompany())) {
Predicate p2 = cb.like(
root.get("company").as(String.class),
"%" + courier.getCompany() + "%");
list.add(p2);
}
if (StringUtils.isNotBlank(courier.getType())) {
Predicate p3 = cb.equal(root.get("type").as(String.class),
courier.getType());
list.add(p3);
}
// 多表查询
Join standardJoin = root.join("standard",
JoinType.INNER);
if (courier.getStandard() != null
&& StringUtils.isNotBlank(courier.getStandard()
.getName())) {
Predicate p4 = cb.like(
standardJoin.get("name").as(String.class), "%"
+ courier.getStandard().getName() + "%");
list.add(p4);
}
return cb.and(list.toArray(new Predicate[0]));
}
};
// 调用业务层 ,返回 Page
Page pageData = courierService.findPageData(specification,
pageable);
// 将返回page对象 转换datagrid需要格式
Map result = new HashMap();
result.put("total", pageData.getTotalElements());
result.put("rows", pageData.getContent());
// 将结果对象 压入值栈顶部
ActionContext.getContext().getValueStack().push(result);
return SUCCESS;
}
// 属性驱动
private String ids;
public void setIds(String ids) {
this.ids = ids;
}
// 作废快递员
@Action(value = "courier_delBatch", results = { @Result(name = "success", location = "./pages/base/courier.html", type = "redirect") })
public String delBatch() {
// 按,分隔ids
String[] idArray = ids.split(",");
// 调用业务层,批量作废
courierService.delBatch(idArray);
return SUCCESS;
}
}