转载:PageOffice最简单的集成代码(SpringMVC)
# PageOffice最简单的集成代码(SpringMVC)
本文描述了PageOffice产品在SpringMVC项目中如何集成调用。
新建SpringMVC项目:pageoffice6-springmvc-simple
在您项目的pom.xml中通过下面的代码引入PageOffice依赖。pageoffice.jar已发布到Maven中央仓库 (opens new window),建议使用最新版本。
<dependency>
<groupId>com.zhuozhengsoft</groupId>
<artifactId>pageoffice</artifactId>
<version>6.0.0.11-javax</version>
</dependency>
- 在您项目的web.xml配置PageOffice的服务器端servlet程序,添加如下代码:
<servlet>
<servlet-name>poserver</servlet-name>
<servlet-class>com.zhuozhengsoft.pageoffice.poserver.Server</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>poserver</servlet-name>
<url-pattern>/poserver.zz</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>poserver</servlet-name>
<url-pattern>/sealsetup.exe</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>poserver</servlet-name>
<url-pattern>/poclient</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>poserver</servlet-name>
<url-pattern>/pageoffice.js</url-pattern>
</servlet-mapping>
- 在pageoffice6-springmvc-simple/src/main/webapp/WEB-INF目录下新建lib文件夹,拷贝PageOffice客户端安装程序到此目录下。比如:
- Windows客户端安装程序:posetup_6.0.0.11.exe
- 国产版操作系统客户端安装程序:com.zhuozhengsoft.pageoffice_6.0.0.11_amd64.deb
PageOffice客户端安装程序下载地址:https://gitee.com/pageoffice/pageoffice6-client/releases (opens new window)
- 在pageoffice6-springmvc-simple/src/main/webapp目录下新建doc文件夹,准备一个有内容test.docx文件。新建Controller并调用PageOffice打开此文件,例如:
@Controller @RequestMapping("Document") public class DocumentController {
<span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/openFile"</span><span class="token punctuation">)</span> <span class="token keyword">public</span> <span class="token class-name">ModelAndView</span> <span class="token function">openFile</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> request<span class="token punctuation">,</span> <span class="token class-name">Map</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">Object</span><span class="token punctuation">></span></span> map<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span><span class="token punctuation">{</span> <span class="token comment">//******************************卓正PageOffice组件的使用*******************************</span> <span class="token class-name">PageOfficeCtrl</span> poCtrl <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">PageOfficeCtrl</span><span class="token punctuation">(</span>request<span class="token punctuation">)</span><span class="token punctuation">;</span> poCtrl<span class="token punctuation">.</span><span class="token function">setSaveFilePage</span><span class="token punctuation">(</span><span class="token string">"saveFile"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//打开Word文档</span> poCtrl<span class="token punctuation">.</span><span class="token function">webOpen</span><span class="token punctuation">(</span><span class="token string">"/doc/test.docx"</span><span class="token punctuation">,</span> <span class="token class-name">OpenModeType</span><span class="token punctuation">.</span>docNormalEdit<span class="token punctuation">,</span> <span class="token string">"张三"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"pageoffice"</span><span class="token punctuation">,</span> poCtrl<span class="token punctuation">.</span><span class="token function">getHtmlCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">ModelAndView</span> mv <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ModelAndView</span><span class="token punctuation">(</span><span class="token string">"Document/openFile"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> mv<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/saveFile"</span><span class="token punctuation">)</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">saveFile</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> request<span class="token punctuation">,</span> <span class="token class-name">HttpServletResponse</span> response<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span><span class="token punctuation">{</span> <span class="token class-name">FileSaver</span> fs <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileSaver</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span> response<span class="token punctuation">)</span><span class="token punctuation">;</span> fs<span class="token punctuation">.</span><span class="token function">saveToFile</span><span class="token punctuation">(</span>request<span class="token punctuation">.</span><span class="token function">getSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getServletContext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getRealPath</span><span class="token punctuation">(</span><span class="token string">"doc/"</span><span class="token punctuation">)</span><span class="token operator">+</span>fs<span class="token punctuation">.</span><span class="token function">getFileName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> fs<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
}
- 为上一步代码中的的ModelAndView准备页面。新建Document/openFile.jsp,在页面中添加下面的代码用来嵌入PageOffice控件:
<div style=" width:auto; height:800px;">
${pageoffice}
</div>
- 在需要点击超链接在线打开文件的页面添加pageoffice.js的引用。
<script type="text/javascript" src="/pageoffice.js"></script>
- 编写打开文件的超链接代码。调用POBrowser的openWindow方法弹出窗口访问Controller方法
Document/openFile
在线打开文件。
<a href="javascript:POBrowser.openWindow('Document/openFile','width=1150px;height=900px;');">
在线打开文档
</a>
- 启动项目,点击“在线打开文档”超链接,查看在线打开编辑保存Office文件的效果。