目录
1.CRM:用户模块登录功能
1.1编写登录页面
<form action="${pageContext.request.contextPath}/user_login.action" method="post">
<div id="login_tip">
用户登录 UserLogin
</div>
1.2编写Action中login方法
/*
* 用户登陆的方法
*/
public String login(){
User loginuser=userService.login(user);
if(user==null){
//登陆失败,保存错误信息
this.addActionError("用户名或密码错误!");
return LOGIN;
}else{
//登陆成功,保存登陆的用户
ActionContext.getContext().getSession().put("loginuser", loginuser);
return SUCCESS;
}
}
1.3编写Service
//登陆方法
@Override
public User login(User user) {
// 对密码进行加密处理
user.setUser_password(MD5Utils.md5(user.getUser_password()));
return userDao.find(user);
}
1.4编写DAO
//查找方法:根据用户名和密码进行查询
@Override
public User find(User user) {
List<User> users=(List<User>) this.getHibernateTemplate().find("from user where user_code=? and user_password=?", user.getUser_code(),user.getUser_password());
if(users.size()>0){
return users.get(0);
}
return null;
}
1.5配置页面的跳转
<!-- 配置Action -->
<package name="crm" extends="struts-default" namespace="/">
<action name="user_*" class="userAction" method="{1}">
<result name="login" type="redirect">/login.jsp</result>
<result name="success" type="redirect">/index.jsp</result>
</action>
</package>
1.6在页面中显示数据
- 在主页面上显示登陆用户信息
<div class="info_center">
<s:property value="#session.loginuser.user_name"/>
<span id="nt">通知</span><span><a href="#" id="notice">3</a></span>
</div>
- 在登陆页面上显示错误信息
<s:actionerror/>
2.CRM:客户管理保存客户
2.1客户管理:准备工作
2.1.1创建表
CREATE TABLE `cst_customer` (
`cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)',
`cust_name` varchar(32) NOT NULL COMMENT '客户名称(公司名称)',
`cust_source` varchar(32) DEFAULT NULL COMMENT '客户信息来源',
`cust_industry` varchar(32) DEFAULT NULL COMMENT '客户所属行业',
`cust_level` varchar(32) DEFAULT NULL COMMENT '客户级别',
`cust_phone` varchar(64) DEFAULT NULL COMMENT '固定电话',
`cust_mobile` varchar(16) DEFAULT NULL COMMENT '移动电话',
PRIMARY KEY (`cust_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
2.1.2创建实体和映射
- 创建实体
public class Customer {
private Long cust_id;
private String cust_name;
private String cust_source;
private String cust_industry;
private String cust_level;
private String cust_phone;
private String cust_mobile;
public Long getCust_id() {
return cust_id;
}
public void setCust_id(Long cust_id) {
this.cust_id = cust_id;
}
public String getCust_name() {
return cust_name;
}
public void setCust_name(String cust_name) {
this.cust_name = cust_name;
}
public String getCust_source() {
return cust_source;
}
public void setCust_source(String cust_source) {
this.cust_source = cust_source;
}
public String getCust_industry() {
return cust_industry;
}
public void setCust_industry(String cust_industry) {
this.cust_industry = cust_industry;
}
public String getCust_level() {
return cust_level;
}
public void setCust_level(String cust_level) {
this.cust_level = cust_level;
}
public String getCust_phone() {
return cust_phone;
}
public void setCust_phone(String cust_phone) {
this.cust_phone = cust_phone;
}
public String getCust_mobile() {
return cust_mobile;
}
public void setCust_mobile(String cust_mobile) {
this.cust_mobile = cust_mobile;
}
}
- 创建映射
<?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.albertyy.crm.entity.Customer" table="cst_customer" >
<!-- 建立类中的属性与表中的主键对应 -->
<id name="cust_id" column="cust_id" >
<!-- 主键生成策略 -->
<generator class="native"/>
</id>
<!-- 建立类中的普通的属性和表的字段的对应 -->
<property name="cust_name" column="cust_name" />
<property name="cust_source" column="cust_source" />
<property name="cust_industry" column="cust_industry"/>
<property name="cust_level" column="cust_level"/>
<property name="cust_phone" column="cust_phone"/>
<property name="cust_mobile" column="cust_mobile"/>
</class>
</hibernate-mapping>
2.1.3创建Action
public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {
//模型驱动使用的对象
private Customer customer =new Customer();
@Override
public Customer getModel() {
// TODO Auto-generated method stub
return customer;
}
//注入Services
private CustomerService customerService;
public void setCustomerService(CustomerService customerService) {
this.customerService = customerService;
}
}
2.1.4创建Service
public class CustomerServiceImpl implements CustomerService {
//注入Dao
private CustomerDao customerDao;
public void setCustomerDao(CustomerDao customerDao) {
this.customerDao = customerDao;
}
}
2.1.5创建DAO
/**
*
* 项目名称:CRM
* 类名称:CustomerDaoImpl
* 类描述:客户管理Dao实现类
* 创建人:yangyangyang
* 创建时间:2018年12月16日 下午4:18:24
* 修改人:yangyangyang
* 修改时间:2018年12月16日 下午4:18:24
* 修改备注:
* @version
*
*/
public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {
}
2.1.6配置Action、Service、DAO
<!-- ======配置客户相关类======= -->
<!-- 配置Action -->
<bean id="customerAction" class="com.albertyy.crm.web.action.CustomerAction" scope="prototype">
<property name="customerService" ref="customerService"/>
</bean>
<!-- 配置Service -->
<bean id="customerService" class="com.albertyy.crm.serviceImpl.CustomerServiceImpl">
<property name="customerDao" ref="customerDao"/>
</bean>
<!-- 配置DAO -->
<bean id="customerDao" class="com.albertyy.crm.daoImpl.CustomerDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
2.2跳转到添加页面
2.2.1修改左侧菜单页面
<li><a htef="${pageContext.request.contextPath }/customer_saveUI.action">新增客户</a></li>
2.2.2编写Action中的saveUI的方法和添加页面
//客户管理跳转到添加页面
public String saveUI(){
return "saveUI";
}
2.2.3配置Action的跳转
<!-- 客户管理Action -->
<action name="customer_*" class="customerAction" method="{1}">
<result name="saveUI" >/customer/add.jsp</result>
</action>
2.2.4测试跳转
2.3引入数据字典
2.3.1什么是数据字典
数据字典用来规范某些地方具体值和数据
2.3.2创建数据字典表
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.3.3客户表和字典表的关系分析
2.3.4创建字典的实体和映射
- 创建实体
public class BaseDict {
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_sort;
private String dict_enable;
private String dict_memo;
public String getDict_id() {
return dict_id;
}
public void setDict_id(String dict_id) {
this.dict_id = dict_id;
}
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 String getDict_type_name() {
return dict_type_name;
}
public void setDict_type_name(String dict_type_name) {
this.dict_type_name = dict_type_name;
}
public String getDict_item_name() {
return dict_item_name;
}
public void setDict_item_name(String dict_item_name) {
this.dict_item_name = dict_item_name;
}
public String getDict_item_code() {
return dict_item_code;
}
public void setDict_item_code(String dict_item_code) {
this.dict_item_code = dict_item_code;
}
public String getDict_sort() {
return dict_sort;
}
public void setDict_sort(String dict_sort) {
this.dict_sort = dict_sort;
}
public String getDict_enable() {
return dict_enable;
}
public void setDict_enable(String dict_enable) {
this.dict_enable = dict_enable;
}
public String getDict_memo() {
return dict_memo;
}
public void setDict_memo(String dict_memo) {
this.dict_memo = dict_memo;
}
}
- 创建映射
<?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.albertyy.crm.entity.BaseDict" table="base_dict">
<id name="dict_id" column="dict_id">
<generator class="uuid"/>
</id>
<property name="dict_type_code" column="dict_type_code"/>
<property name="dict_type_name" column="dict_type_name"/>
<property name="dict_item_name" column="dict_item_name"/>
<property name="dict_item_code" column="dict_item_code"/>
<property name="dict_sort" column="dict_sort"/>
<property name="dict_enable" column="dict_enable"/>
<property name="dict_memo" column="dict_memo"/>
</class>
</hibernate-mapping>
2.3.5修改字典和客户的关系映射
- 修改了客户的实体
package com.albertyy.crm.entity;
/**
* 客户管理的实体
* @author yxy
*Create database ssh1;
Use ssh1;
CREATE TABLE `cst_customer` (
`cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)',
`cust_name` varchar(32) NOT NULL COMMENT '客户名称(公司名称)',
`cust_source` varchar(32) DEFAULT NULL COMMENT '客户信息来源',
`cust_industry` varchar(32) DEFAULT NULL COMMENT '客户所属行业',
`cust_level` varchar(32) DEFAULT NULL COMMENT '客户级别',
`cust_phone` varchar(64) DEFAULT NULL COMMENT '固定电话',
`cust_mobile` varchar(16) DEFAULT NULL COMMENT '移动电话',
PRIMARY KEY (`cust_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
*/
public class Customer {
private Long cust_id;
private String cust_name;
/*private String cust_source;
private String cust_industry;
private String cust_level;*/
private String cust_phone;
private String cust_mobile;
//客户和字典的关系是多对一,所以需要在多的一方,放一的一方的对象
private BaseDict baseDictSource;
private BaseDict baseDictIndustry;
private BaseDict baseDictLevel;
public BaseDict getBaseDictSource() {
return baseDictSource;
}
public void setBaseDictSource(BaseDict baseDictSource) {
this.baseDictSource = baseDictSource;
}
public BaseDict getBaseDictIndustry() {
return baseDictIndustry;
}
public void setBaseDictIndustry(BaseDict baseDictIndustry) {
this.baseDictIndustry = baseDictIndustry;
}
public BaseDict getBaseDictLevel() {
return baseDictLevel;
}
public void setBaseDictLevel(BaseDict baseDictLevel) {
this.baseDictLevel = baseDictLevel;
}
public Long getCust_id() {
return cust_id;
}
public void setCust_id(Long cust_id) {
this.cust_id = cust_id;
}
public String getCust_name() {
return cust_name;
}
public void setCust_name(String cust_name) {
this.cust_name = cust_name;
}
public String getCust_phone() {
return cust_phone;
}
public void setCust_phone(String cust_phone) {
this.cust_phone = cust_phone;
}
public String getCust_mobile() {
return cust_mobile;
}
public void setCust_mobile(String cust_mobile) {
this.cust_mobile = cust_mobile;
}
}
- 修改客户的映射
<?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.albertyy.crm.entity.Customer" table="cst_customer" >
<!-- 建立类中的属性与表中的主键对应 -->
<id name="cust_id" column="cust_id" >
<!-- 主键生成策略 -->
<generator class="native"/>
</id>
<!-- 建立类中的普通的属性和表的字段的对应 -->
<property name="cust_name" column="cust_name" />
<!-- <property name="cust_source" column="cust_source" />
<property name="cust_industry" column="cust_industry"/>
<property name="cust_level" column="cust_level"/> -->
<property name="cust_phone" column="cust_phone"/>
<property name="cust_mobile" column="cust_mobile"/>
<!-- 配置客户和字典多对一的关系 -->
<many-to-one name="baseDictSource" class="com.albertyy.crm.entity.BaseDict" column="cust_source"></many-to-one>
<many-to-one name="baseDictIndustry" class="com.albertyy.crm.entity.BaseDict" column="cust_industry"></many-to-one>
<many-to-one name="baseDictLevel" class="com.albertyy.crm.entity.BaseDict" column="cust_level"></many-to-one>
</class>
</hibernate-mapping>
2.3.6将映射文件交给Spring
<!-- 引入映射文件 -->
<property name="mappingResources">
<list>
<value>com/albertyy/crm/entity/User.hbm.xml</value>
<value>com/albertyy/crm/entity/Customer.hbm.xml</value>
<value>com/albertyy/crm/entity/BaseDict.hbm.xml</value>
</list>
</property>
2.4在添加页面上异步加载字典数据
2.4.1创建字典的Action、Service、DAO
- 编写DAO
/**
*
* 项目名称:CRM
* 类名称:BaseDictDaoImpl
* 类描述: 字典的Dao实现类
* 创建人:yangyangyang
* 创建时间:2018年12月16日 下午7:48:55
* 修改人:yangyangyang
* 修改时间:2018年12月16日 下午7:48:55
* 修改备注:
* @version
*
*/
public class BaseDictDaoImpl extends HibernateDaoSupport implements BaseDictDao {
}
- 编写Service
/**
*
* 项目名称:CRM
* 类名称:BaseDictServiceImpl
* 类描述: 字典业务层实现类
* 创建人:yangyangyang
* 创建时间:2018年12月16日 下午7:51:38
* 修改人:yangyangyang
* 修改时间:2018年12月16日 下午7:51:38
* 修改备注:
* @version
*
*/
public class BaseDictServiceImpl implements BaseDictServie {
//注入Dao
private BaseDictDao baseDictDao;
public void setBaseDictDao(BaseDictDao baseDictDao) {
this.baseDictDao = baseDictDao;
}
}
- 编写Action
/**
*
* 项目名称:CRM
* 类名称:BaseDictAction
* 类描述:字典的Action
* 创建人:yangyangyang
* 创建时间:2018年12月16日 下午7:54:21
* 修改人:yangyangyang
* 修改时间:2018年12月16日 下午7:54:21
* 修改备注:
* @version
*
*/
public class BaseDictAction extends ActionSupport implements ModelDriven<BaseDict> {
//模型驱动使用的对象
private BaseDict baseDict =new BaseDict();
@Override
public BaseDict getModel() {
// TODO Auto-generated method stub
return baseDict;
}
//注入Service
private BaseDictService baseDictService;
public void setBaseDictService(BaseDictService baseDictService) {
this.baseDictService = baseDictService;
}
}
2.4.2将字典类交给Spring管理
<!-- ======配置字典的相关类======= -->
<!-- 配置Action -->
<bean id="baseDictAction" class="com.albertyy.crm.web.action.BaseDictAction" scope="prototype">
<property name="baseDictService" ref="baseDictService"/>
</bean>
<!-- 配置Service -->
<bean id="baseDictService" class="com.albertyy.crm.serviceImpl.BaseDictServiceImpl">
<property name="baseDictDao" ref="baseDictDao"/>
</bean>
<!-- 配置DAO -->
<bean id="baseDictDao" class="com.albertyy.crm.daoImpl.BaseDictDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
2.4.3添加页面上引入jquery的js
<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery.min.js"></script>
2.4.4编写异步加载的方法
//页面加载,异步查询字典数据
//页面加载函数就会执行:
$(function(){
// 加载客户来源
$.post("${pageContext.request.contextPath }/baseDict_findByTypeCode.action",{"dict_type_code":"002"},function(data){
//遍历JOSN类型的数据
$(data).each(function(i,n){
$("#cust_source").append("<option value='"+n.dict_id+"'>"+n.dict_item_name+"</option>");
});
},"json");
});
2.4.5编写Action
package com.albertyy.crm.web.action;
import java.io.IOException;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.albertyy.crm.entity.BaseDict;
import com.albertyy.crm.service.BaseDictService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
/**
*
* 项目名称:CRM
* 类名称:BaseDictAction
* 类描述:字典的Action
* 创建人:yangyangyang
* 创建时间:2018年12月16日 下午7:54:21
* 修改人:yangyangyang
* 修改时间:2018年12月16日 下午7:54:21
* 修改备注:
* @version
*
*/
public class BaseDictAction extends ActionSupport implements ModelDriven<BaseDict> {
//模型驱动使用的对象
private BaseDict baseDict;
@Override
public BaseDict getModel() {
// TODO Auto-generated method stub
return baseDict;
}
//注入Service
private BaseDictService baseDictService;
public void setBaseDictService(BaseDictService baseDictService) {
this.baseDictService = baseDictService;
}
//根据类型名称查询字典
public String findByTypeCode(){
//调用业务层查询方法
List<BaseDict> list=baseDictService.findByTypeCode(baseDict.getDict_type_code());
//使用jsonlib将list转换成JSON(常用工具jsonlib fastjson)
/*
* JsonConfig:转JSON的配置对象
* JSONArray:将数组或者list集合转成JSON
* JSONObject:将对象或者map集合转成JSON
*/
JsonConfig jsonConfig=new JsonConfig();
jsonConfig.setExcludes(new String[]{"dict_sort","dict_enable","dict_memo"});
JSONArray jsonArray=JSONArray.fromObject(list,jsonConfig);
//将JSON数据传到页面
try {
ServletActionContext.getResponse().getWriter().println(jsonArray.toString());
} catch (IOException e) {
e.printStackTrace();
}
return NONE;
}
}
配置action
<!-- 字典管理Action -->
<action name="baseDict_*" class="baseDictAction" method="{1}">
</action>
2.4.6编写Service
public class BaseDictServiceImpl implements BaseDictService {
//注入Dao
private BaseDictDao baseDictDao;
public void setBaseDictDao(BaseDictDao baseDictDao) {
this.baseDictDao = baseDictDao;
}
//业务层根据类型查询
@Override
public List<BaseDict> findByTypeCode(String dict_type_code) {
return baseDictDao.findByTypeCode(dict_type_code);
}
}
2.4.7编写DAO
public class BaseDictDaoImpl extends HibernateDaoSupport implements BaseDictDao {
//根据类型编码查询字典数据
@Override
public List<BaseDict> findByTypeCode(String dict_type_code) {
return (List<BaseDict>) this.getHibernateTemplate().find("from BaseDict where dict_type_code=?", dict_type_code);
}
}
2.4.8编写其他字典项数据异步加载
//页面加载,异步查询字典数据
//页面加载函数就会执行:
$(function(){
// 加载客户来源
$.post("${pageContext.request.contextPath }/baseDict_findByTypeCode.action",{"dict_type_code":"002"},function(data){
//遍历JOSN类型的数据
$(data).each(function(i,n){
$("#cust_source").append("<option value='"+n.dict_id+"'>"+n.dict_item_name+"</option>");
});
},"json");
// 加载客户所属行业
$.post("${pageContext.request.contextPath }/baseDict_findByTypeCode.action",{"dict_type_code":"001"},function(data){
//遍历JOSN类型的数据
$(data).each(function(i,n){
$("#cust_industry").append("<option value='"+n.dict_id+"'>"+n.dict_item_name+"</option>");
});
},"json");
// 加载客户级别
$.post("${pageContext.request.contextPath }/baseDict_findByTypeCode.action",{"dict_type_code":"006"},function(data){
//遍历JOSN类型的数据
$(data).each(function(i,n){
$("#cust_level").append("<option value='"+n.dict_id+"'>"+n.dict_item_name+"</option>");
});
},"json");
});
2.5保存数据到数据库中
2.5.1修改添加页面
<form action="${pageContext.request.contextPath }/customer_save.action" method="post" class="jqtransform">
- 修改表单项名称:
<tr >
<td class="td_right">信息来源:</td>
<td class="">
<span class="fl">
<div class="select_border">
<div class="select_containers ">
<select name="baseDictSource.dict_id" id="cust_source" class="select">
<option>-----</option>
</select>
</div>
</div>
</span>
</td>
</tr>
<tr >
<td class="td_right">所属行业:</td>
<td class="">
<span class="fl">
<div class="select_border">
<div class="select_containers ">
<select name="baseDictIndustry.dict_id" id="cust_industry" class="select">
<option>-----</option>
</select>
</div>
</div>
</span>
</td>
</tr>
<tr >
<td class="td_right">客户级别:</td>
<td class="">
<span class="fl">
<div class="select_border">
<div class="select_containers ">
<select name="baseDictLevel.dict_id" id="cust_level" class="select">
<option>-----</option>
</select>
</div>
</div>
</span>
</td>
</tr>
2.5.2编写Action
//保存客户的方法
public String save(){
customerService.save(customer);
return NONE;
}
2.5.3编写Service
//保存客户
@Override
public void save(Customer customer) {
customerDao.save(customer);
}
2.5.4编写DAO
public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {
//保存客户
@Override
public void save(Customer customer) {
this.getHibernateTemplate().save(customer);
}
}
2.5.6业务层添加注解事务:@Transactional