action跳轉到本地靜態html
項目新需求:需要將help鏈接指向到本地配置文件夾里面的一個靜態幫助頁面(hp)。
個人分析:
如果將hp存放在工程目錄下,這個問題就很好解決了。只要用javascript就可以完全控制了。
1 <a href="javascript:openHelp();">Help</a>
1 function openHelp()
2 {
3 window.open("/yourProj/form/hp.html","help","top=0, left=0, toolbar=no,menubar=no, scrollbars=yes, resizable=yes,location=no, status=yes");
4 }
注:hp.html存放路徑是:yourProj/WebContent/form/hp.html2 {
3 window.open("/yourProj/form/hp.html","help","top=0, left=0, toolbar=no,menubar=no, scrollbars=yes, resizable=yes,location=no, status=yes");
4 }
這樣可以在新窗口中彈出hp頁面。
殘酷的現實:
需求需要將hp存放在服務器其他文件夾下,如果還用javascript控制,就不知道能不能行了。我試過將window.open()的第一個參數改成hp相應的文件夾絕對路徑,但是javascript報錯,打不開。還有個疑問,如果這樣能行的話,是不是打開是的客戶端電腦上的文件,還是服務器上的文件?
折中辦法:
想了兩個辦法解決:
- 創建新的action專門用于打開hp頁面。
- 在系統登錄后,將hp頁面的內容讀取到session中,然后在用戶點擊help鏈接時,從session中讀取文件內容寫到新窗口。
jsp
1 <a href="<%= basePath %>getHelp.do?" target="_blank">Help</a>
注:關于這個_blank也想了很久,action跳轉本身是直來直去的,利用<a> 的屬性就可以實現在新窗口打開hp頁面。另外插一句,如果是form提交的也可以實現在新頁面打開,只需在submit方法中添加這句話 document.XXXXXXForm.target="_blank";aciton
1 String SystemHelp = "/yourDir/hp.htm";
2
3 File existFile = new File(SystemHelp);
4 if (!existFile.exists()) {
5 throw new Exception("file isn't exist: " + SystemHelp);
6 }
7
8 String mimetype = "text/html;charset=UTF-8";
9 response.setContentType(mimetype);
10
11 ServletOutputStream out = response.getOutputStream();
12 FileInputStream fis = new FileInputStream(SystemHelp);
13 BufferedInputStream bis = new BufferedInputStream(fis);
14
15 byte[] buf = new byte[4096];
16 int len = 0;
17 while ((len = bis.read(buf)) != -1) {
18 out.write(buf, 0, len);
19 }
20 out.flush();
21 out.close();
22 bis.close();
23 fis.close();
24
25 return mapping.findForward(null);
struts-config.xml2
3 File existFile = new File(SystemHelp);
4 if (!existFile.exists()) {
5 throw new Exception("file isn't exist: " + SystemHelp);
6 }
7
8 String mimetype = "text/html;charset=UTF-8";
9 response.setContentType(mimetype);
10
11 ServletOutputStream out = response.getOutputStream();
12 FileInputStream fis = new FileInputStream(SystemHelp);
13 BufferedInputStream bis = new BufferedInputStream(fis);
14
15 byte[] buf = new byte[4096];
16 int len = 0;
17 while ((len = bis.read(buf)) != -1) {
18 out.write(buf, 0, len);
19 }
20 out.flush();
21 out.close();
22 bis.close();
23 fis.close();
24
25 return mapping.findForward(null);
1 <action path="/getHelp" type="youPackage.GetHelpAction">
2 <exception key="help.exception" type="java.lang.Exception" path="yourProj.exception"></exception>
3 </action>
這樣勉強是實現了,但我不知道這是不是最好的辦法,也不知道這樣實現會不會有別的問題,至少目前看沒什么問題。2 <exception key="help.exception" type="java.lang.Exception" path="yourProj.exception"></exception>
3 </action>
第二種辦法:
這個辦法就比較土了,是我想破頭想出的辦法。還是用window.open()方法,只不過是打開一個空白的,然后在里面寫入hp.html文件內容。土不土?
在系統登錄成功后,將hp.html的內容讀到session中。
1 String SystemHelp = "/yourDir/hp.htm";
2 File existFile = new File(SystemHelp);
3 if (existFile.exists()) {
4 BufferedReader in11 = new BufferedReader(new FileReader(SystemHelp ));
5 String s;
6 String links="";
7 while((s=in11.readLine())!=null){
8 links+=s;
9 }
10
11 session.setAttribute("helpContent", links);
12 }else{
13 session.setAttribute("helpContent", "file isn't exist:" + SystemHelp );
14 }
至于是用String保存文件內容還是String[]還是別的就值得另外探討了。2 File existFile = new File(SystemHelp);
3 if (existFile.exists()) {
4 BufferedReader in11 = new BufferedReader(new FileReader(SystemHelp ));
5 String s;
6 String links="";
7 while((s=in11.readLine())!=null){
8 links+=s;
9 }
10
11 session.setAttribute("helpContent", links);
12 }else{
13 session.setAttribute("helpContent", "file isn't exist:" + SystemHelp );
14 }
然后 修改openHelp()函數
1 function openHelp()
2 {
3 openWin=window.open("/yourProj/form/hp.html","help","top=0, left=0, toolbar=no,menubar=no, scrollbars=yes, resizable=yes,location=no, status=yes");
4 openWin.document.write(<%=session.getAttribute("helpContent")%>);
5 openWin.document.close();
6 }
這樣就算是實現了。但是總感覺這個辦法不太合適。2 {
3 openWin=window.open("/yourProj/form/hp.html","help","top=0, left=0, toolbar=no,menubar=no, scrollbars=yes, resizable=yes,location=no, status=yes");
4 openWin.document.write(<%=session.getAttribute("helpContent")%>);
5 openWin.document.close();
6 }
小結:
先把問題列在這里,看看以后能不能再碰到,再思考思考。
-----------最新進展的分割線2010.08.13-------------------------------
用第一種辦法實現之后,出現一個問題。是這樣的,hp文檔里面有很多html頁面互相銜接,而且都是用相對路徑。單單在電腦上用文件形式打開時,各鏈接間跳轉正常,URL是file:///E:/support/SystemHelp.htm。
但當用上面第一種辦法實現的時候,打開第一個hp鏈接是正常的,再點開里面的鏈接就出問題了。原因是就算用第一種辦法實現,打開hp之后的url還是http://yourIP/yourProj/getHelp.do。再之后顯然就不對了。
解決辦法:
建立虛擬目錄。實際上就是將幫組文檔看作一個獨立的Proj系統。這樣就跟本系統獨立開來,而在本系統只需要一個鏈接就可以打開hp。
建立虛擬目錄,按各server的種類稍不一樣。
列舉來說:
hp文檔實際路徑 E:\support
起始文檔:SystemHelp.htm
對Tomcat來說,修改server.xml
在</Context>和</Host>之間添加以下代碼:
<Context path="/yourProjHelp" docBase="e:/support" debug="0" reloadable="true" crossContext="true"></Context>
對Weblogic來說,修改weblogic.xml
添加以下代碼:
<virtual-directory-mapping>
<local-path>e:/support</local-path>
<url-pattern>/yourProjHelp/*</url-pattern>
</virtual-directory-mapping>
對weblogic按照以上方法,沒有實現成功,有點問題,暫擱再議。<local-path>e:/support</local-path>
<url-pattern>/yourProjHelp/*</url-pattern>
</virtual-directory-mapping>
在頁面引用
<a href="http://localhost:8080/yourProjHelp/SystemHelp.htm" target="_blank">Help</a>
注對于以上URL,一般不推薦用硬代碼,所以可以按下面這樣修改。在jsp頭部添加下述定義
<%
//String path = request.getContextPath();
String path = yourProjHelp;
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
//String path = request.getContextPath();
String path = yourProjHelp;
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
這樣引用URL就就可以改成
<a href="<%= basePath %>SystemHelp.htm" target="_blank">Help</a>
posted on 2010-04-27 11:09 游雯 閱讀(3326) 評論(0) 編輯 收藏 所屬分類: Java編程技巧