Jsf 自定义下载servlet

Jsf 自定义下载servlet,jsf,servlets,download,Jsf,Servlets,Download,我研究了BalusC的代码,从AbsolutePath下载了一个定制的servlet(请参阅)。我不是Java Web开发专家,所以如果有人能解释这部分代码,我会很高兴 private String filePath; // Actions ------------------------------------------------------------------------------------ public void init() throws ServletException

我研究了BalusC的代码,从AbsolutePath下载了一个定制的servlet(请参阅)。我不是Java Web开发专家,所以如果有人能解释这部分代码,我会很高兴

private String filePath;

// Actions ------------------------------------------------------------------------------------

public void init() throws ServletException {

    // Define base path somehow. You can define it as init-param of the servlet.
    this.filePath = "/files";

    // In a Windows environment with the Applicationserver running on the
    // c: volume, the above path is exactly the same as "c:\files".
    // In UNIX, it is just straightforward "/files".
}
什么时候调用init方法?为什么我们需要在init方法中设置文件路径

我有一个XHTML(Mojarra+IceFaces),下面的代码非常有用。我的页面只缺少下载文件的一部分,该部分由
outputLink
标记引用

                <ice:tree id="tree"
                          value="#{treeBean.model}"
                          var="item"
                          hideRootNode="false"
                          hideNavigation="false"
                          >
                    <ice:treeNode>
                        <f:facet name="icon">
                            <ice:panelGroup style="display: inline">
                                <h:graphicImage value="#{item.userObject.icon}" />
                            </ice:panelGroup>
                        </f:facet>
                        <f:facet name="content">
                            <ice:panelGroup style="display: inline-block">
                                <ice:outputLink value="#{item.userObject.filePath}">
                                    <ice:outputText value="#{item.userObject.fileName}"/>
                                </ice:outputLink>
                            </ice:panelGroup>
                        </f:facet>
                    </ice:treeNode>
                </ice:tree>

这就是我用IceFaces渲染树的方式,我只是在支持bean中有文件名(即ok.txt或bad.txt),但我不知道如何下载树中链接指向的文件。

Well终于让它工作了

首先感谢巴卢斯,这里有一些帖子帮助我理解,但有人删除了它们。不管怎样,这是我学到的

  • 在Servlet的init方法中,filePath变量必须指向下载文件的绝对路径
  • web.xml中,当浏览器具有该url模式时,servlet映射url模式将导致执行servlet
  • 在xhtml页面中,链接的值应以url模式开头,后跟名称(或路径+文件名),因此当您单击链接时,下载开始
  • 这就是我要做的

    在问题的示例中,servlet的init方法中的filePath变量将指向绝对路径,类似于C:\myApp\dataFiles 然后,在xhtml中,将使用以下命令调用servlet

    <ice:outputLink value="dl/myPath/#{myBean.fileName}>
        <ice:outputText value="#{myBean.fileName}"/>
    </ice:outputLink>
    

    我自己删除了它,因为这个问题变得过于广泛和本地化,我再也看不到你真正的问题了。花更多的时间来计算/猜测你的具体问题是不值得的。你认为你是对的。不过还是谢谢你,有了你的建议,我解决了我的问题。
    
    <ice:outputLink value="dl/myPath/#{myBean.fileName}>
        <ice:outputText value="#{myBean.fileName}"/>
    </ice:outputLink>