開發環境:JDK1.八、Eclipse、Sping Boot + Thymeleaf框架。javascript
一. 構建Sping Boot + Thymeleaf框架的項目(再也不詳述):css
1. 新建一個maven project項目:demo。html
2. 修改pom.xml配置,把項目配置爲Spring Boot項目;java
3. 配置Thymeleaf:添加Thymeleaf依賴,並在application.properties文件中添加Thymeleaf的配置;jquery
4. 新建DemoController,添加showWord、showIndex方法:web
@RequestMapping(value="/word", method=RequestMethod.GET) public ModelAndView showWord(HttpServletRequest request, Map<String,Object> map){ ModelAndView mv = new ModelAndView("Word"); return mv; } @RequestMapping(value="/index", method=RequestMethod.GET) public ModelAndView showIndex(){ ModelAndView mv = new ModelAndView("Index"); return mv; }
5. 新建Thymeleaf模板頁:Word.html、Index.html;sql
6. 運行demo項目,併成功訪問:http://localhost:8080/index服務器
2、 集成PageOfficeapp
1. 在pom.xml中添加PageOffice的依賴:框架
<!-- 添加Sqlite依賴(可選:若是不須要使用印章功能的話,不須要添加此依賴)--> <dependency> <groupId>org.xerial</groupId> <artifactId>sqlite-jdbc</artifactId> <version>3.7.2</version> </dependency> <!-- 添加PageOffice依賴(必須) --> <dependency> <groupId>com.zhuozhengsoft</groupId> <artifactId>pageoffice</artifactId> <version>4.3.0.2</version> </dependency>
2. 在application.properties文件中添加兩個自定義參數配置,posyspath:指定一個磁盤目錄用來存放PageOffice註冊成功以後生成的license.lic文件;popassword:設置PageOffice自帶印章管理程序的登陸密碼;以備給PageOffice的服務器端Servlet程序使用:
######################################################## ###PageOffice ######################################################## posyspath=d:/lic/ popassword=111111
3. 在DemoController中添加代碼獲取上一步在application.properties中定義的兩個參數:
@Value("${posyspath}") private String poSysPath; @Value("${popassword}") private String poPassWord;
4. 在DemoController中添加PageOffice的Servlet的註冊代碼:
/** * 添加PageOffice的服務器端受權程序Servlet(必須) * @return */ @Bean public ServletRegistrationBean servletRegistrationBean() { com.zhuozhengsoft.pageoffice.poserver.Server poserver = new com.zhuozhengsoft.pageoffice.poserver.Server(); //設置PageOffice註冊成功後,license.lic文件存放的目錄 poserver.setSysPath(poSysPath); ServletRegistrationBean srb = new ServletRegistrationBean(poserver); srb.addUrlMappings("/poserver.zz"); srb.addUrlMappings("/posetup.exe"); srb.addUrlMappings("/pageoffice.js"); srb.addUrlMappings("/jquery.min.js"); srb.addUrlMappings("/pobstyle.css"); srb.addUrlMappings("/sealsetup.exe"); return srb; // }
5. 在DemoController的showWord方法中添加建立PageOfficeCtrl對象的代碼,其中WebOpen方法的第一個參數是office文件在服務器端的磁盤路徑,在此demo中暫時使用常量:d:\\test.doc
@RequestMapping(value="/word", method=RequestMethod.GET) public ModelAndView showWord(HttpServletRequest request, Map<String,Object> map){ //--- PageOffice的調用代碼 開始 ----- PageOfficeCtrl poCtrl=new PageOfficeCtrl(request); poCtrl.setServerPage("/poserver.zz");//設置受權程序servlet poCtrl.addCustomToolButton("保存","Save",1); //添加自定義按鈕 poCtrl.setSaveFilePage("/save");//設置保存的action poCtrl.webOpen("d:\\test.doc",OpenModeType.docAdmin,"張三"); map.put("pageoffice",poCtrl.getHtmlCode("PageOfficeCtrl1")); //--- PageOffice的調用代碼 結束 ----- ModelAndView mv = new ModelAndView("Word"); return mv; }
6. 在Word.html中添加PageOffice客戶端控件所在的div和js代碼:
<div style="width:1000px;height:700px;" th:utext="${pageoffice}"> </div> <script type="text/javascript"> function Save() { document.getElementById("PageOfficeCtrl1").WebSave(); } </script>
7. 在Word.html中添加pageoffice.js和jquery.min.js的引用,並添打開文件的超連接:
<script type="text/javascript" src="jquery.min.js"></script> <script type="text/javascript" src="pageoffice.js" id="po_js_main"></script> <a href="javascript:POBrowser.openWindowModeless('/word','width=1200px;height=800px;');">打開文件</a>
8. 在DemoController添加saveFile方法,用來接收PageOffice客戶端上傳的文件流並保存到服務器指定磁盤目錄,在此demo中暫時使用常量:d:\\
@RequestMapping("/save") public void saveFile(HttpServletRequest request, HttpServletResponse response){ FileSaver fs = new FileSaver(request, response); fs.saveToFile("d:\\" + fs.getFileName()); fs.close(); }
9. 在d盤根目錄下準備一個test.doc文件(不要用0字節的文件)以備測試;
10. 運行demo項目,訪問:http://localhost:8080/index點擊「打開文件」的超連接便可在線打開、編輯和保存文件。
3、源碼下載
下載地址:http://www.zhuozhengsoft.com/download/PageOffice4.3.0.2ForSpringBoot.zip