一、先编写远程服务类的接口
1 package com.rl.ecps.ws.service; 2 3 import javax.jws.WebService; 4 5 @WebService 6 public interface EbWSItemService { 7 8 public String publishItem(Long itemId,String password) throws Exception; 9 }
二、实现该接口的方法,1、先根据id查询获取商品,2’将商品放入map中 3、将path和request_file_path变量的值放入map中 以替换模板中的内容
1 @Service 2 public class EbWSItemServiceImpl implements EbWSItemService { 3 4 @Autowired 5 private EbItemDao itemDao; 6 7 public String publishItem(Long itemId, String password) throws Exception { 8 String result = "success"; 9 if(StringUtils.equals(password, ECPSUtils.readProp("ws_pass"))) { 10 EbItem item = itemDao.selectItemDetailById(itemId); 11 Map<String,Object> map = new HashMap<String,Object>(); 12 map.put("item", item); 13 map.put("path", ECPSUtils.readProp("portal_path")); 14 map.put("request_file_path", ECPSUtils.readProp("request_file_path")); 15 FMutil.ouputFile("productDetail.ftl", item.getItemId()+".html", map); 16 }else { 17 result = "fail"; 18 } 19 return result; 20 } 21 22 }
三、配置cxf
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" 4 xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd 8 http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd 9 http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"> 10 <!-- 引入CXF Bean定义如下,早期的版本中使用 --> 11 <import resource="classpath:META-INF/cxf/cxf.xml" /> 12 <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> 13 <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> 14 15 <jaxws:server id="publishItem" address="/publishItem" serviceClass="com.rl.ecps.ws.service.EbWSItemService"> 16 <jaxws:serviceBean> 17 <bean class="com.rl.ecps.ws.service.impl.EbWSItemServiceImpl"></bean> 18 </jaxws:serviceBean> 19 </jaxws:server> 20 21 </beans>
四、在web.xml中配置servlet
1 <servlet> 2 <servlet-name>cxf</servlet-name> 3 <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> 4 </servlet> 5 <servlet-mapping> 6 <servlet-name>cxf</servlet-name> 7 <url-pattern>/services/*</url-pattern> 8 </servlet-mapping>
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:beans.xml,classpath*:cxf-servlet.xml</param-value> </context-param>
五、通过wsdl2java生成服务代码
PS F:wscode> wsdl2java -d . -p com.rl.ecps.stub http://localhost:8086/ecps-portal/services/publishItem?wsdl
六、编写调用方法
1 public String publishItem(Long itemId, String password) { 2 //创建集合的访问对象 3 EbWSItemServiceService wsItemServiceService = new EbWSItemServiceService(); 4 EbWSItemService wsItemService = wsItemServiceService.getEbWSItemServicePort(); 5 return wsItemService.publishItem(itemId, password); 6 }
七、Controller调用并返回结果给Ajax请求
1 //发布商品静态页面 2 @RequestMapping("/publishItem.do") 3 public void publishItem(Long itemId,PrintWriter writer) { 4 String result = itemService.publishItem(itemId, ECPSUtils.readProp("ws_pass")); 5 writer.write(result); 6 }
八、前台 发布 按钮的事件
1 //商品发布 2 function publish(itemId){ 3 tipShow("#refundLoadDiv"); 4 $.ajax({ 5 url:path+"/item/publishItem.do", 6 type:"post", 7 dataType:"text", 8 data:{ 9 itemId:itemId 10 }, 11 success:function(responseText){ 12 if(responseText == "success"){ 13 alert("发布成功"); 14 }else{ 15 alert("发布失败"); 16 } 17 tipHide("#refundLoadDiv"); 18 } 19 }) 20 }