| package com.web.action; import java.util.List; import javax.annotation.Resource; import org.apache.commons.lang3.StringUtils; import org.apache.struts2.ServletActionContext; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Namespace; import org.apache.struts2.convention.annotation.ParentPackage; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.Results; import org.hibernate.criterion.DetachedCriteria; import org.hibernate.criterion.Restrictions; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import com.bean.BaseDict; import com.bean.Customer; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; import com.opensymphony.xwork2.util.ValueStack; import com.service.CustomerService; import com.web.commons.Page; @Controller("customerAction") @Scope("prototype") @ParentPackage("struts-default") @Namespace("/customer") @Results({ @Result(name = "addUI", type = "dispatcher", location = "/jsp/customer/add.jsp"), @Result(name = "listCustomer", type = "dispatcher", location = "/jsp/customer/list.jsp"), @Result(name = "allCustomer", type = "redirectAction", location = "findAllCustomer"), @Result(name = "editUI", type = "dispatcher", location = "/jsp/customer/edit.jsp"), @Result(name = "industryCount", type = "dispatcher", location = "/jsp/statistic/indlist.jsp"), @Result(name = "sourceCount", type = "dispatcher", location = "/jsp/statistic/soulist.jsp") }) public class CustomerAction extends ActionSupport implements ModelDriven<Customer> { private List list; private Integer curNum; private Page<Customer> page; private List<BaseDict> customerSources; private List<BaseDict> customerLevels; @Resource(name = "customerService") private CustomerService customerService; private Customer customer = new Customer(); public void setCustomer(Customer customer) { this.customer = customer; } @Override public Customer getModel() { return customer; } @Action("customerIndustryCount") public String customerIndustryCount() { list=customerService.customerIndustryCount(); return "industryCount"; } @Action("customerSourceCount") public String customerSourceCount() { list=customerService.findCustomerSourceCount(); return "sourceCount"; } @Action("editCustomer") public String editCustomer() { customerService.updateCustomer(customer); return "allCustomer"; } @Action("editUICustomer") public String editUICustomer() { // 查询获取客户来源 customerSources = customerService.findAllCustomerSource(); // 查询所有客户级别 customerLevels = customerService.findAllCustomerLevel(); //此处自己定义对象是为了避免和customer对象造成混淆,模型中的customer和查出获得到的两个customer不是同一个对象,所以如果直接压栈,将会是空 Customer c=customerService.findByIdCustomer(customer.getCust_id()); //把获取到的对象压入值栈 ActionContext.getContext().getValueStack().push(c); return "editUI"; } @Action("removeCustomer") public String removeCustomer() { customerService.removeByIdCustomer(customer.getCust_id()); return "allCustomer"; } @Action("addUICustomer") public String addUICustomer() { // 查询获取客户来源 customerSources = customerService.findAllCustomerSource(); // 查询所有客户级别 customerLevels = customerService.findAllCustomerLevel(); return "addUI"; } @Action("findAllCustomer") public String findAllCustomer() { //1.创建离线查询东西 DetachedCriteria dCriteria = DetachedCriteria.forClass(Customer.class); //2.拼装查询条件 //判断是否提供了客户名称,此处将使用apach提供的判断字符串的工具类commm-lang3.jar isNotBlank()可以去掉空格后判断字符串部位null或者"" if (StringUtils.isNotBlank(customer.getCust_name())) { //添加条件,模糊查询客户名称 dCriteria.add(Restrictions.like("cust_name","%" customer.getCust_name() "%")); } //判断是否提供了客户行业 if (StringUtils.isNotBlank(customer.getCust_industry())) { //添加条件,模糊查询客户行业 dCriteria.add(Restrictions.like("cust_industry","%" customer.getCust_industry() "%")); } //判断是否提供了客户来源 if (customer.getCust_source()!=null && StringUtils.isNotBlank(customer.getCust_source().getDict_id())) { //精确查询客户来源 dCriteria.add(Restrictions.eq("cust_source",customer.getCust_source())); } //判断是否提供了客户级别 if (customer.getCust_level()!=null && StringUtils.isNotBlank(customer.getCust_level().getDict_id())) { //精确查询客户级别 dCriteria.add(Restrictions.eq("cust_level",customer.getCust_level())); } //3.按条件查询客户信息 page = customerService.findAllCustomer(dCriteria, curNum); //4.查询获取客户来源 customerSources = customerService.findAllCustomerSource(); //5.查询所有客户级别 customerLevels = customerService.findAllCustomerLevel(); return "listCustomer"; } @Action("addCustomer") public String addCustomer() { customerService.saveCustomer(customer); return "allCustomer"; } // 把查询到的信息放在值栈中 public List<BaseDict> getCustomerSources() { return customerSources; } public Page<Customer> getPage() { return page; } public void setPage(Page<Customer> page) { this.page = page; } public void setCustomerSources(List<BaseDict> customerSources) { this.customerSources = customerSources; } public List<BaseDict> getCustomerLevels() { return customerLevels; } public void setCustomerLevels(List<BaseDict> customerLevels) { this.customerLevels = customerLevels; } public Integer getCurNum() { return curNum; } public void setCurNum(Integer curNum) { this.curNum = curNum; } public List getList() { return list; } public void setList(List list) { this.list = list; } } |