CRM第二天:用户登陆和客户管理保存客户功能实现

目录

1.CRM:用户模块登录功能

1.1编写登录页面

1.2编写Action中login方法

1.3编写Service

1.4编写DAO

1.5配置页面的跳转

1.6在页面中显示数据

2.CRM:客户管理保存客户

2.1客户管理:准备工作

2.1.1创建表

2.1.2创建实体和映射

2.1.3创建Action

2.1.4创建Service

2.1.5创建DAO

2.1.6配置Action、Service、DAO

2.2跳转到添加页面

2.2.1修改左侧菜单页面

2.2.2编写Action中的saveUI的方法和添加页面

2.2.3配置Action的跳转

2.2.4测试跳转

2.3引入数据字典

2.3.1什么是数据字典

2.3.2创建数据字典表

2.3.3客户表和字典表的关系分析

2.3.4创建字典的实体和映射

2.3.5修改字典和客户的关系映射

2.3.6将映射文件交给Spring

2.4在添加页面上异步加载字典数据

2.4.1创建字典的Action、Service、DAO

2.4.2将字典类交给Spring管理

2.4.3添加页面上引入jquery的js

2.4.4编写异步加载的方法

2.4.5编写Action

2.4.6编写Service

2.4.7编写DAO

2.4.8编写其他字典项数据异步加载

2.5保存数据到数据库中

2.5.1修改添加页面

2.5.2编写Action

2.5.3编写Service

2.5.4编写DAO

2.5.6业务层添加注解事务:@Transactional


 

1.CRM:用户模块登录功能

1.1编写登录页面


  
  1. <form action="${pageContext.request.contextPath}/user_login.action" method="post">
  2.                        <div id="login_tip">
  3.                             用户登录&nbsp;&nbsp;UserLogin
  4.                        </div>

1.2编写Action中login方法

 


  
  1. /*
  2.      * 用户登陆的方法
  3.      */
  4.      public String login(){
  5.          User loginuser=userService.login(user);
  6.          if(user==null){
  7.              //登陆失败,保存错误信息
  8.               this.addActionError("用户名或密码错误!");
  9.               return LOGIN;
  10.          }else{
  11.               //登陆成功,保存登陆的用户
  12.               ActionContext.getContext().getSession().put("loginuser", loginuser);
  13.               return SUCCESS;
  14.          }
  15.         
  16.         
  17.      }

1.3编写Service

 


  
  1. //登陆方法
  2.      @Override
  3.      public User login(User user) {
  4.          // 对密码进行加密处理
  5.          user.setUser_password(MD5Utils.md5(user.getUser_password()));
  6.          return userDao.find(user);
  7.      }

1.4编写DAO

 


  
  1. //查找方法:根据用户名和密码进行查询
  2.      @Override
  3.      public User find(User user) {
  4.          List<User> users=(List<User>) this.getHibernateTemplate().find("from user where user_code=? and user_password=?", user.getUser_code(),user.getUser_password());
  5.          if(users.size()>0){
  6.               return users.get(0);
  7.          }
  8.          return null;
  9.      }

1.5配置页面的跳转


  
  1. <!-- 配置Action -->
  2.      <package name="crm" extends="struts-default" namespace="/">
  3.          <action name="user_*" class="userAction" method="{1}">
  4.               <result name="login" type="redirect">/login.jsp</result>
  5.               <result name="success" type="redirect">/index.jsp</result>
  6.          </action>
  7.      </package>

1.6在页面中显示数据

  1. 在主页面上显示登陆用户信息

  
  1. <div class="info_center">
  2.               <s:property value="#session.loginuser.user_name"/>
  3.               <span id="nt">通知</span><span><a href="#" id="notice">3</a></span>
  4. </div>
  1. 在登陆页面上显示错误信息
<s:actionerror/>
 

 

2.CRM:客户管理保存客户

2.1客户管理:准备工作

2.1.1创建表


  
  1. CREATE TABLE `cst_customer` (
  2.   `cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)',
  3.   `cust_name` varchar(32) NOT NULL COMMENT '客户名称(公司名称)',
  4.   `cust_source` varchar(32) DEFAULT NULL COMMENT '客户信息来源',
  5.   `cust_industry` varchar(32) DEFAULT NULL COMMENT '客户所属行业',
  6.   `cust_level` varchar(32) DEFAULT NULL COMMENT '客户级别',
  7.   `cust_phone` varchar(64) DEFAULT NULL COMMENT '固定电话',
  8.   `cust_mobile` varchar(16) DEFAULT NULL COMMENT '移动电话',
  9.   PRIMARY KEY (`cust_id`)
  10. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

2.1.2创建实体和映射

  1. 创建实体

  
  1. public class Customer {
  2.      private Long cust_id;
  3.      private String cust_name;
  4.      private String cust_source;
  5.      private String cust_industry;
  6.      private String cust_level;
  7.      private String cust_phone;
  8.      private String cust_mobile;
  9.      public Long getCust_id() {
  10.          return cust_id;
  11.      }
  12.      public void setCust_id(Long cust_id) {
  13.          this.cust_id = cust_id;
  14.      }
  15.      public String getCust_name() {
  16.          return cust_name;
  17.      }
  18.      public void setCust_name(String cust_name) {
  19.          this.cust_name = cust_name;
  20.      }
  21.      public String getCust_source() {
  22.          return cust_source;
  23.      }
  24.      public void setCust_source(String cust_source) {
  25.          this.cust_source = cust_source;
  26.      }
  27.      public String getCust_industry() {
  28.          return cust_industry;
  29.      }
  30.      public void setCust_industry(String cust_industry) {
  31.          this.cust_industry = cust_industry;
  32.      }
  33.      public String getCust_level() {
  34.          return cust_level;
  35.      }
  36.      public void setCust_level(String cust_level) {
  37.          this.cust_level = cust_level;
  38.      }
  39.      public String getCust_phone() {
  40.          return cust_phone;
  41.      }
  42.      public void setCust_phone(String cust_phone) {
  43.          this.cust_phone = cust_phone;
  44.      }
  45.      public String getCust_mobile() {
  46.          return cust_mobile;
  47.      }
  48.      public void setCust_mobile(String cust_mobile) {
  49.          this.cust_mobile = cust_mobile;
  50.      }
  51.     
  52.     
  53. }
  1. 创建映射

 


  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3.     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4.     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping>
  6.      <!-- 建立类与表的映射 -->
  7.      <class name="com.albertyy.crm.entity.Customer" table="cst_customer" >
  8.          <!-- 建立类中的属性与表中的主键对应 -->
  9.          <id name="cust_id" column="cust_id" >
  10.               <!-- 主键生成策略 -->
  11.               <generator class="native"/>
  12.          </id>
  13.         
  14.          <!-- 建立类中的普通的属性和表的字段的对应 -->
  15.          <property name="cust_name" column="cust_name"  />
  16.          <property name="cust_source" column="cust_source" />
  17.          <property name="cust_industry" column="cust_industry"/>
  18.          <property name="cust_level" column="cust_level"/>
  19.          <property name="cust_phone" column="cust_phone"/>
  20.          <property name="cust_mobile" column="cust_mobile"/>
  21.      </class>
  22.     
  23.     
  24. </hibernate-mapping>

2.1.3创建Action


  
  1. public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {
  2.     //模型驱动使用的对象
  3.      private Customer customer =new Customer();
  4.      @Override
  5.      public Customer getModel() {
  6.          // TODO Auto-generated method stub
  7.          return customer;
  8.      }
  9.      //注入Services
  10.      private CustomerService customerService;
  11.      public void setCustomerService(CustomerService customerService) {
  12.          this.customerService = customerService;
  13.      }
  14.    
  15. }

2.1.4创建Service


  
  1. public class CustomerServiceImpl implements CustomerService {
  2.     //注入Dao
  3.      private CustomerDao customerDao;
  4.      public void setCustomerDao(CustomerDao customerDao) {
  5.          this.customerDao = customerDao;
  6.      }
  7.     
  8. }

2.1.5创建DAO


  
  1. /**  
  2. *   
  3. * 项目名称:CRM  
  4. * 类名称:CustomerDaoImpl  
  5. * 类描述:客户管理Dao实现类  
  6. * 创建人:yangyangyang  
  7. * 创建时间:2018年12月16日 下午4:18:24  
  8. * 修改人:yangyangyang  
  9. * 修改时间:2018年12月16日 下午4:18:24  
  10. * 修改备注:  
  11. * @version   
  12. *   
  13. */
  14. public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {
  15. }

 

2.1.6配置Action、Service、DAO

 


  
  1. <!-- ======配置客户相关类======= -->
  2.      <!-- 配置Action -->
  3.      <bean id="customerAction" class="com.albertyy.crm.web.action.CustomerAction" scope="prototype">
  4.          <property name="customerService" ref="customerService"/>
  5.      </bean>
  6.      <!-- 配置Service -->
  7.      <bean id="customerService" class="com.albertyy.crm.serviceImpl.CustomerServiceImpl">
  8.          <property name="customerDao" ref="customerDao"/>
  9.      </bean>
  10.      <!-- 配置DAO -->
  11.      <bean id="customerDao" class="com.albertyy.crm.daoImpl.CustomerDaoImpl">
  12.          <property name="sessionFactory" ref="sessionFactory"/>
  13.      </bean>

2.2跳转到添加页面

2.2.1修改左侧菜单页面

<li><a htef="${pageContext.request.contextPath }/customer_saveUI.action">新增客户</a></li>
 

 

2.2.2编写Action中的saveUI的方法和添加页面


  
  1. //客户管理跳转到添加页面
  2.      public String saveUI(){
  3.          return "saveUI";
  4.      }

2.2.3配置Action的跳转


  
  1. <!-- 客户管理Action -->
  2.          <action name="customer_*" class="customerAction" method="{1}">
  3.               <result name="saveUI" >/customer/add.jsp</result>
  4.          </action>

2.2.4测试跳转

2.3引入数据字典

2.3.1什么是数据字典

数据字典用来规范某些地方具体值和数据

2.3.2创建数据字典表


  
  1. CREATE TABLE `base_dict` (
  2.   `dict_id` varchar(32) NOT NULL COMMENT '数据字典id(主键)',
  3.   `dict_type_code` varchar(10) NOT NULL COMMENT '数据字典类别代码',
  4.   `dict_type_name` varchar(64) NOT NULL COMMENT '数据字典类别名称',
  5.   `dict_item_name` varchar(64) NOT NULL COMMENT '数据字典项目名称',
  6.   `dict_item_code` varchar(10) DEFAULT NULL COMMENT '数据字典项目(可为空)',
  7.   `dict_sort` int(10) DEFAULT NULL COMMENT '排序字段',
  8.   `dict_enable` char(1) NOT NULL COMMENT '1:使用 0:停用',
  9.   `dict_memo` varchar(64) DEFAULT NULL COMMENT '备注',
  10.   PRIMARY KEY (`dict_id`)
  11. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

2.3.3客户表和字典表的关系分析

2.3.4创建字典的实体和映射

  1. 创建实体

  
  1. public class BaseDict {
  2.     private String dict_id;
  3.     private String dict_type_code;
  4.     private String dict_type_name;
  5.     private String dict_item_name;
  6.     private String dict_item_code;
  7.     private String dict_sort;
  8.     private String dict_enable;
  9.     private String dict_memo;
  10.      public String getDict_id() {
  11.          return dict_id;
  12.      }
  13.      public void setDict_id(String dict_id) {
  14.          this.dict_id = dict_id;
  15.      }
  16.      public String getDict_type_code() {
  17.          return dict_type_code;
  18.      }
  19.      public void setDict_type_code(String dict_type_code) {
  20.          this.dict_type_code = dict_type_code;
  21.      }
  22.      public String getDict_type_name() {
  23.          return dict_type_name;
  24.      }
  25.      public void setDict_type_name(String dict_type_name) {
  26.          this.dict_type_name = dict_type_name;
  27.      }
  28.      public String getDict_item_name() {
  29.          return dict_item_name;
  30.      }
  31.      public void setDict_item_name(String dict_item_name) {
  32.          this.dict_item_name = dict_item_name;
  33.      }
  34.      public String getDict_item_code() {
  35.          return dict_item_code;
  36.      }
  37.      public void setDict_item_code(String dict_item_code) {
  38.          this.dict_item_code = dict_item_code;
  39.      }
  40.      public String getDict_sort() {
  41.          return dict_sort;
  42.      }
  43.      public void setDict_sort(String dict_sort) {
  44.          this.dict_sort = dict_sort;
  45.      }
  46.      public String getDict_enable() {
  47.          return dict_enable;
  48.      }
  49.      public void setDict_enable(String dict_enable) {
  50.           this.dict_enable = dict_enable;
  51.      }
  52.      public String getDict_memo() {
  53.          return dict_memo;
  54.      }
  55.      public void setDict_memo(String dict_memo) {
  56.          this.dict_memo = dict_memo;
  57.      }
  58.    
  59. }

 

  1. 创建映射

 


  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3.     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4.     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping>
  6.      <class name="com.albertyy.crm.entity.BaseDict" table="base_dict">
  7.          <id name="dict_id" column="dict_id">
  8.               <generator class="uuid"/>
  9.          </id>
  10.         
  11.          <property name="dict_type_code" column="dict_type_code"/>
  12.          <property name="dict_type_name" column="dict_type_name"/>
  13.          <property name="dict_item_name" column="dict_item_name"/>
  14.          <property name="dict_item_code" column="dict_item_code"/>
  15.          <property name="dict_sort" column="dict_sort"/>
  16.          <property name="dict_enable" column="dict_enable"/>
  17.          <property name="dict_memo" column="dict_memo"/>
  18.         
  19.      </class>
  20. </hibernate-mapping>

2.3.5修改字典和客户的关系映射

  1. 修改了客户的实体

 


  
  1. package com.albertyy.crm.entity;
  2. /**
  3.  * 客户管理的实体
  4.  * @author yxy
  5.  *Create database ssh1;
  6. Use ssh1;
  7. CREATE TABLE `cst_customer` (
  8.   `cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)',
  9.   `cust_name` varchar(32) NOT NULL COMMENT '客户名称(公司名称)',
  10.   `cust_source` varchar(32) DEFAULT NULL COMMENT '客户信息来源',
  11.   `cust_industry` varchar(32) DEFAULT NULL COMMENT '客户所属行业',
  12.   `cust_level` varchar(32) DEFAULT NULL COMMENT '客户级别',
  13.   `cust_phone` varchar(64) DEFAULT NULL COMMENT '固定电话',
  14.   `cust_mobile` varchar(16) DEFAULT NULL COMMENT '移动电话',
  15.   PRIMARY KEY (`cust_id`)
  16. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
  17.  */
  18. public class Customer {
  19.      private Long cust_id;
  20.      private String cust_name;
  21.      /*private String cust_source;
  22.      private String cust_industry;
  23.      private String cust_level;*/
  24.      private String cust_phone;
  25.      private String cust_mobile;
  26.     
  27.      //客户和字典的关系是多对一,所以需要在多的一方,放一的一方的对象
  28.      private BaseDict baseDictSource;
  29.      private BaseDict baseDictIndustry;
  30.      private BaseDict baseDictLevel;
  31.     
  32.      public BaseDict getBaseDictSource() {
  33.          return baseDictSource;
  34.      }
  35.      public void setBaseDictSource(BaseDict baseDictSource) {
  36.          this.baseDictSource = baseDictSource;
  37.      }
  38.      public BaseDict getBaseDictIndustry() {
  39.          return baseDictIndustry;
  40.      }
  41.      public void setBaseDictIndustry(BaseDict baseDictIndustry) {
  42.          this.baseDictIndustry = baseDictIndustry;
  43.      }
  44.      public BaseDict getBaseDictLevel() {
  45.          return baseDictLevel;
  46.      }
  47.      public void setBaseDictLevel(BaseDict baseDictLevel) {
  48.          this.baseDictLevel = baseDictLevel;
  49.      }
  50.      public Long getCust_id() {
  51.          return cust_id;
  52.      }
  53.      public void setCust_id(Long cust_id) {
  54.          this.cust_id = cust_id;
  55.      }
  56.      public String getCust_name() {
  57.          return cust_name;
  58.      }
  59.      public void setCust_name(String cust_name) {
  60.          this.cust_name = cust_name;
  61.      }
  62.     
  63.      public String getCust_phone() {
  64.          return cust_phone;
  65.      }
  66.      public void setCust_phone(String cust_phone) {
  67.          this.cust_phone = cust_phone;
  68.      }
  69.      public String getCust_mobile() {
  70.          return cust_mobile;
  71.      }
  72.      public void setCust_mobile(String cust_mobile) {
  73.          this.cust_mobile = cust_mobile;
  74.      }
  75.     
  76.     
  77. }

 

  1. 修改客户的映射

 


  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3.     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4.     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping>
  6.      <!-- 建立类与表的映射 -->
  7.      <class name="com.albertyy.crm.entity.Customer" table="cst_customer" >
  8.          <!-- 建立类中的属性与表中的主键对应 -->
  9.          <id name="cust_id" column="cust_id" >
  10.               <!-- 主键生成策略 -->
  11.               <generator class="native"/>
  12.          </id>
  13.         
  14.          <!-- 建立类中的普通的属性和表的字段的对应 -->
  15.          <property name="cust_name" column="cust_name"  />
  16.          <!-- <property name="cust_source" column="cust_source" />
  17.          <property name="cust_industry" column="cust_industry"/>
  18.          <property name="cust_level" column="cust_level"/> -->
  19.          <property name="cust_phone" column="cust_phone"/>
  20.          <property name="cust_mobile" column="cust_mobile"/>
  21.         
  22.          <!-- 配置客户和字典多对一的关系 -->
  23.          <many-to-one name="baseDictSource" class="com.albertyy.crm.entity.BaseDict" column="cust_source"></many-to-one>
  24.          <many-to-one name="baseDictIndustry" class="com.albertyy.crm.entity.BaseDict" column="cust_industry"></many-to-one>
  25.          <many-to-one name="baseDictLevel" class="com.albertyy.crm.entity.BaseDict" column="cust_level"></many-to-one>
  26.      </class>
  27.     
  28.     
  29. </hibernate-mapping>

2.3.6将映射文件交给Spring


  
  1. <!-- 引入映射文件 -->
  2.          <property name="mappingResources">
  3.               <list>
  4.                    <value>com/albertyy/crm/entity/User.hbm.xml</value>
  5.                    <value>com/albertyy/crm/entity/Customer.hbm.xml</value>
  6.                    <value>com/albertyy/crm/entity/BaseDict.hbm.xml</value>
  7.               </list>
  8.          </property>

2.4在添加页面上异步加载字典数据

2.4.1创建字典的Action、Service、DAO

  1. 编写DAO

  
  1. /**  
  2. *   
  3. * 项目名称:CRM  
  4. * 类名称:BaseDictDaoImpl  
  5. * 类描述:  字典的Dao实现类
  6. * 创建人:yangyangyang  
  7. * 创建时间:2018年12月16日 下午7:48:55  
  8. * 修改人:yangyangyang  
  9. * 修改时间:2018年12月16日 下午7:48:55  
  10. * 修改备注:  
  11. * @version   
  12. *   
  13. */
  14. public class BaseDictDaoImpl extends HibernateDaoSupport implements BaseDictDao {
  15. }
  1. 编写Service

  
  1. /**  
  2. *   
  3. * 项目名称:CRM  
  4. * 类名称:BaseDictServiceImpl  
  5. * 类描述:  字典业务层实现类
  6. * 创建人:yangyangyang  
  7. * 创建时间:2018年12月16日 下午7:51:38  
  8. * 修改人:yangyangyang  
  9. * 修改时间:2018年12月16日 下午7:51:38  
  10. * 修改备注:  
  11. * @version   
  12. *   
  13. */
  14. public class BaseDictServiceImpl implements BaseDictServie {
  15.    //注入Dao
  16.      private BaseDictDao baseDictDao;
  17.      public void setBaseDictDao(BaseDictDao baseDictDao) {
  18.          this.baseDictDao = baseDictDao;
  19.      }
  20.     
  21. }
  1. 编写Action


 


  
  1. /**  
  2. *   
  3. * 项目名称:CRM  
  4. * 类名称:BaseDictAction  
  5. * 类描述:字典的Action  
  6. * 创建人:yangyangyang  
  7. * 创建时间:2018年12月16日 下午7:54:21  
  8. * 修改人:yangyangyang  
  9. * 修改时间:2018年12月16日 下午7:54:21  
  10. * 修改备注:  
  11. * @version   
  12. *   
  13. */
  14. public class BaseDictAction extends ActionSupport implements ModelDriven<BaseDict> {
  15.     //模型驱动使用的对象
  16.      private BaseDict baseDict =new BaseDict();
  17.      @Override
  18.      public BaseDict getModel() {
  19.          // TODO Auto-generated method stub
  20.          return baseDict;
  21.      }
  22.     //注入Service
  23.      private BaseDictService baseDictService;
  24.      public void setBaseDictService(BaseDictService baseDictService) {
  25.          this.baseDictService = baseDictService;
  26.      }
  27.     
  28. }

2.4.2将字典类交给Spring管理


  
  1. <!-- ======配置字典的相关类======= -->
  2.      <!-- 配置Action -->
  3.      <bean id="baseDictAction" class="com.albertyy.crm.web.action.BaseDictAction" scope="prototype">
  4.          <property name="baseDictService" ref="baseDictService"/>
  5.      </bean>
  6.      <!-- 配置Service -->
  7.      <bean id="baseDictService" class="com.albertyy.crm.serviceImpl.BaseDictServiceImpl">
  8.          <property name="baseDictDao" ref="baseDictDao"/>
  9.      </bean>
  10.      <!-- 配置DAO -->
  11.      <bean id="baseDictDao" class="com.albertyy.crm.daoImpl.BaseDictDaoImpl">
  12.          <property name="sessionFactory" ref="sessionFactory"/>
  13.      </bean>

2.4.3添加页面上引入jquery的js

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

2.4.4编写异步加载的方法


  
  1. //页面加载,异步查询字典数据
  2. //页面加载函数就会执行:
  3. $(function(){
  4.      // 加载客户来源
  5.      $.post("${pageContext.request.contextPath }/baseDict_findByTypeCode.action",{"dict_type_code":"002"},function(data){
  6.          //遍历JOSN类型的数据
  7.          $(data).each(function(i,n){
  8.               $("#cust_source").append("<option value='"+n.dict_id+"'>"+n.dict_item_name+"</option>");
  9.          });
  10.      },"json");
  11. });

2.4.5编写Action


  
  1. package com.albertyy.crm.web.action;
  2. import java.io.IOException;
  3. import java.util.List;
  4. import org.apache.struts2.ServletActionContext;
  5. import com.albertyy.crm.entity.BaseDict;
  6. import com.albertyy.crm.service.BaseDictService;
  7. import com.opensymphony.xwork2.ActionSupport;
  8. import com.opensymphony.xwork2.ModelDriven;
  9. import net.sf.json.JSONArray;
  10. import net.sf.json.JsonConfig;
  11. /**  
  12. *   
  13. * 项目名称:CRM  
  14. * 类名称:BaseDictAction  
  15. * 类描述:字典的Action  
  16. * 创建人:yangyangyang  
  17. * 创建时间:2018年12月16日 下午7:54:21  
  18. * 修改人:yangyangyang  
  19. * 修改时间:2018年12月16日 下午7:54:21  
  20. * 修改备注:  
  21. * @version   
  22. *   
  23. */
  24. public class BaseDictAction extends ActionSupport implements ModelDriven<BaseDict> {
  25.     //模型驱动使用的对象
  26.      private BaseDict baseDict;
  27.      @Override
  28.      public BaseDict getModel() {
  29.          // TODO Auto-generated method stub
  30.          return baseDict;
  31.      }
  32.     //注入Service
  33.      private BaseDictService baseDictService;
  34.      public void setBaseDictService(BaseDictService baseDictService) {
  35.          this.baseDictService = baseDictService;
  36.      }
  37.     
  38.      //根据类型名称查询字典
  39.      public String findByTypeCode(){
  40.          //调用业务层查询方法
  41.          List<BaseDict> list=baseDictService.findByTypeCode(baseDict.getDict_type_code());
  42.          //使用jsonlib将list转换成JSON(常用工具jsonlib fastjson)
  43.          /*
  44.           * JsonConfig:转JSON的配置对象
  45.           * JSONArray:将数组或者list集合转成JSON
  46.           * JSONObject:将对象或者map集合转成JSON
  47.           */
  48.          JsonConfig jsonConfig=new JsonConfig();
  49.          jsonConfig.setExcludes(new String[]{"dict_sort","dict_enable","dict_memo"});
  50.          JSONArray jsonArray=JSONArray.fromObject(list,jsonConfig);
  51.          //将JSON数据传到页面
  52.          try {
  53.               ServletActionContext.getResponse().getWriter().println(jsonArray.toString());
  54.          } catch (IOException e) {
  55.               e.printStackTrace();
  56.          }
  57.         
  58.          return NONE;
  59.      }
  60. }

配置action


  
  1. <!-- 字典管理Action -->
  2.          <action name="baseDict_*" class="baseDictAction" method="{1}">
  3.              
  4.          </action>

2.4.6编写Service


  
  1. public class BaseDictServiceImpl implements BaseDictService {
  2.    //注入Dao
  3.      private BaseDictDao baseDictDao;
  4.      public void setBaseDictDao(BaseDictDao baseDictDao) {
  5.          this.baseDictDao = baseDictDao;
  6.      }
  7.     //业务层根据类型查询
  8.      @Override
  9.      public List<BaseDict> findByTypeCode(String dict_type_code) {
  10.          return baseDictDao.findByTypeCode(dict_type_code);
  11.      }
  12.     
  13. }

2.4.7编写DAO


  
  1. public class BaseDictDaoImpl extends HibernateDaoSupport implements BaseDictDao {
  2.     //根据类型编码查询字典数据
  3.      @Override
  4.      public List<BaseDict> findByTypeCode(String dict_type_code) {
  5.          return (List<BaseDict>) this.getHibernateTemplate().find("from BaseDict where dict_type_code=?", dict_type_code);
  6.      }
  7. }

2.4.8编写其他字典项数据异步加载


  
  1. //页面加载,异步查询字典数据
  2. //页面加载函数就会执行:
  3. $(function(){
  4.     // 加载客户来源
  5. $.post("${pageContext.request.contextPath }/baseDict_findByTypeCode.action",{"dict_type_code":"002"},function(data){
  6.         //遍历JOSN类型的数据
  7.         $(data).each(function(i,n){
  8.             $("#cust_source").append("<option value='"+n.dict_id+"'>"+n.dict_item_name+"</option>");
  9.         });
  10.     },"json");
  11.    
  12.     // 加载客户所属行业
  13. $.post("${pageContext.request.contextPath }/baseDict_findByTypeCode.action",{"dict_type_code":"001"},function(data){
  14.         //遍历JOSN类型的数据
  15.         $(data).each(function(i,n){
  16.             $("#cust_industry").append("<option value='"+n.dict_id+"'>"+n.dict_item_name+"</option>");
  17.         });
  18.     },"json");
  19.    
  20.     // 加载客户级别
  21. $.post("${pageContext.request.contextPath }/baseDict_findByTypeCode.action",{"dict_type_code":"006"},function(data){
  22.         //遍历JOSN类型的数据
  23.         $(data).each(function(i,n){
  24.             $("#cust_level").append("<option value='"+n.dict_id+"'>"+n.dict_item_name+"</option>");
  25.         });
  26.     },"json");
  27. });

2.5保存数据到数据库中

2.5.1修改添加页面

<form action="${pageContext.request.contextPath }/customer_save.action" method="post" class="jqtransform">
 
  1. 修改表单项名称:

 


  
  1. <tr >
  2.                   <td class="td_right">信息来源:</td>
  3.                   <td class="">
  4.                     <span class="fl">
  5.                       <div class="select_border">
  6.                         <div class="select_containers ">
  7.                         <select name="baseDictSource.dict_id" id="cust_source" class="select">
  8.                         <option>-----</option>
  9.             
  10.                         </select>
  11.                         </div>
  12.                       </div>
  13.                     </span>
  14.                   </td>
  15.                  </tr>
  16.                 <tr >
  17.                   <td class="td_right">所属行业:</td>
  18.                   <td class="">
  19.                     <span class="fl">
  20.                       <div class="select_border">
  21.                         <div class="select_containers ">
  22.                         <select name="baseDictIndustry.dict_id" id="cust_industry" class="select">
  23.                         <option>-----</option>
  24.                         </select>
  25.                         </div>
  26.                       </div>
  27.                     </span>
  28.                   </td>
  29.                  </tr>
  30.                  <tr >
  31.                   <td class="td_right">客户级别:</td>
  32.                   <td class="">
  33.                     <span class="fl">
  34.                       <div class="select_border">
  35.                         <div class="select_containers ">
  36.                         <select name="baseDictLevel.dict_id" id="cust_level" class="select">
  37.                         <option>-----</option>
  38.                         </select>
  39.                         </div>
  40.                       </div>
  41.                     </span>
  42.                   </td>
  43.                  </tr>

2.5.2编写Action


  
  1. //保存客户的方法
  2.      public String save(){
  3.          customerService.save(customer);
  4.          return NONE;
  5.      }

2.5.3编写Service


  
  1. //保存客户
  2.      @Override
  3.      public void save(Customer customer) {
  4.          customerDao.save(customer);
  5.      }

2.5.4编写DAO

 


  
  1. public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {
  2.     //保存客户
  3.      @Override
  4.      public void save(Customer customer) {
  5.          this.getHibernateTemplate().save(customer);
  6.      }
  7. }

2.5.6业务层添加注解事务:@Transactional

 

 

 

文章来源: albertyang.blog.csdn.net,作者:Albert Yang,版权归原作者所有,如需转载,请联系作者。

原文链接:albertyang.blog.csdn.net/article/details/85042210

(完)