项目一:第五天 1、区域数据(pinyin4j-简码,城市编码) 2、Web层代码重构(model对象,分页代码提取) 3、区域分页查询 3、分区添加功能 4、定区管理管理-添加,分页

 

Service:

/**

  * @Description: 1、保存定区  2、让分区关联定区

  * 对象三种状态 1、持久态(被session管理对象-一级缓存中有对象) 2、托管态(有OID标识,数据库中有记录)  3、瞬时态 new对象

      

            对象关联:持久态可以关联持久态 / 持久态关联托管态

*/

public void save(FixedArea model, String[] subAreaId) {

//问题:保存完成参数定区对象是瞬时态

//解决:使用save方法返回对象(返回结果是持久态)

//结论:1、当保存实体主键类型如果是java基本类型,save方法参数对象保存完后就是持久态

//    2、当保存实体主键类型如果不是java基本类型,save方法返回值才是持久态

model = fixedAreaDao.save(model);

//方式一:执行sql语句完成关联-update t_sub_area s set s.c_fixedarea_id = ? where s.c_id = ?

//方式二:通过对象关联

//假设model对象是持久态

if(subAreaId!=null && subAreaId.length>0){

for (String sId : subAreaId) {

//查询到分区对象是持久态

SubArea subArea = subAreaDao.findOne(sId);

//查询实体配置:由分区维护关系

subArea.setFixedArea(model);  //更新分区记录中定区外键

}

}

}

 

 

 

1、 区域数据(pinyin4j-简码,城市编码

2、 Web层代码重构(model对象,分页代码提取)

3、 区域分页查询

3、分区添加功能

4、定区管理管理-添加,分页

BaseAction抽取

1.1 getModel模型驱动对象提取

1、 创建BaseAction使用泛型,继承ActionSupport 实现ModelDriven接口

 

2、 让其他的action继承BaseAction,

 

3、 子类对象action对象创建,BaseAction无参构造执行

 

4、 在无参构造中获取实际类型参数

 

protected T model;

 

public T getModel() {

return model;

}

 

 

/**

     子类action对象创建,父类BaseAction无参构造执行---目的获取实际类型(class)参数

  1T:代表任意类型。一般写大写字母T (大写字母即可)

  2BaseAction<Standard>:参数化类型(class)

  3<>中:实际类型参数

 */

public BaseAction() {

try {

//第一步:获取当前运行class(子类class

Class clzz = this.getClass();  //  cn.itcast.bos.web.action.base.StandardAction

System.out.println(clzz);

 

//第二步:获取父类参数化class(BaseActioncalss)

/*Type getGenericSuperclass()

返回表示此 Class 所表示的实体(类、接口、基本类型或 void)的直接超类的 Type*/

Type type = clzz.getGenericSuperclass();  //cn.itcast.bos.web.action.common.BaseAction<cn.itcast.bos.domain.base.Standard>

System.out.println(type);

 

 

//type是顶级接口--将顶级接口转为子接口

ParameterizedType pt = (ParameterizedType) type;

 

//第三步:获取实际类型(class)参数

 /*Type[] getActualTypeArguments()

 返回表示此类型实际类型参数的 Type 对象的数组。 */

Type[] types = pt.getActualTypeArguments(); // [cn.itcast.bos.domain.base.Standard]

//Standardtype转为standardClass

Class clzzzzzzzzzzzzzzz = (Class) types[0];  //cn.itcast.bos.domain.base.Standard

System.out.println(clzzzzzzzzzzzzzzz);

 

//第四步:将实际class实例化

model = (T) clzzzzzzzzzzzzzzz.newInstance();

} catch (Exception e) {

e.printStackTrace();

}

}

1.2 分页代码提取

1、 page,rowsBaseAction中接收

2、 将转Json代码定义到父类中

 

//通过属性驱动接收datagrid提交的两个参数  page ,rows

protected int page;

protected int rows;

public void setPage(int page) {

this.page = page;

}

 

public void setRows(int rows) {

this.rows = rows;

}

 

/**

  * @Description: page结果转为分页查询json

  * java对象转为json字符串

  * @param page :分页查询结果

  * @param excludes :json排除属性

 */

public void java2json(Page<T> page, String[] excludes){

try {

Map<String, Object> map = new HashMap<>();

map.put("total", page.getTotalElements());

map.put("rows", page.getContent());

//fiexeares属性排除掉

JsonConfig  jsonConfig = new JsonConfig();

jsonConfig.setExcludes(excludes);

 

String json = JSONObject.fromObject(map, jsonConfig).toString();

System.out.println(json);

//通过response对象向浏览器输出

HttpServletResponse response = ServletActionContext.getResponse();

response.setContentType("text/json;charset=utf-8");

response.getWriter().write(json);

} catch (Exception e) {

e.printStackTrace();

}

}

分区添加

1.1 使用combobox展示区域数据

1、 pages/baes/sub_area.jsp

 

2、 在添加分区窗口中,修改选择区域 comboboxurl

1、 问题:combobox通过text获取显示文本

2、 解决 :在区域实体中添加属性get方法

 

1.1 服务端

创建分区三层对象:略

 

在分区的action中添加保存方法:

 因为分区实体中的主键是String类型 不能自增 所以需要手动给主键赋值

 

分区分页查询

由于在页面中展示分区所属的区域信息,区域对象不能排除。解决方案:将区域下分区集合subareas属性排除掉!!

定区的添加

第一步:在定区添加表单中展示未关联到定区分区记录

第二步:提交表单(1、定区相关属性  2、多个分区ID

第三步:在服务端提供保存方法(1、保存定区  2、让分区关联定区)

定区由物理地址上相近的分区构成。定区是物流公司进行物流人员分配基本单位。

1、 页面:pages/base/fixed_area.jsp

2、 调整页面:增加选择分区功能

 

<tr height="300">

<td valign="top">关联分区</td>

<td>

<table id="subareaGrid"  class="easyui-datagrid" border="false" style="300px;height:300px"

data-options="url:'${pageContext.request.contextPath}/subAreaAction_listajax.action',fitColumns:true,singleSelect:false">

<thead>  

        <tr>  

            <th data-options="field:'id',30,checkbox:true">编号</th>  

            <th data-options="field:'keyWords',150">关键字</th>  

            <th data-options="field:'assistKeyWords',200,align:'right'">辅助关键字</th>  

        </tr>  

    </thead> 

</table>

</td>

</tr>

 

1.1 通过datagrid展示分区数据

1、 发出请求获取未关联到定区分区数据

1、 在分区action中添加方法:返回分区数据(不需要分页),josn的数组

 

action:

1、 解决:将datagrid ,展示形式为checkboxfield改为:subareaId

1、 问题2:提交subaareaId的值为null

1、 解决:在分区的实体中添加subareaId属性的get方法即可

1.1 服务端保存定区

定区-分区:一对多;在分区表中有外键定区ID

 

保存定区:保存一条定区数据,分区关联定区

 

Action:

原文地址:https://www.cnblogs.com/shan1393/p/9196852.html