您好,欢迎访问一九零五行业门户网

JAVAEE新增客户、数据字典、文件上传和修改客户讲解

作者: kent鹏  
转载请注明出处:  
一、新增客户1.数据字典 用于枚举项目中有限个数的字典项
(1)表中数据字典与其他表的关系:
建表语句:
create table `base_dict` (   `dict_id` varchar(32) not null comment '数据字典id(主键)',   `dict_type_code` varchar(10) not null comment '数据字典类别代码',   `dict_type_name` varchar(64) not null comment '数据字典类别名称',   `dict_item_name` varchar(64) not null comment '数据字典项目名称',   `dict_item_code` varchar(10) default null comment '数据字典项目(可为空)',   `dict_sort` int(10) default null comment '排序字段',   `dict_enable` char(1) not null comment '1:使用 0:停用',   `dict_memo` varchar(64) default null comment '备注',  primary key (`dict_id`) ) engine=innodb default charset=utf8;
(2)映射文件配置 客户实体中引用数据字典对象:
    //引用关联的数据字典对象private basedict cust_source; //客户来源 cust_source.dict_idprivate basedict cust_industry; //客户行业private basedict cust_level; //客户级别
将数据字典对象在映射文件中配置:
        <!-- 多对一 --><many-to-one name="cust_source" column="cust_source" class="basedict" ></many-to-one><many-to-one name="cust_industry" column="cust_industry" class="basedict" ></many-to-one><many-to-one name="cust_level" column="cust_level" class="basedict" ></many-to-one>
2.使用ajax技术在页面加载字典下拉选//使用ajax加载数据字典,生成select//参数1: 数据字典类型 (dict_type_code)//参数2: 将下拉选放入的标签id//参数3: 生成下拉选时,select标签的name属性值//参数4: 需要回显时,选中哪个optionfunction loadselect(typecode,positionid,selectname,selectedid){//1 创建select对象,将name属性指定var $select =  $(<select name="+selectname+" ></select>);//2 添加提示选项$select.append($(<option value='' >---请选择---</option>));//3 使用jquery 的ajax 方法,访问后台action$.post(${pagecontext.request.contextpath}/basedictaction, { dict_type_code:typecode},      function(data){               //遍历//4 返回json数组对象,对其遍历   $.each( data, function(i, json){// 每次遍历创建一个option对象   var $option = $(<option value='"+json['dict_id']+"' >+json[dict_item_name]+</option>);                     if(json['dict_id'] == selectedid){//判断是否需要回显 ,如果需要使其被选中$option.attr(selected,selected);             }//并添加到select对象                $select.append($option);                });       },json);        //5 将组装好的select对象放入页面指定位置$(#+positionid).append($select); }
add.jsp
$(document).ready(function(){     loadselect(006,level,cust_level.dict_id);     loadselect(001,industry,cust_industry.dict_id);     loadselect(009,source,cust_source.dict_id);     });</script>
basedictaction:
public class basedictaction extends actionsupport {private string dict_type_code;    private basedictservice basedictservice;     @overridepublic string execute() throws exception {//1 调用service根据typecode获得数据字典对象listlist<basedict> list = basedictservice.getlistbytypecode(dict_type_code);//2 将list转换为 json格式string json = jsonarray.fromobject(list).tostring();//3 将json发送给浏览器servletactioncontext.getresponse().setcontenttype(application/json;charset=utf-8);         servletactioncontext.getresponse().getwriter().write(json);return null;//告诉struts2不需要进行结果处理    }    public string getdict_type_code() {return dict_type_code;     }public void setdict_type_code(string dict_type_code) {this.dict_type_code = dict_type_code;     }public void setbasedictservice(basedictservice basedictservice) {this.basedictservice = basedictservice;     } }
basedictserviceimpl:
public class basedictserviceimpl implements basedictservice {    private basedictdao bdd;          @overridepublic list<basedict> getlistbytypecode(string dict_type_code) {return bdd.getlistbytypecode(dict_type_code);     }public void setbdd(basedictdao bdd) {this.bdd = bdd;     } }
basedictdaoimpl:
public class basedictdaoimpl extends basedaoimpl<basedict> implements basedictdao {     @overridepublic list<basedict> getlistbytypecode(string dict_type_code) {//criteria        //创建离线查询对象detachedcriteria dc = detachedcriteria.forclass(basedict.class);//封装条件dc.add(restrictions.eq(dict_type_code, dict_type_code));//执行查询list<basedict> list = (list<basedict>) gethibernatetemplate().findbycriteria(dc);        return list;     } }
struts.xml
        <!-- 数据字典action --><action name="basedictaction" class="basedictaction" method="execute" ></action>
applicationcontext.xml
    <bean name="basedictaction" class="cn.xyp.web.action.basedictaction" scope="prototype" ><property name="basedictservice" ref="basedictservice" ></property></bean><bean name="basedictservice" class="cn.xyp.service.impl.basedictserviceimpl" ><property name="bdd" ref="basedictdao" ></property></bean></bean><bean name="basedictdao" class="cn.xyp.dao.impl.basedictdaoimpl" ><!-- 注入sessionfactory --><property name="sessionfactory" ref="sessionfactory" ></property></bean>
3.分析实现新增客户
二、在新增客户中加入文件上传1.文件上传页面3个要求    <!-- 文件上传页面3个要求:1.表单必须post提交2.表单提交类型enctype.必须多段式.3.文件上传使用<input type="file" /> 组件      -->     <form id=form1 name=form1 action="${pagecontext.request.contextpath }/customeraction_add"method="post" enctype="multipart/form-data" >
2.后台接收(记得生成getset方法)    //上传的文件会自动封装到file对象//在后台提供一个与前台input type=file组件 name相同的属性private file photo;//在提交键名后加上固定后缀filename,文件名称会自动封装到属性中private string photofilename;//在提交键名后加上固定后缀contenttype,文件mime类型会自动封装到属性中 private string photocontenttype;
使用:
    public string add() throws exception {if(photo!=null){         system.out.println(文件名称:+photofilename);         system.out.println(文件类型:+photocontenttype);//将上传文件保存到指定位置photo.renameto(new file(e:/upload/haha.jpg));         }
三、客户修改  
以上就是javaee新增客户、数据字典、文件上传和修改客户讲解的详细内容。
其它类似信息

推荐信息