1、jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>管理分区</title> <!-- 导入jquery核心类库 --> <script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.8.3.js"></script> <!-- 导入easyui类库 --> <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/js/easyui/themes/default/easyui.css"> <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/js/easyui/themes/icon.css"> <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/js/easyui/ext/portal.css"> <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/css/default.css"> <script type="text/javascript" src="${pageContext.request.contextPath }/js/easyui/jquery.easyui.min.js"></script> <script type="text/javascript" src="${pageContext.request.contextPath }/js/easyui/ext/jquery.portal.js"></script> <script type="text/javascript" src="${pageContext.request.contextPath }/js/easyui/ext/jquery.cookie.js"></script> <script src="${pageContext.request.contextPath }/js/easyui/locale/easyui-lang-zh_CN.js" type="text/javascript"></script> <script type="text/javascript"> function doAdd() { $('#addSubareaWindow').window("open"); } function doEdit() { alert("修改..."); } function doDelete() { alert("删除..."); } function doSearch() { $('#searchWindow').window("open"); } function doExport() { alert("导出"); } function doImport() { alert("导入"); } //工具栏 var toolbar = [{ id: 'button-search', text: '查询', iconCls: 'icon-search', handler: doSearch }, { id: 'button-add', text: '增加', iconCls: 'icon-add', handler: doAdd }, { id: 'button-edit', text: '修改', iconCls: 'icon-edit', handler: doEdit }, { id: 'button-delete', text: '删除', iconCls: 'icon-cancel', handler: doDelete }, { id: 'button-import', text: '导入', iconCls: 'icon-redo', handler: doImport }, { id: 'button-export', text: '导出', iconCls: 'icon-undo', handler: doExport }]; // 定义列 var columns = [[{ field: 'id', checkbox: true, }, { field: 'showid', title: '分拣编号', width: 120, align: 'center', formatter: function (data, row, index) { return row.id; } }, { field: 'province', title: '省', width: 120, align: 'center', formatter: function (data, row, index) { return row.region.province; } }, { field: 'city', title: '市', width: 120, align: 'center', formatter: function (data, row, index) { return row.region.city; } }, { field: 'district', title: '区', width: 120, align: 'center', formatter: function (data, row, index) { return row.region.district; } }, { field: 'addresskey', title: '关键字', width: 120, align: 'center' }, { field: 'startnum', title: '起始号', width: 100, align: 'center' }, { field: 'endnum', title: '终止号', width: 100, align: 'center' }, { field: 'single', title: '单双号', width: 100, align: 'center' }, { field: 'position', title: '位置', width: 200, align: 'center' }]]; $(function () { // 先将body隐藏,再显示,不会出现页面刷新效果 $("body").css({visibility: "visible"}); // 收派标准数据表格 $('#grid').datagrid({ iconCls: 'icon-forward', fit: true, border: true, rownumbers: true, striped: true, pageSize:2, pageList: [2, 4, 6], pagination: true, toolbar: toolbar, url: "${pageContext.request.contextPath}/SubAreaAction_selectregion.action", //显示分页数据 idField: 'id', columns: columns, onDblClickRow: doDblClickRow }); // 添加、修改分区 $('#addSubareaWindow').window({ title: '添加修改分区', width: 600, modal: true, shadow: true, closed: true, height: 400, resizable: false }); // 查询分区 $('#searchWindow').window({ title: '查询分区', width: 400, modal: true, shadow: true, closed: true, height: 400, resizable: false }); $("#btn").click(function () { alert("执行查询..."); }); $("#save").click(function () { var v = $('#addsubareaFrom').form("validate"); if (v) { $('#addsubareaFrom').submit(); } else { $.messager.alert("提示", "表单数据不正确", "error"); } }); }); function doDblClickRow() { alert("双击表格数据..."); } </script> </head> <body class="easyui-layout" style="visibility:hidden;"> <div region="center" border="false"> <table id="grid"></table> </div> <!-- 添加 修改分区 --> <div class="easyui-window" title="分区添加修改" id="addSubareaWindow" collapsible="false" minimizable="false" maximizable="false" style="top:20px;left:200px"> <div style="height:31px;overflow:hidden;" split="false" border="false"> <div class="datagrid-toolbar"> <a id="save" icon="icon-save" href="#" class="easyui-linkbutton" plain="true">保存</a> </div> </div> <div style="overflow:auto;padding:5px;" border="false"> <form id="addsubareaFrom" action="${pageContext.request.contextPath}/SubAreaAction_save.action"> <table class="table-edit" width="80%" align="center"> <tr class="title"> <td colspan="2">分区信息</td> </tr> <tr> <td>分拣编码</td> <td><input type="text" name="id" class="easyui-validatebox" required="true"/></td> </tr> <tr> <td>选择区域</td> <td> <input class="easyui-combobox" name="region.id" data-options="valueField:'id',textField:'name',url:'${pageContext.request.contextPath}/region_selectregion.action'"/> </td> </tr> <tr> <td>关键字</td> <td><input type="text" name="addresskey" class="easyui-validatebox" required="true"/></td> </tr> <tr> <td>起始号</td> <td><input type="text" name="startnum" class="easyui-validatebox" required="true"/></td> </tr> <tr> <td>终止号</td> <td><input type="text" name="endnum" class="easyui-validatebox" required="true"/></td> </tr> <tr> <td>单双号</td> <td> <select class="easyui-combobox" name="single" style="width:150px;"> <option value="0">单双号</option> <option value="1">单号</option> <option value="2">双号</option> </select> </td> </tr> <tr> <td>位置信息</td> <td><input type="text" name="position" class="easyui-validatebox" required="true" style="width:250px;"/></td> </tr> </table> </form> </div> </div> <!-- 查询分区 --> <div class="easyui-window" title="查询分区窗口" id="searchWindow" collapsible="false" minimizable="false" maximizable="false" style="top:20px;left:200px"> <div style="overflow:auto;padding:5px;" border="false"> <form> <table class="table-edit" width="80%" align="center"> <tr class="title"> <td colspan="2">查询条件</td> </tr> <tr> <td>省</td> <td><input type="text" name="region.province"/></td> </tr> <tr> <td>市</td> <td><input type="text" name="region.city"/></td> </tr> <tr> <td>区(县)</td> <td><input type="text" name="region.district"/></td> </tr> <tr> <td>关键字</td> <td><input type="text" name="addresskey"/></td> </tr> <tr> <td colspan="2"><a id="btn" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'">查询</a> </td> </tr> </table> </form> </div> </div> </body> </html>
2、json数据样式
{
"rows": [{
"addresskey": "朝阳",
"endnum": "11",
"id": "A01",
"position": "北京市北京市丰台区",
"region": {
"city": "北京市",
"citycode": "beijing",
"district": "丰台区",
"id": "QY004",
"name": "北京市北京市丰台区",
"postcode": "110106",
"province": "北京市",
"shortcode": "BJFT"
},
"single": "0",
"startnum": "1"
}],
"total": 2
}
3、region 中的关系映射
package com.dong.bos.model;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
public class Region {
private String id;
private String province;
private String city;
private String district;
private String postcode;
private String shortcode;
private String citycode;
private Set<Subarea> subareaSet=new HashSet<Subarea>();
public Set<Subarea> getSubareaSet() {
return subareaSet;
}
public void setSubareaSet(Set<Subarea> subareaSet) {
this.subareaSet = subareaSet;
}
public String getName() {
return province+city+district;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getDistrict() {
return district;
}
public void setDistrict(String district) {
this.district = district;
}
public String getPostcode() {
return postcode;
}
public void setPostcode(String postcode) {
this.postcode = postcode;
}
public String getShortcode() {
return shortcode;
}
public void setShortcode(String shortcode) {
this.shortcode = shortcode;
}
public String getCitycode() {
return citycode;
}
public void setCitycode(String citycode) {
this.citycode = citycode;
}
public Region() {
}
public Region(String id, String province, String city, String district, String postcode, String shortcode, String citycode) {
this.id = id;
this.province = province;
this.city = city;
this.district = district;
this.postcode = postcode;
this.shortcode = shortcode;
this.citycode = citycode;
}
@Override
public String toString() {
return "Region{" +
"id='" + id + '\'' +
", province='" + province + '\'' +
", city='" + city + '\'' +
", district='" + district + '\'' +
", postcode='" + postcode + '\'' +
", shortcode='" + shortcode + '\'' +
", citycode='" + citycode + '\'' +
'}';
}
}
Region.hbm.xml:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.dong.bos.model.Region" table="bc_region" schema="ssh">
<id name="id" column="id"/>
<property name="province" column="province"/>
<property name="city" column="city"/>
<property name="district" column="district"/>
<property name="postcode" column="postcode"/>
<property name="shortcode" column="shortcode"/>
<property name="citycode" column="citycode"/>
//一对多
<set name="subareaSet">
<key column="region_id"></key>
<one-to-many class="com.dong.bos.model.Subarea"></one-to-many>
</set>
</class>
</hibernate-mapping>
4、Subarea中的关系映射
package com.dong.bos.model;
import java.util.Objects;
public class Subarea {
private String id;
private String addresskey;
private String startnum;
private String endnum;
private String single;
private String position;
private Region region;
public Region getRegion() {
return region;
}
public void setRegion(Region region) {
this.region = region;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getAddresskey() {
return addresskey;
}
public void setAddresskey(String addresskey) {
this.addresskey = addresskey;
}
public String getStartnum() {
return startnum;
}
public void setStartnum(String startnum) {
this.startnum = startnum;
}
public String getEndnum() {
return endnum;
}
public void setEndnum(String endnum) {
this.endnum = endnum;
}
public String getSingle() {
return single;
}
public void setSingle(String single) {
this.single = single;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
@Override
public String toString() {
return "Subarea{" +
"id='" + id + '\'' +
", addresskey='" + addresskey + '\'' +
", startnum='" + startnum + '\'' +
", endnum='" + endnum + '\'' +
", single='" + single + '\'' +
", position='" + position + '\'' +
'}';
}
}
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.dong.bos.model.Subarea" table="bc_subarea" schema="ssh"> <id name="id" column="id"/> <property name="addresskey" column="addresskey"/> <property name="startnum" column="startnum"/> <property name="endnum" column="endnum"/> <property name="single" column="single"/> <property name="position" column="position"/> <many-to-one name="region" class="com.dong.bos.model.Region" column="region_id" lazy="false"> </many-to-one> </class> </hibernate-mapping>
5、action
package com.dong.bos.web.action; import com.dong.bos.model.Region; import com.dong.bos.model.Subarea; import com.dong.bos.service.ISubAreaService; import com.dong.bos.web.action.base.BaseAction; import org.springframework.beans.factory.annotation.Autowired; import java.io.IOException; import java.util.List; public class SubAreaAction extends BaseAction<Subarea> { @Autowired private ISubAreaService iSubAreaService; @Override public String save() { System.out.println(getModel()); iSubAreaService.save(getModel()); return SUCCESS; } //提供分页数据支持 public void selectregion() throws IOException { PageBeanSet(); iSubAreaService.pageQuery(pb); pagequery(new String[]{"currentPage", "pageSize", "detachedCriteria", "subareaSet"}); } @Override public String update() { return null; } @Override public String delete() throws IOException { return null; } @Override public String list() { return null; } }
6、基类中的方法:
protected int page; protected int rows; public void setPage(int page) { this.page = page; } public void setRows(int rows) { this.rows = rows; }
protected void PageBeanSet() { pb.setPageSize(rows); pb.setCurrentPage(page); }
protected void pagequery(String[] strs) throws IOException { JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setExcludes(strs); JSONObject jsonObject = JSONObject.fromObject(pb, jsonConfig); HttpServletResponse httpServletResponse = ServletActionContext.getResponse(); httpServletResponse.setHeader("content-type", "text/json;charset=utf-8"); httpServletResponse.getWriter().write(jsonObject.toString()); }