有个空间

有个标题

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.html

这样可以在新窗口中弹出hp页面。

残酷的现实
需求需要将hp存放在服务器其他文件夹下,如果还用javascript控制,就不知道能不能行了。我试过将window.open()的第一个参数改成hp相应的文件夹绝对路径,但是javascript报错,打不开。还有个疑问,如果这样能行的话,是不是打开是的客户端电脑上的文件,还是服务器上的文件?

折中办法:

想了两个办法解决:
  • 创建新的action专门用于打开hp页面。
  • 在系统登录后,将hp页面的内容读取到session中,然后在用户点击help链接时,从session中读取文件内容写到新窗口。
第一种办法:
jsp
1 <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.xml
1     <action path="/getHelp" type="youPackage.GetHelpAction">
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[]还是别的就值得另外探讨了。

然后 修改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 }
这样就算是实现了。但是总感觉这个办法不太合适。

小结:
先把问题列在这里,看看以后能不能再碰到,再思考思考。

-----------最新进展的分割线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按照以上方法,没有实现成功,有点问题,暂搁再议。


在页面引用
<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+"/";
%>

这样引用URL就就可以改成
<href="<%= basePath %>SystemHelp.htm" target="_blank">Help</a>

posted on 2010-04-27 11:09 游雯 阅读(3296) 评论(0)  编辑  收藏 所属分类: Java编程技巧


只有注册用户登录后才能发表评论。


网站导航: