`
taiyang.yu
  • 浏览: 18583 次
  • 性别: Icon_minigender_1
  • 来自: 西安
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

SSH模式下的DAO-service-Action

阅读更多
上一篇对SSH公用方法做了总结,这一章主要是在DAO层对上一章中公用方法的调用
/**
*DAO
*接口
*/
package crm.dao;

import hk.com.magenta.exception.DaoException;
import java.util.List;
import crm.entity.ContactInfo;


/**
* 联系人DAO
* @author mismyy
*
*/
public interface ContactInfoDao{

/*
* 添加
*/
public abstract void insertContactInfo(ContactInfo contactInfo) throws DaoException;

/*
* 更新
*/
public abstract void updateContactInfo(ContactInfo contactInfo) throws DaoException;

/*
*批量删除
**/
public Boolean deleteConts(String hql) throws DaoException;
/*
* 根据ID删除
*/
public abstract void deleteContactInfo(int id) throws DaoException;

/*
* *根据ID查询
*/
public abstract ContactInfo getContactInfo(int id) throws DaoException;
/*
* 根据HQL查询
*/
public abstract List<ContactInfo> queryContactInfo(String hql) throws DaoException;
//分页查询
public abstract List<ContactInfo> pageContactInfo(String hql,int size,int pagesize) throws DaoException;

//生成图表要用的方法
public List getPhotos(String sql) throws DaoException;

}

/**
*DAOIMPL
*接口的实现类
*/
package crm.dao.impl;

import hk.com.magenta.common.Common;
import hk.com.magenta.common.ICommon;
import hk.com.magenta.exception.CommonException;
import hk.com.magenta.exception.DaoException;
import hk.com.magenta.util.LogUtil;

import java.util.List;

/**
* Log(基本记录器)、LogFactory(负责创建Log实例)的工厂
* log4j的三大组建
* Logger日志写入器、Appender日志输入终端、Layout日志布局模式
*/

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import org.hibernate.Query;

import crm.dao.ContactInfoDao;
import crm.entity.ContactInfo;



/**
* Data access object (DAO) for domain model class ContactInfo.
*
* @see crm.entity.ContactInfo
* @author MyEclipse Persistence Tools
*/
/**
* 联系人DAO的实现
*/
public class ContactInfoDaoImpl extends Common implements ContactInfoDao {

//通过LogFactory创建一个日志记录器对象(用户记录实现ContactInfoDao的日志)
private static final Log log = LogFactory.getLog(ContactInfoDaoImpl.class);

protected void initDao() {
// do nothing
}
//日志写入器对象,
private static Logger logger = Logger.getLogger("dao");

//ICommon的接口对象,用于调用公用方法
private ICommon<ContactInfo> comm;
public void setComm(ICommon<ContactInfo> comm) {
this.comm = comm;
}



/**
* 根据主键(id)删除对象
* @author mismyy
* @param id
* @return void
*/
public void deleteContactInfo(int id) throws DaoException {

try {
//调用公用的删除方法
this.comm.deleteObject(ContactInfo.class,id);
} catch (CommonException e) {
e.printStackTrace();
logger.error(LogUtil.getLogStack(e));
throw new DaoException(e);
}
}

/**
* 根据主键(id)得到联系人信息
* @author mismyy
* @param id
* @return ContactInfo
*
*/
public ContactInfo getContactInfo(int id) throws DaoException {
try {
return this.comm.loadPojoByOid(ContactInfo.class, id);
} catch (CommonException e) {
e.printStackTrace();
logger.error(LogUtil.getLogStack(e));
throw new DaoException(e);
}
}

/**
* 添加联系人信息
* @author mismyy
* @param ContactInfo contactInfo
*
*/
public void insertContactInfo(ContactInfo contactInfo) throws DaoException {
try {
this.comm.insertObject(contactInfo);
} catch (CommonException e) {
e.printStackTrace();
logger.error(LogUtil.getLogStack(e));
throw new DaoException(e);
}
}

/**
* 分页得到联系人信息
* @author mismyy
* @param String hql,int size,int pagesize
* @return List<ContactInfo>
*
*/
public List<ContactInfo> pageContactInfo(String hql, int size, int pagesize)
throws DaoException {
try {
return this.comm.selectPageHQL(hql, size, pagesize);
} catch (CommonException e) {
e.printStackTrace();
logger.error(LogUtil.getLogStack(e));
throw new DaoException(e);
}
}

/**
* 根据查询语句(hql)得到联系人信息
* @author mismyy
* @param String hql
* @return List<ContactInfo>
*
*/
public List<ContactInfo> queryContactInfo(String hql) throws DaoException {
try {
return this.comm.queryHQL(hql);
} catch (CommonException e) {
e.printStackTrace();
logger.error(LogUtil.getLogStack(e));
throw new DaoException(e);
}
}

/**
* 更改联系人信息
* @author mismyy
* @param ContactInfo
*
*/
public void updateContactInfo(ContactInfo contactInfo) throws DaoException {

try {
this.comm.updateObject(contactInfo);
} catch (CommonException e) {
e.printStackTrace();
logger.error(LogUtil.getLogStack(e));
throw new DaoException(e);
}
}



/**
* 批量删除
*/
public Boolean deleteConts(String hql) throws DaoException {
Boolean flag= null;
try{
this.comm.excuteHQL(hql);
flag=true;
}catch(CommonException e){
e.printStackTrace();
logger.error(LogUtil.getLogStack(e));
flag=false;
}
return flag;
}
/**
* 生成图表要用的方法
*/
public List getPhotos(String sql) throws DaoException {
try{
Query query = this.getSession().createSQLQuery(sql);
return query.list();
}catch (Exception e) {
e.printStackTrace();
}
return null;
}


}
/************************Service*************************/
package crm.service;

import hk.com.magenta.exception.ServiceException;

import java.util.List;
import java.util.Map;
import crm.entity.ContactInfo;
public interface ContactInfoService {

/**
* 分页
* @param size
* @param pagesize
* @return
* @throws ServiceException
*/
public String pageQueryContactInfoAll( int size,int pagesize,String queryStr) throws ServiceException;
/**
* 添加
* @param contactInfo
* @return
* @throws ServiceException
*/
public Boolean insertContactInfo(ContactInfo contactInfo) throws ServiceException;
/**
* 更改
* @param contactInfo
* @return
* @throws ServiceException
*/
public void updateContactInfo(ContactInfo contactInfo) throws ServiceException;
/**
* 批量删除
* @param deleteId
* @return
* @throws ServiceException
*/
public Boolean deleteConts(String deleteId) throws ServiceException;
/**
* 根据hql查询
* @return
* @throws ServiceException
*/
public String queryContactInfo(String cusName) throws ServiceException;
/**
* 根据id得到联系人信息
* @param id
* @return
* @throws ServiceException
*/
public String getContactInfo(String id)throws ServiceException;
//public ContactInfo getContactInfo(String id)throws ServiceException;
/**
* 修改联系人信息
* @param custMap
* @param id
* @return
* @throws ServiceException
*/
public String updateContactInfo(Map custMap,String id)throws ServiceException;

/**
*
* 根据联系人编号,添加联系人照片
* @param conId
* @return
* @throws ServiceException
*/
public Boolean upContById(int conId,String images) throws ServiceException;
/**
* 根据编号得到照片
* @param conId
* @return
* @throws ServiceException
*/
public String getContById(String conId) throws ServiceException;

public String getImages(String conId) throws ServiceException;


/**
* 生成图表要用到的方法
* @param param
* @return
* @throws ServiceException
*/
public List getPhotos(String param) throws ServiceException;
}
/************************ServiceImpl*************************/
package crm.service.impl;

import hk.com.magenta.exception.DaoException;
import hk.com.magenta.exception.ServiceException;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;

import crm.common.DateConvert;
import crm.dao.ContactInfoDao;
import crm.entity.ContactInfo;
import crm.entity.Customer;
import crm.service.ContactInfoService;

public class ContactInfoServiceImpl implements ContactInfoService {

//联系人接口,用于注入
private ContactInfoDao contactInfoDao=null;
public void setContactInfoDao(ContactInfoDao contactInfoDao) {
this.contactInfoDao = contactInfoDao;
}
private Boolean flag=null;
DateConvert dc=new DateConvert();//实例公用包中的日期处理对象
/**
* 保存联系人信息
*/
public Boolean insertContactInfo(ContactInfo contactInfo)
throws ServiceException {
try {
this.contactInfoDao.insertContactInfo(contactInfo);
flag=true;
} catch (DaoException e) {
e.printStackTrace();
flag=false;
}
return flag;
}
/**
* 分页方法
*/
public String pageQueryContactInfoAll( int size,int pagesize,String queryStr)
throws ServiceException{
String json="{";
String hql="from ContactInfo where isdelete='N' ";
if(!StringUtils.isBlank(queryStr)){
hql=hql+queryStr;
}
//定义一个数组集合,用于存放ContactInfo对象
List<ContactInfo> lists=new ArrayList<ContactInfo>();
try {
//查询所有信息
List list=contactInfoDao.queryContactInfo(hql);
//分页
lists=contactInfoDao.pageContactInfo(hql, size, pagesize);
//
json = json + "results:'"+list.size()+"',rows:[";
for(int i=0;i<lists.size();i++){
ContactInfo cont=(ContactInfo)lists.get(i);
   json=json+"{conId:'"+cont.getConId()
+"',conName:'"+cont.getConName()
+"',sex:'"+cont.getSex()
+"',cusName:'"+cont.getCusName()
+"',contType:'"+cont.getContType()
+"',occupation:'"+cont.getOccupation()
+"',appointment:'"+cont.getAppointment()
+"',telephone:'"+cont.getTelphone()
+"',phone:'"+cont.getPhone()
+"',fax:'"+cont.getFax()
+"',email:'"+cont.getEmail()
+"'}";
if(i==lists.size()-1){
}else{
json=json+",";
}
}
} catch (DaoException e) {
e.printStackTrace();
}
json+="]}";
return json;
}
/**
* 更改联系人信息
*/
public void updateContactInfo(ContactInfo contactInfo)
throws ServiceException {
try {
String image=this.getContById(contactInfo.getConId().toString());
contactInfo.setImages(image);
contactInfoDao.updateContactInfo(contactInfo);
} catch (DaoException e) {
e.printStackTrace();
}
}


/**
* 批量删除的方法
*/
public Boolean deleteConts(String deleteId) throws ServiceException {
String hql="update contactInfo set isdelete='Y' where con_Id in ("+deleteId+")";
try {
flag=contactInfoDao.deleteConts(hql);
} catch (DaoException e) {
e.printStackTrace();
}
return flag;
}

/**
* 查询所有信息
* @return
* @throws ServiceException
*/
public String queryContactInfo(String cusName) throws ServiceException {
String json="{";
String hql="from ContactInfo where isdelete='N' and cus_name='"+cusName+"'";

List<ContactInfo> list=new ArrayList<ContactInfo>();
try {
list= contactInfoDao.queryContactInfo(hql);
json = json + "results:'"+list.size()+"',rows:[";
for(int i=0;i<list.size();i++){
ContactInfo cont=(ContactInfo)list.get(i);
json = json + "{id:'"+cont.getConId()
+"',value:'"+cont.getConName()
+"',remark:'"+cont.getCusName()
+"'}";
if(i==list.size()-1){
}else{
json=json+",";
}
}
} catch (DaoException e) {
e.printStackTrace();
}
json+="]}";
return json;
}
/**
* 根据联系人id查询
*/
public String getContactInfo(String id) throws ServiceException {
ContactInfo cont=null;
Integer conId=Integer.parseInt(id);
try {
cont= contactInfoDao.getContactInfo(conId);
//String filePath="";//给图片一个相对路径

String json = "{";
json = json + "success:'true',rows:[";
json = json + "{conId:'"+cont.getConId()
+"',conName:'"+cont.getConName()
+"',cusName:'"+cont.getCusName()
+"',brank1:'"+cont.getBrank1()
+"',jiguan:'"+cont.getJiguan()
+"',nation:'"+cont.getNation()
+"',address:'"+cont.getAddress()
+"',city:'"+cont.getCity()
+"',professionTitle:'"+cont.getProfessionTitle()
+"',appointment:'"+cont.getAppointment()
+"',occupation:'"+cont.getOccupation()
+"',contType:'"+cont.getContType()
+"',sex:'"+cont.getSex()
+"',birthday:'"+dc.DateToString(cont.getBirthday())
+"',images:'"+cont.getImages()
+"',pcard:'"+cont.getPcard()
+"',email:'"+cont.getEmail()
+"',zhuye:'"+cont.getZhuye()
+"',interests:'"+cont.getInterests()
+"',qq:'"+cont.getQq()
+"',marryname:'"+cont.getMarryname()
+"',telphone:'"+cont.getTelphone()
+"',phone:'"+cont.getPhone()
+"',fax:'"+cont.getFax()
+"',zcode:'"+cont.getZcode()
+"',yearMoney:'"+cont.getYearMoney()
+"',firstDate:'"+dc.DateToString(cont.getFirstDate())
+"',zjDatev:'"+dc.DateToString(cont.getZjDatev())
+"',brankId:'"+cont.getBrankId()

+"',remark:'"+cont.getRemark()
+"',writtdate:'"+dc.DateToString(cont.getWrittdate())
+"',writtmen:'"+cont.getWrittmen()
+"',information:'"+cont.getInformation()
+"',isdelete:'"+cont.getIsdelete()
+"',locking:'"+cont.getLocking()
+"',grading:'"+cont.getGrading()
+"',cusId:'"+cont.getCusId()
+"'}";
json = json +",";
json = json +"]}";
return json;
} catch (DaoException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* 根据id找到的照片信息
*/
public String getImages(String id) throws ServiceException {
ContactInfo cont=null;
Integer conId=Integer.parseInt(id);
try {
cont= contactInfoDao.getContactInfo(conId);
String json="{";
if(cont==null){
  json=json+"results:0,rows:[0]}";
  return json;
}
String image=cont.getImages();
System.out.println("根据ID找到的照片信息"+image);
if(image.equals("aaa")){
  json=json+"results:0,rows:[0]}";
return json;
}
String[]a=image.split(",");
List<String> b=new ArrayList<String>();
for(int i=0;i<a.length;i++){
//System.out.println("这个是字符数组s的内容"+str+a[i]);
b.add( i,a[i]);
}
json = json + "results:'"+b.size()+"',rows:[";
for(int j=0;j<b.size();j++){
//System.out.println("这个是字符数组b的内容"+b.get(j));
json = json + "{id:'"+b.get(j)
+"',value:'"+b.get(j)
+"',remark:'"+b.get(j)
+"'}";
if(j==b.size()-1){
}else{
json=json+",";
}
}
json+="]}";
return json;
} catch (DaoException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

/**
* 修改联系人信息
*/
public String updateContactInfo(Map custMap, String id)
throws ServiceException {

String json="{success:";
Boolean flag=null;
ContactInfo contactInfo=null;
try{
contactInfo=this.contactInfoDao.getContactInfo(Integer.parseInt(id));
}catch(DaoException e){
e.printStackTrace();
}
try{
for(Object key: custMap.keySet()){
String[] temp=(String[])custMap.get(key);

if(key.equals("birthday")||key.equals("firstDate")||key.equals("zjDatev")||key.equals("writtdate")){
System.out.println("key="+key+"value="+temp[0]);
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
Date tempDate=format.parse(temp[0]);
BeanUtils.copyProperty(contactInfo, ""+key,tempDate);
}else{
BeanUtils.copyProperty(contactInfo, ""+key, temp[0]);
}
}
this.contactInfoDao.updateContactInfo(contactInfo);
flag=true;
}catch(Exception e){
e.printStackTrace();
flag=false;
}
json+=flag+"}";
return json;
}
/**
* 上传照片
*/
public Boolean upContById(int conId,String images) throws ServiceException {
String hql="update contactInfo set images='"+images+"' where con_Id='"+conId+"'";
try {
contactInfoDao.deleteConts(hql);
flag=true;
} catch (DaoException e) {
flag=false;
e.printStackTrace();
}
return flag;
}
public String getContById(String conId) throws ServiceException {
String hql="select images from contInfo where conId="+conId;
ContactInfo con=null;
try {
con=contactInfoDao.getContactInfo(Integer.parseInt(conId));
} catch (DaoException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con.getImages();
}
/**
* 根据参数得到相应的list集合
*/
public List getPhotos(String param) throws ServiceException {
String sql="select "+param+" as tex1,count("+param+") as tex2 from contactInfo where isdelete='N' group by "+param+"";
try {
return this.contactInfoDao.getPhotos(sql);
} catch (DaoException e) {
e.printStackTrace();
}
return null;
}
}

/***********************Action*******************************/
package crm.web.action;

import hk.com.magenta.exception.ServiceException;



import javax.servlet.ServletConfig;

import java.awt.Color;
import java.awt.Font;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import crm.service.ContactInfoService;
import crm.service.ConfigInfoService;
import crm.service.impl.ContactInfoServiceImpl;
import crm.common.DateConvert;
import crm.entity.ContactInfo;

import org.apache.commons.beanutils.*;
import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.lang.StringUtils;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;

import com.jspsmart.upload.SmartUpload;
import java.io.*;

/**
* 联系人信息管理
* @author mismyy
*
*/
public class ContactInfoAction extends DispatchAction {
DateConvert dc=new DateConvert();

private final String METHOD="method";
private final String SET="set";

private ContactInfo contactInfo=null;//联系人对象
private ContactInfoService contactInfoService=null;//联系人业务接口对象
public void setContactInfoService(ContactInfoService contactInfoService) {
this.contactInfoService = contactInfoService;
}
/**
* 联系人信息查找方法(带分页)
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward pageQueryContactInfoAll(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//定义当前页,和每页显示的条数
try {
String queryParams="";
String content=null;
if(request.getParameter("contconName")!=null){
content=request.getParameter("contconName").trim();
}
if(content!=null){
queryParams="and cusName like '%"+content+"%' or sex like '%"+content
+"%' or conName like '%"+content+"%' or contType like '%"+content
+"%' or occupation like '%"+content+"%' or appointment like '%"+
content+"%' or email like '%"+content+"%' or remark like '%"+
content+"%' group by conId having isdelete='N'";
}else{
queryParams="and 1=1";
}
int start=0;
if(request.getParameter("start")!=null){
start=Integer.parseInt(request.getParameter("start"));
}
int limit=0;
if(request.getParameter("limit")!=null){
limit=Integer.parseInt(request.getParameter("limit"));
}
String json=contactInfoService.pageQueryContactInfoAll(start, limit,queryParams);
//设置响应类型
response.setCharacterEncoding("UTF-8");
//将业务方法查找回来的字符串写回客户端
response.getWriter().write(json);
} catch (ServiceException e) {
e.printStackTrace();
}catch(Exception ex){
ex.printStackTrace();
}
return null;
}

/**
* 添加联系人信息方法
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward saveContactInfo(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
Enumeration<String> enumation=request.getParameterNames();//获取页面提交的所有属性
ContactInfo cont=new ContactInfo();
Class custClazz=cont.getClass();
for(Enumeration<String> e =enumation; e.hasMoreElements();)
{
String property = e.nextElement();     //获得页面上传的所有属性
    String firstLetter = property.substring(0, 1).toUpperCase(); //javaBean 规定首字母大写
    String setMethodName = SET + firstLetter + property.substring(1); // 获得和属性对应的setXxx()方法 
    if (!property.equals(METHOD)) { //method 是 struts 用的
String propertyValue = request.getParameter(property); //从request 获取property 的值
if (!StringUtils.isBlank(propertyValue) && !propertyValue.equals("请选择...")) { //过滤空值 和 无效的下拉框值
System.out.println(property + " : "+ propertyValue);
try {
Field clazzField = custClazz.getDeclaredField(property); //获取对象的属性 主要是为了 反射时候调用属性的type
Method method = custClazz.getMethod(setMethodName,clazzField.getType());//获取方法. 这里可以看出"方法重载"只与 方法名和参数有关 而与返回值无关 ps:第二个参数为可选数组 因为我们调用的是set方法所以不用class[] 数组了
if (clazzField.getType() == java.util.Date.class) {//如果是method的 形参是date,Float,Integer,Double 需要转换 这里只用到了date与Integer 如果需要 可参照Integer 写一个if ps:另外一种解决方案就是 为class 对象都提供一个形参为string 的set方法
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date temp = format.parse(propertyValue);
method.invoke(cont,temp);
}else if(clazzField.getType() == java.lang.Integer.class){
Integer temp =Integer.parseInt(propertyValue);
method.invoke(cont,temp);
}else{
method.invoke(cont,propertyValue);//调用对象的方法 第二个为 实参
}
}  catch (Exception e1) {
e1.printStackTrace();
}//end try
}//end propertyValue
}//end property
}//#注入完毕

Boolean flag=null;
String json="{success:";
try{/*
cont.setCusName(request.getParameter("conName"));
cont.setCusName(request.getParameter("cusName"));
cont.setSex(request.getParameter("sex"));
cont.setContType(request.getParameter("contType"));
cont.setNation(request.getParameter("nation"));
cont.setOccupation(request.getParameter("occupation"));
cont.setProfessionTitle(request.getParameter("professionTitle"));
cont.setAppointment(request.getParameter("appointment"));
cont.setTelphone(request.getParameter("telphone"));
cont.setPhone(request.getParameter("phone"));
cont.setEmail(request.getParameter("email"));
cont.setBirthday(dc.StringToDate(request.getParameter("birthday")));
cont.setCity(request.getParameter("city"));
cont.setZcode(request.getParameter("zcode"));
cont.setAddress(request.getParameter("address"));
cont.setInterests(request.getParameter("interests"));
cont.setFirstDate(dc.StringToDate(request.getParameter("firstDate")));
cont.setZjDatev(dc.StringToDate(request.getParameter("zjDatev")));
cont.setYearMoney(request.getParameter("yearMoney"));
cont.setJiguan(request.getParameter("jiguan"));
cont.setMarryname(request.getParameter("marryname"));
cont.setFax(request.getParameter("fax"));
cont.setZhuye(request.getParameter("zhuye"));
cont.setBrank1(request.getParameter("brank1"));
cont.setBrankId(request.getParameter("brankId"));
cont.setQq(request.getParameter("qq"));
cont.setWrittdate(dc.StringToDate(request.getParameter("writtdate")));
cont.setWrittmen(request.getParameter("writtmen"));
cont.setRemark(request.getParameter("remark"));
*/
cont.setImages("aaa");
cont.setIsdelete("N");
//手动得到custome的编号
cont.setCusId(1);
cont.setGrading("a");
cont.setInformation("a");
cont.setLocking("N");
cont.setPcard("132155");
/*System.out.println("-----------------------------------");
System.out.println(cont.getAddress());
System.out.println(cont.getAppointment());
System.out.println(cont.getBrank1());
System.out.println(cont.getBrankId());
System.out.println(dc.DateToString(cont.getBirthday()));
System.out.println(cont.getCity());
System.out.println(cont.getConName());
System.out.println(cont.getContType());
System.out.println(cont.getCusName());
System.out.println(cont.getEmail());
System.out.println(dc.DateToString(cont.getFirstDate()));
System.out.println(cont.getFax());
System.out.println(cont.getGrading());
System.out.println(cont.getImages());
System.out.println(cont.getInformation());
System.out.println(cont.getInterests());
System.out.println(cont.getIsdelete());
System.out.println(cont.getJiguan());
System.out.println(cont.getLocking());
System.out.println(cont.getMarryname());
System.out.println(cont.getNation());
System.out.println(cont.getOccupation());
System.out.println(cont.getPcard());
System.out.println(cont.getPhone());
System.out.println(cont.getProfessionTitle());
System.out.println(cont.getQq());
System.out.println(cont.getRemark());
System.out.println(cont.getSex());
System.out.println(cont.getTelphone());
System.out.println(cont.getWrittmen());
System.out.println(dc.DateToString(cont.getWrittdate()));
System.out.println(cont.getYearMoney());
System.out.println(cont.getZcode());
System.out.println(cont.getZhuye());
System.out.println(dc.DateToString(cont.getZjDatev()));
System.out.println("-----------------------------------");*/

flag=contactInfoService.insertContactInfo(cont);
json+=flag+"}";
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);

}catch(Exception e){
e.printStackTrace();
}
return null;
}

/**
* 根据联系人id得到联系人信息
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward getContactInfo(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

//获得联系人编号
String conId=request.getParameter("id");
//给imageID一个联系人参数
if(StringUtils.isBlank(conId)) {
return null;
}
try {
String str =  contactInfoService.getContactInfo(conId);
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.print(str);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 更改方法
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward updateContactInfo(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

String id=request.getParameter("conId");
Map requestMap=request.getParameterMap();
String json="";
try{
json=this.contactInfoService.updateContactInfo(requestMap,id);
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);
}catch(Exception e){
e.printStackTrace();
}
return null;
}
/**
* 删除联系人方法
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward deleteContactInfo(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

String deleteId="";
if(request.getParameter("deleteId")!=null){
deleteId=request.getParameter("deleteId");
}
System.out.println("联系人的Action中取得的值"+deleteId);
Boolean flag=null;
String json="{success:";
try{
flag=contactInfoService.deleteConts(deleteId);
json+=flag+"}";
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);
}catch(Exception e){
e.printStackTrace();
}
return null;
}

/**
* 交流信息下拉列表
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/

public ActionForward queryContactInfo(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String json="";
String cusName=request.getParameter("cusName");
try{
json=contactInfoService.queryContactInfo(cusName);
}catch(Exception e){
e.printStackTrace();
}
//设置响应类型
response.setCharacterEncoding("UTF-8");
//将业务方法查找回来的字符串写回客户端
response.getWriter().write(json);
return null;
}
/**
* 文件上传
*/

public ActionForward testUploadFile(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
Boolean flag=null;
StringBuffer fileNames=new StringBuffer();
try{

//得到站点的绝对路径
String getpath=request.getSession().getServletContext().getRealPath("/");
String getPath1=request.getSession().getServletContext().getRealPath("");
//用于存放图片的主文件和临时文件
String uploadPath=getpath+"crm\\uploadtest\\";
String tempPath=getpath+"crm\\tmp\\";
System.out.println("uploadPath:"+uploadPath);
System.out.println("tempPath:"+tempPath);

//设置文件上传的尺寸和缓存区大小
DiskFileUpload fu=new DiskFileUpload();
fu.setSizeMax(4194304);
fu.setSizeThreshold(4096);
fu.setRepositoryPath(tempPath);
//得到所有文件
List fileItems=fu.parseRequest(request);
//编辑得到文件
Iterator i=fileItems.iterator();
//一次处理每个文件
while(i.hasNext()){
FileItem fi=(FileItem)i.next();
//获取文件名称
String fileName=fi.getName();
//System.out.println(fileName);
if(fileName!=null){
// 在这里可以记录用户和文件信息 
                    // 此处应该定义一个接口(CallBack),用于处理后事。 
                    // 写入文件a.txt,你也可以从fileName中提取文件名: 

String extfile=fileName.substring(fileName.indexOf("."));
Timestamp now=new Timestamp((new java.util.Date()).getTime());

SimpleDateFormat fmt=new SimpleDateFormat("yyMMddHHmmssSS");
String pfileName=fmt.format(now).toString().trim();

System.out.println(uploadPath+"MG"+pfileName+extfile);
fi.write(new File(uploadPath+"MG"+pfileName+extfile));
fileNames.append(uploadPath+"MG"+pfileName+extfile+";");

}
}
flag=true;
}catch(Exception e){
e.printStackTrace();
flag=false;
}
System.out.println("文件上传成功true:"+flag);
System.out.println("文件名称:"+fileNames);

String file=fileNames.substring(fileNames.length()-22, fileNames.length()-1);
System.out.println("file的值为:"+file);

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

String json ="";
if(flag==true){
try{
json= "{'success':true,'message':'上传成功','filePaths':'"+file+"'}";
//response.getWriter().print(json);
//response.getWriter().print("{'success':true,'message':'上传成功'}");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.print(json);
}catch(IOException e){
e.printStackTrace();
}
}else{
try{
json="{'success':flase,'message':'失败'}";
//response.getWriter().print(json);
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.print(json);
}catch(IOException e){
e.printStackTrace();
}
}
System.out.println(json);
return null;
}
public ActionForward fileUpload(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//得到联系人编号和保存成功的照片路径
String conId=request.getParameter("conId");
String images=request.getParameter("images");
System.out.println("conID:"+conId);
System.out.print("images:"+images);
if(StringUtils.isBlank(conId)&&StringUtils.isBlank(images)){
return null;
}

Boolean flag=null;
//根据联系人编号找到相关联系人的照片,如果存在照片信息,就将页面传过来的照片信息追加到已有照片信息的后面,如果没有就直接添加
String image=contactInfoService.getContById(conId);
if(image.equals("aaa")||image==null){
image=images;
}else{
image=image+images;
}
String json="{success:";
try{
flag=contactInfoService.upContById(Integer.parseInt(conId), image);
json+=flag+"}";
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);
}catch(Exception e){
flag=false;
e.printStackTrace();
}
return null;
}
/**
* 获取图片的下拉列表
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward getImages(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String conId="0";
if(request.getParameter("imgId")!=null){
conId=request.getParameter("imgId");
}
try {
String str =contactInfoService.getImages(conId);
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.print(str);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 生成图片
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward createPie(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String rdName=request.getParameter("radioName");
HttpSession session=request.getSession();
int td=-1;
if(rdName!=null){
td=Integer.parseInt(rdName);
}
String radioString="";
String paramString="";
System.out.println(rdName);
switch (td) {
case 0:
radioString="联系人类型";//(cont_Type)
paramString="cont_Type";
break;
case 1:
radioString="公司";
paramString="cus_name";
break;
case 2:
radioString="职业";
paramString="profession_Title";
break;
case 3:
radioString="职位";
paramString="occupation";
break;
case 4:
radioString="职称";
paramString="appointment";
break;
case 5:
radioString="城市";//city
paramString="city";
break;
case 6:
radioString="性别";//sex
paramString="sex";
break;
default:
break;
}
List list=contactInfoService.getPhotos(paramString);
DefaultPieDataset datesetade=new DefaultPieDataset();
for(int i=0;i<list.size();i++){
Object[] a=(Object[])list.get(i);
datesetade.setValue(a[0].toString(),Integer.parseInt(a[1].toString()));
}
String fileName="";
PieDataset dataset=datesetade;
//创建JFreeChart对象
JFreeChart jfreechart=ChartFactory.createPieChart3D("", dataset, true, true, false);

//ChartFrame pieFrame=new ChartFrame("水果产量图",jfreechart);
//jfreechart.getTitle().setFont(new Font("黑体",Font.BOLD,30));//设置标题字体
PiePlot piePlot=(PiePlot)jfreechart.getPlot();//获取图表区域的对象
piePlot.setLabelFont(new Font("黑体",Font.BOLD,15));
piePlot.setNoDataMessage("No data available");
piePlot.setCircular(true);
piePlot.setLabelGap(0.01D);//间距

jfreechart.getLegend().setItemFont(new Font("黑体",Font.BOLD,15));//
jfreechart.setBackgroundPaint(Color.pink);
//得到jfreetChart的保持路径;
String ft=request.getContextPath();
FileOutputStream out=new FileOutputStream(ft);
PrintWriter pw=new PrintWriter(out);

ChartRenderingInfo info=new ChartRenderingInfo(new StandardEntityCollection());
try {
fileName=ServletUtilities.saveChartAsPNG(jfreechart, 900,540, info,session);
//使用printWriter将文件写出
ChartUtilities.writeImageMap(pw, fileName, info, true);
pw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//取得生成图片的路径
String json=request.getContextPath() + "/servlet/DisplayChart?filename="+fileName;
response.setCharacterEncoding("UTF-8");
response.getWriter().print(json);
return null;
}
}
分享到:
评论

相关推荐

    ssh.rar_Action!_service dao_ssh action_ssh com.dao_ssh dao

    基于ssh三层架构的JAVA开发简单示例,包含service,dao,action,pojo

    ssh分页(.dao,.daoImp,.service,.serviceImp,.action,.jsp,applicationContext.xml)

    ssh分页注释加说明借鉴整理的 ssh分页(.dao,.daoImp,.service,.serviceImp,.action,.jsp,applicationContext.xml

    SSH实现简单的后台管理系统

    后台与mysql数据库进行连接,dao-service-action都是一一对应,便于理解。此后台管理系统,一共有三个表,用户注册表,部门表以及员工表。主要功能有用户的登录与注册,部门的增查,员工的增删改查。还采用了session...

    SSH代码生成工具 SSH代码生成器

    验证框架--&gt; bean-validation.xml (实体验证)、SaveAction-validation.xml (保存验证)、UpdateAction-validation.xml (修改验证) 日志管理配置 --&gt; log4j.xml 国际化配置--&gt;globalMessages_zh.properties(中文) ...

    AutoCode代码生成器【SSH版】

    Action--&gt; 业务Action.java和导出Excel的Action等 VO --&gt; bean.java及 bean.hbm.xml配置文件 Service --&gt; Service接口定义类 DAO --&gt; DAO接口定义类 DAOImpl --&gt; DAO接口Implements实现类 程序配置 --&gt; web....

    ssh代码生成器轻松、快捷

    验证框架--&gt; bean-validation.xml (实体验证)、SaveAction-validation.xml (保存验证)、UpdateAction-validation.xml (修改验证) 日志管理配置 --&gt; log4j.xml 国际化配置-&gt;globalMessages_zh.properties(中文) ...

    基于SSH模拟当当网项目(电子商务平台)

    a.findCategory.action--&gt;CategoryAction--&gt;CategoryDAO 改造成CategoryAction--&gt;MainService--&gt;CategoryDAO b.重构CategoryDAO c.提取MainService d.改造CategoryAction 4)类别浏览界面 a.booklist.action--&gt;...

    AutoCode代码生成器(SSH版)

    验证框架--&gt; bean-validation.xml (实体验证)、SaveAction-validation.xml (保存验证)、UpdateAction-validation.xml (修改验证) 日志管理配置 --&gt; log4j.xml 国际化配置--&gt;globalMessages_zh.properties(中文) ...

    ssh框架模板

    ssh框架模板,框架已经搭建好,只需套用。action-service-serviceImpl-dao-daoImpl,pojo中是实体配置文件和实体类。

    通用SSH2结构的标准代码,通用dao实现类serviceImpl实现类

    通用SSH2结构的增删查改标准代码, bean,HBM文件为自定义, service,dao接口为标准可重用 ...action 调用通用service,service调用通用dao 调用关系用spring 配址文件设置 因为上传限制问题,把lib目录下的jar文件都删掉了

    SSH整合案例-商品管理.rar

    1. 公共类搭建 1.1 po对象及其映射 1.2util包工具类 ...3.7.编辑applicationContext.xml, dao注入sessionFactory,service注入dao,action注入service。实现Spring IoC配置 4 各模块具体实现 4.1 登录模块

    在一个ssh框架的项目当中,页面、action、service、model、dao1

    jsp传参数给action action调用service service调用dao 他们相互调用的时候传递的参数就是modelStruts负责前台与后台之间数

    一个非常简单的SSH整合

    struts2+hibernate4+spring3的整合 步骤:a. 创建数据库表 userinfo b. 创建web工程,导入... action --&gt; service --&gt; dao --&gt; db e.配置spring 的applicationContext.xml f.配置struts.xml g.编写页面

    ssh框架下代码自动生成

    ssh框架下代码自动生成,代码(dao、service、serviceImpl、action)、页面

    spring_day01_all.zip_Action!_ioc mechanism_service调用dao

    在SSH整合开发中,利用Spring管理Service、DAO等组件,利用IoC机制实现Action和Service,Service和DAO之间低耦合调用。利用AoP机制实现事务管理、以及共通功能的切入等。 功能是整合,好处是解耦。

    一个最简单的SSH框架

    在 struts-config.xml 中定义 &lt;action-mapping&gt;, ActionServlet 会加载。 2 ) spring 负责业务层管理,即 Service (或 Manager). 1 . service 为 action 提供统计的调用接口,封装持久层的 DAO. 2 .可以写...

    ssh框架搭建实例源码4

    基础dao,service,action操作的封装;struts2通配符;div布局框架;log4j;ssh流程。 留意: target="_self"&gt;改成target="MainBody"&gt;便可以真正使用div布局。 本资源是在“ssh框架搭建实例源码3”基础上添加struts2...

    java底层代码:泛型DAO+SSH+Proxool连接池+国际化语言

    2、采用Jsp-&gt;Action-&gt;Service-&gt;Dao的编码方式,封装了HibernateUtil、SpringUtil、HqlUtil等工具,以及简化了增删查改操作。 3、此底层包含泛型DAO、Proxool连接池、国际化语言、DateUtil工具、ExcelUtil报表工具、...

    SSH自动生成代码工具加MySQL数据库

    dao domains service strut serviceImpls utils action applicationContext.xml Log4j.properties struts-config.xml 非常好用的SSH框架代码生成工具,只要输入数据库表名 数据库密码 数据库用户名。就可以生成SSH...

Global site tag (gtag.js) - Google Analytics