美文网首页
SSH项目_03

SSH项目_03

作者: 编程_书恨少 | 来源:发表于2018-09-04 17:02 被阅读0次

1. 数据字典

一张列举了项目中所有需要用到的枚举项的数据库表

Snip20180903_3.png

2. 实现数据字典的接口请求

2.1 创建model

分析:这里是一对多的关系,DataDict是数据量大的一方,被选择,所以是一的一方。Customer是选择数据的一方,所以是多的一方。
Customer.java

    private Long cust_id;
    
    private String cust_name;
    private String cust_linkman;
    private String cust_phone;
    private String cust_mobile;

    private DataDict cust_source;
    private DataDict cust_industry;
    private DataDict cust_level;

Customer.hbm.xml

<hibernate-mapping package="domain" >

        <class name="Customer" table="t_customer"  lazy="true">

            <id name="cust_id"  >
                <generator class="native"></generator>
            </id>

            <property name="cust_name"></property>
            <property name="cust_linkman" ></property>
            <property name="cust_phone"  ></property>
            <property name="cust_mobile"  ></property>

            <!-- 多对一 -->
            <many-to-one name="cust_source" column="cust_source" class="domain.DataDict"></many-to-one>
            <many-to-one name="cust_industry" column="cust_industry" class="domain.DataDict"></many-to-one>
            <many-to-one name="cust_level" column="cust_level" class="domain.DataDict"></many-to-one>
        </class>
    </hibernate-mapping>

DataDict.java

    private String dict_id;
    private String dict_type_code;
    private String dict_type_name;
    private String dict_item_name;
    private String dict_item_code;
    private String dict_memo;
    private Integer dict_sort;
    private Character dict_enable;

DataDict.hbm.xml

<hibernate-mapping package="domain" >

        <class name="DataDict" table="data_dict" lazy="true">


            <!--
                    对于String类型的主键,只能选择uuid和assigned(手动指定),
                    这里一般情况下数据字典都是通过手动的方式,在表中进行更改的
            -->
            <id name="dict_id"  >
                <generator class="assigned"></generator>
            </id>

            <property name="dict_type_code"  ></property>
            <property name="dict_type_name"  ></property>
            <property name="dict_item_name" ></property>
            <property name="dict_item_code" ></property>
            <property name="dict_memo"  ></property>
            <property name="dict_sort"  ></property>
            <property name="dict_enable" ></property>
    </class>
</hibernate-mapping>

DataDictAction

public class DataDictAction extends ActionSupport {

    // service
    private DataDictService dataDictService;

    public void setDataDictService(DataDictService dataDictService) {
        this.dataDictService = dataDictService;
    }

    // 接收用户输入的数据
    private String dict_type_code;

    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;
    }



    @Override
    public String execute() throws Exception {

        List<DataDict> list = dataDictService.getListByTypeCode(dict_type_code);
        // 对象转换成json字符串
        String jsonString = JSONArray.fromObject(list).toString();


        ServletActionContext.getResponse().setContentType("application/json;charset=utf-8");

        ServletActionContext.getResponse().getWriter().write(jsonString);

        // 告诉Struts2不需要处理结果
        return null;
    }
}

DataDictService


public interface DataDictService {

    List<DataDict> getListByTypeCode(String dict_type_code);
}

DataDictServiceImpl

public class DataDictServiceImpl implements DataDictService {

    DataDictDao dataDictDao;

    public void setDataDictDao(DataDictDao dataDictDao) {
        this.dataDictDao = dataDictDao;
    }


    @Override
    public List<DataDict> getListByTypeCode(String dict_type_code) {

        List<DataDict> list = dataDictDao.getListByTypeCode(dict_type_code);

        return list;
    }
}

DataDictDao

public interface DataDictDao extends BaseDao<DataDict> {
    List <DataDict> getListByTypeCode(String dict_type_code);
}

DataDictDaoImpl

public class DataDictDaoImpl extends BaseDaoImpl<DataDict> implements DataDictDao {

    @Override
    public List<DataDict> getListByTypeCode(String dict_type_code) {

        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(DataDict.class);

        detachedCriteria.add(Restrictions.eq("dict_type_code", dict_type_code));

        List<DataDict> list = (List<DataDict>) getHibernateTemplate().findByCriteria(detachedCriteria);

        return list;
    }
}

3. 封装前端下拉选的js函数

封装selectedList.js

//使用ajax加载数据字典,生成select
//参数1: 数据字典类型 (dict_type_code)
//参数2: 将下啦选放入的标签id
//参数3: 生成下拉选时,select标签的name属性值
//参数4: 需要回显时,选中哪个option
function 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中调用函数

<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/selectedList.js"></script>
<script type="text/javascript">


$(document).ready(function(){
        
    loadSelect("006","level","cust_level.dict_id" <s:if test="#customer.cust_level!=null">,<s:property value="#customer.cust_level.dict_id" /></s:if>);
    loadSelect("001","industry","cust_industry.dict_id" <s:if test="#customer.cust_industry!=null">,<s:property value="#customer.cust_industry.dict_id" /></s:if>);
    loadSelect("009","source","cust_source.dict_id" <s:if test="#customer.cust_source!=null">,<s:property value="#customer.cust_source.dict_id" /></s:if>);
    
    
    });
</script>

4. struts2文件上传

在CustomerAction中


public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {

    //上传的文件会自动封装到File对象
    //在后台提供一个与前台input type=file组件 name相同的属性
    private File photo;
    //在提交键名后加上固定后缀FileName,文件名称会自动封装到属性中
    private String photoFileName;
    //在提交键名后加上固定后缀ContentType,文件MIME类型会自动封装到属性中 
    private String photoContentType;

     public File getPhoto() {
        return photo;
    }

    public void setPhoto(File photo) {
        this.photo = photo;
    }

    public String getPhotoContentType() {
        return photoContentType;
    }

    public void setPhotoContentType(String photoContentType) {
        this.photoContentType = photoContentType;
    }

    public String getPhotoFileName() {
        return photoFileName;
    }

    public void setPhotoFileName(String photoFileName) {
        this.photoFileName = photoFileName;
    }

    public String add() throws Exception {
        if(photo!=null){
            System.out.println("文件名称:"+photoFileName);
            System.out.println("文件类型:"+photoContentType);
            //将上传文件保存到指定位置
            photo.renameTo(new File("E:/upload/haha.jpg"));
        }
    }
}

相关文章

网友评论

      本文标题:SSH项目_03

      本文链接:https://www.haomeiwen.com/subject/tlhewftx.html