要想通过webservice发布静态化页面也就是freemaker 首先你要有一个模板页面其次你得写一个服务端实现类和接口
在你要生成的目录下打开cmd命令窗口 输入 wsdl2java -d. -p 包名 发布的网址(例如http://localhost:8080/项目名/service)
public class EbWSItemServiceImpl implements EbWSItemService {
@Autowired
private IEbItemDao itemDao;
public String publishItem(Long itemId, String password) throws Exception {
if(StringUtils.equals(pass, password)){
EbItem item = itemDao.findItemDetailById(itemId);
Map<String, Object> map = new HashMap<String, Object>();
map.put("item", item);
FMutil fMutil = new FMutil();
fMutil.ouputFile("productDetail.ftl", item.getItemId()+".html", map);
return "success";
}else{
return "password error";
}
}
}
这是我的服务端实现类其中的FMutil是一个生成静态化页面的类
public class FMutil {
/**
*
* @param ftlName:模板名
* @param fileName:生成的html名字
* @param map 数据库中的数据
* @throws Exception
*/
public void ouputFile(String ftlName, String fileName, Map<String, Object> map) throws Exception{
//创建fm的配置
Configuration config = new Configuration();
//指定默认编码格式
config.setDefaultEncoding("UTF-8");
//设置模板的包路径
config.setClassForTemplateLoading(this.getClass(), "/com/cb/ecps/ftl");
//获得包的模板
Template template = config.getTemplate(ftlName);
//指定文件输出的路径
String path = "D:/JAVA_EE/workspace/ecps-parent/ecps-protal/src/main/webapp/static";
//定义输出流,注意的必须指定编码
Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(path+"/"+fileName)),"UTF-8"));
//生成模板
template.process(map, writer);
}
}
通过这个类指定生成地址 字符编码 就可以生成相应的代码 但是我们要在项目里使用它远远没有这么简单
接下来我们需要将它发布到服务器上 要想发布到服务器上需要如下配置
cxf-servlet.xml
<?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:jaxws="http://cxf.apache.org/jaxws"
xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<!-- 引入CXF Bean定义如下,早期的版本中使用 -->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<!--
address:
http://localhost:8080/ecps-portal/[url-partten]/address
如果不加/ 页面则取不到相应路径
-->
<jaxws:server id="publishItem" address="/publishItem" serviceClass="com.cb.ecps.ws.service.EbWSItemService">
<jaxws:serviceBean>
<bean class="com.cb.ecps.ws.service.impl.EbWSItemServiceImpl"></bean>
</jaxws:serviceBean>
</jaxws:server>
</beans>
web.xml
<!--发布webservice 一旦访问service下的类服务器就将该webservice发布出去-->
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
在你要生成的目录下打开cmd命令窗口 输入 wsdl2java -d. -p 包名 发布的网址(例如http://localhost:8080/项目名/service)
这样就能生成客户端代码