用户下订单(客户端)(可能是多个)把订单重组后(服务端),供客户端调用并展示
1先搭建CXF 环境
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd" default-autowire="byName"> <!-- 导入CXF的内置Bean --> <import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> <!-- 服务终端 --> <jaxws:endpoint id="test" implementor="com.thd2000.coccrm.test.webservice.TestDemoImpl" address="/TestDemo" /> <!-- 客户端登录 --> <bean id="memberlogin" class="com.thd2000.coccrm.service.ws.MemberLoginImpl"> <property name="tbMemberUsersDao" ref="tbMemberUsersDao"></property> <property name="tbMembermgtDao" ref="tbMembermgtDao"></property> </bean> <jaxws:endpoint id="memberloginService" implementor="#memberlogin" address="/MemberLogin"/> <!-- 客户端修改密码 --> <bean id="modifypassword" class="com.thd2000.coccrm.service.ws.ModifyMemberPasswordImpl"> <property name="tbMemberUsersDao" ref="tbMemberUsersDao"></property> </bean> <jaxws:endpoint id="modifypasswordService" implementor="#modifypassword" address="/ModifyPassword"/> <!-- 客户端订单信息展现 --> <bean id="memberOrderList" class="com.thd2000.coccrm.service.ws.MemberOrderListImpl"> <property name="tbMemberUsersDao" ref="tbMemberUsersDao"></property> <property name="tbMembermgtDao" ref="tbMembermgtDao"></property> <property name="tbMemberDao" ref="tbMemberDao"></property> <property name="tbOrderDao" ref="tbOrderDao"></property> </bean> <jaxws:endpoint id="memberOrderListService" implementor="#memberOrderList" address="/MemberOrderList"/> </beans>
2服务端接口
package com.thd2000.coccrm.service.ws; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; @WebService public interface MemberOrderList { @WebResult(name="String") public String getMemberOrderList(@WebParam(name = "memberuserid")String memberuserid) throws Exception ; } 3 服务端接口实现类 package com.thd2000.coccrm.service.ws; import java.util.Set; import javax.jws.WebService; import com.thd2000.coccrm.dao.tbmember.TbMemberDao; import com.thd2000.coccrm.dao.tbmembermgt.TbMembermgtDao; import com.thd2000.coccrm.dao.tbmemberusers.TbMemberUsersDao; import com.thd2000.coccrm.dao.tborder.TbOrderDao; import com.thd2000.coccrm.model.TbMember; import com.thd2000.coccrm.model.TbMemberUsers; import com.thd2000.coccrm.model.TbMembermgt; import com.thd2000.coccrm.model.TbOrder; import com.thd2000.coccrm.util.DateUtil; @WebService public class MemberOrderListImpl implements MemberOrderList { private TbMemberUsersDao tbMemberUsersDao ; private TbMembermgtDao tbMembermgtDao; private TbMemberDao tbMemberDao; private TbOrderDao tbOrderDao ; public String getMemberOrderList(String memberuserid) throws Exception { //第一步 通过会员用户主键ID获得会员用户对象 TbMemberUsers tbMemberUsers = (TbMemberUsers) tbMemberUsersDao.get(TbMemberUsers.class, memberuserid); //第二步 通过会员用户获得会员主表对象 TbMembermgt tbMembermgt = tbMemberUsers.getTbMembermgt(); //第三步 通过会员主表获得会员明细集合(Set) Set<TbMember> tbMemberSet = tbMembermgt.getTbMemberSet(); //第四步 在会员明细集合中需要获得每个会员对应的订单集合 同时需要重新组装一个订单集合 if (tbMemberSet != null && tbMemberSet.size() > 0) { for (TbMember tbMember : tbMemberSet) { if (tbMember != null) { //遍历会员中的订单集合 Set<TbOrder> tbOrderSet = tbMember.getOrders(); //遍历订单集合中的字段 StringBuilder xmlResult=new StringBuilder("<?xml version='1.0' encoding='UTF-8'?>"); //增加<ORDERS>作为一级根节点,因为< ORDER >可能有多个,程序无法找到根节点 同时使用i 变量区分不同的节点,避免使用for循环 或迭代循环,引发的性能问题 xmlResult.append("<ORDERS>"); int i = 1; for(TbOrder tbOrder :tbOrderSet) { String orderid = tbOrder.getOrderid(); String ordercode = tbOrder.getOrdercode(); String ordername = tbOrder.getOrderName(); String placeorganplace = tbMember.getTsOrganizationplace().getOrganname(); String createdate = DateUtil.format(tbOrder.getCreatedate(), "yyyy-MM-dd"); String ispaymoney = tbOrder.getIspaymoney();//1已缴费0未缴费 xmlResult.append("<ORDER"+ i +">"); xmlResult.append("<Orderid>" + orderid + "</Orderid>"); xmlResult.append("<Ordercode>" + ordercode + "</Ordercode>"); xmlResult.append("<Ordername>" + ordername + "</Ordername>"); xmlResult.append("<Placeorganplace>" + placeorganplace + "</Placeorganplace>"); xmlResult.append("<Createdate>" + createdate + "</Createdate>"); if ("1".equals(ispaymoney)) { xmlResult.append("<Ispaymoney>已缴费</Ispaymoney>"); }else if ("0".equals(ispaymoney)) { xmlResult.append("<Ispaymoney>未缴费</Ispaymoney>"); } xmlResult.append("</ORDER"+ i +">"); i++; } xmlResult.append("</ORDERS>"); return xmlResult.toString(); } } } return "orderlist"; } public TbMemberUsersDao getTbMemberUsersDao() { return tbMemberUsersDao; } public void setTbMemberUsersDao(TbMemberUsersDao tbMemberUsersDao) { this.tbMemberUsersDao = tbMemberUsersDao; } public TbMembermgtDao getTbMembermgtDao() { return tbMembermgtDao; } public void setTbMembermgtDao(TbMembermgtDao tbMembermgtDao) { this.tbMembermgtDao = tbMembermgtDao; } public TbMemberDao getTbMemberDao() { return tbMemberDao; } public void setTbMemberDao(TbMemberDao tbMemberDao) { this.tbMemberDao = tbMemberDao; } public TbOrderDao getTbOrderDao() { return tbOrderDao; } public void setTbOrderDao(TbOrderDao tbOrderDao) { this.tbOrderDao = tbOrderDao; } }
3服务端接口实现类
package com.thd2000.coccrm.service.ws; import java.util.Set; import javax.jws.WebService; import com.thd2000.coccrm.dao.tbmember.TbMemberDao; import com.thd2000.coccrm.dao.tbmembermgt.TbMembermgtDao; import com.thd2000.coccrm.dao.tbmemberusers.TbMemberUsersDao; import com.thd2000.coccrm.dao.tborder.TbOrderDao; import com.thd2000.coccrm.model.TbMember; import com.thd2000.coccrm.model.TbMemberUsers; import com.thd2000.coccrm.model.TbMembermgt; import com.thd2000.coccrm.model.TbOrder; import com.thd2000.coccrm.util.DateUtil; @WebService public class MemberOrderListImpl implements MemberOrderList { private TbMemberUsersDao tbMemberUsersDao ; private TbMembermgtDao tbMembermgtDao; private TbMemberDao tbMemberDao; private TbOrderDao tbOrderDao ; public String getMemberOrderList(String memberuserid) throws Exception { //第一步 通过会员用户主键ID获得会员用户对象 TbMemberUsers tbMemberUsers = (TbMemberUsers) tbMemberUsersDao.get(TbMemberUsers.class, memberuserid); //第二步 通过会员用户获得会员主表对象 TbMembermgt tbMembermgt = tbMemberUsers.getTbMembermgt(); //第三步 通过会员主表获得会员明细集合(Set) Set<TbMember> tbMemberSet = tbMembermgt.getTbMemberSet(); //第四步 在会员明细集合中需要获得每个会员对应的订单集合 同时需要重新组装一个订单集合 if (tbMemberSet != null && tbMemberSet.size() > 0) { for (TbMember tbMember : tbMemberSet) { if (tbMember != null) { //遍历会员中的订单集合 Set<TbOrder> tbOrderSet = tbMember.getOrders(); //遍历订单集合中的字段 StringBuilder xmlResult=new StringBuilder("<?xml version='1.0' encoding='UTF-8'?>"); //增加<ORDERS>作为一级根节点,因为< ORDER >可能有多个,程序无法找到根节点 同时使用i 变量区分不同的节点,避免使用for循环 或迭代循环,引发的性能问题 xmlResult.append("<ORDERS>"); int i = 1; for(TbOrder tbOrder :tbOrderSet) { String orderid = tbOrder.getOrderid(); String ordercode = tbOrder.getOrdercode(); String ordername = tbOrder.getOrderName(); String placeorganplace = tbMember.getTsOrganizationplace().getOrganname(); String createdate = DateUtil.format(tbOrder.getCreatedate(), "yyyy-MM-dd"); String ispaymoney = tbOrder.getIspaymoney();//1已缴费0未缴费 xmlResult.append("<ORDER"+ i +">"); xmlResult.append("<Orderid>" + orderid + "</Orderid>"); xmlResult.append("<Ordercode>" + ordercode + "</Ordercode>"); xmlResult.append("<Ordername>" + ordername + "</Ordername>"); xmlResult.append("<Placeorganplace>" + placeorganplace + "</Placeorganplace>"); xmlResult.append("<Createdate>" + createdate + "</Createdate>"); if ("1".equals(ispaymoney)) { xmlResult.append("<Ispaymoney>已缴费</Ispaymoney>"); }else if ("0".equals(ispaymoney)) { xmlResult.append("<Ispaymoney>未缴费</Ispaymoney>"); } xmlResult.append("</ORDER"+ i +">"); i++; } xmlResult.append("</ORDERS>"); return xmlResult.toString(); } } } return "orderlist"; } public TbMemberUsersDao getTbMemberUsersDao() { return tbMemberUsersDao; } public void setTbMemberUsersDao(TbMemberUsersDao tbMemberUsersDao) { this.tbMemberUsersDao = tbMemberUsersDao; } public TbMembermgtDao getTbMembermgtDao() { return tbMembermgtDao; } public void setTbMembermgtDao(TbMembermgtDao tbMembermgtDao) { this.tbMembermgtDao = tbMembermgtDao; } public TbMemberDao getTbMemberDao() { return tbMemberDao; } public void setTbMemberDao(TbMemberDao tbMemberDao) { this.tbMemberDao = tbMemberDao; } public TbOrderDao getTbOrderDao() { return tbOrderDao; } public void setTbOrderDao(TbOrderDao tbOrderDao) { this.tbOrderDao = tbOrderDao; } }
4.客户端JavaBean (根据所需要的字段重组实体类) 之前还把服务端的JavaBean引到客户端写,完全没必要,客户端根据所需字段重组JavaBean,此时客户端JavaBean就是个临时的JavaBean容器
package com.hzfy.jtjyt.jtwz.pojo; public class TbOrder implements java.io.Serializable { private static final long serialVersionUID = -6312605219384298844L; // Fields /** 订单主键ID */ private String orderid; /** 订单编号 */ private String ordercode; /** 订单名称 */ private String ordername; /** 所属园所 */ private String organplacename; /** 创建日期 */ private String createdate; /** 是否缴费(1已缴费0未缴费) */ private String ispaymoney; public String getOrderid() { return orderid; } public void setOrderid(String orderid) { this.orderid = orderid; } public String getOrdercode() { return ordercode; } public void setOrdercode(String ordercode) { this.ordercode = ordercode; } public String getOrdername() { return ordername; } public void setOrdername(String ordername) { this.ordername = ordername; } public String getOrganplacename() { return organplacename; } public void setOrganplacename(String organplacename) { this.organplacename = organplacename; } public String getCreatedate() { return createdate; } public void setCreatedate(String createdate) { this.createdate = createdate; } public String getIspaymoney() { return ispaymoney; } public void setIspaymoney(String ispaymoney) { this.ispaymoney = ispaymoney; } }
5 客户端Action
public void showOrderList(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) throws Exception { String forwardPath = ""; String memberuserid = request.getParameter("memberuserid");// //调用Webservice登陆验证 JaxWsDynamicClientFactory dynamicClient = JaxWsDynamicClientFactory.newInstance(); Client client = dynamicClient.createClient("http://localhost:8281/cxf/MemberOrderList?wsdl"); Object[] o = client.invoke("getMemberOrderList", new Object[]{memberuserid}); ArrayList<Object> list=new ArrayList(); if(!"orderlist".equalsIgnoreCase(o[0].toString())){ Document document = DocumentHelper.parseText(o[0].toString());//将string转成xml Element root = document.getRootElement(); //获取文档的根元素 //定义个保存输出xml数据的缓冲字符串对象 StringBuffer sb = new StringBuffer(); sb.append(o[0].toString() + "\n"); //遍历当前元素(在此是根元素)的子元素 for (Iterator i_pe = root.elementIterator(); i_pe.hasNext();) { Element e_pe = (Element) i_pe.next(); //获取当前元素的名字 String orderid = e_pe.elementTextTrim("Orderid"); String ordercode = e_pe.elementTextTrim("Ordercode"); String ordername = e_pe.elementTextTrim("Ordername"); String placeorganplace = e_pe.elementTextTrim("Placeorganplace"); String createdate = e_pe.elementTextTrim("Createdate"); String ispaymoney = e_pe.elementTextTrim("Ispaymoney"); TbOrder order = new TbOrder(); order.setOrderid(orderid); order.setOrdercode(ordercode); order.setOrdername(ordername); order.setOrganplacename(placeorganplace); order.setCreatedate(createdate); order.setIspaymoney(ispaymoney); list.add(order); } request.setAttribute("list", list); forwardPath = "/jtwz/jtwz_wzsj/coccrm/orderlist.jsp"; request.getRequestDispatcher(forwardPath).forward(request, response); return ; }else{ forwardPath = "/jtwz/jtwz_wzsj/coccrm/message/list_tishi.jsp"; request.getRequestDispatcher(forwardPath).forward(request, response); return; } }