【问题标题】:How to set Struts 2 object inside a Struts 2 tag (a href)如何在 Struts 2 标签内设置 Struts 2 对象(a href)
【发布时间】:2013-12-31 18:27:06
【问题描述】:

我有一个看起来像这样的文件对象

public class FileTO implements Serializable{
private String fileName;
private String filePath;

public String getFileName() {
    return fileName;
}

public void setFileName(String fileName) {
    this.fileName = fileName;
}

public String getFilePath() {
    return filePath;
}

public void setFilePath(String filePath) {
    this.filePath = filePath;
}

当然,我的 struts 动作响应中还有很多其他对象,我没有在这里列出。 操作完成后,filePath 将填充它所在文件的实际路径,以便可以下载文件。我想在<s:a> 标记中显示文件名和文件路径。

目标是让 href 指向 filePath。我尝试使用 OGNL,即 #、%{}、$(),但似乎都没有正确显示链接。

例如:

<s:a href="?????????"> Click to the get the File </s:a>

【问题讨论】:

  • 你能提供你的动作类代码吗??

标签: java jsp struts2 ognl struts-tags


【解决方案1】:

这个问题有点不清楚,但据我了解,您正在寻找s:url 标签。

<a href="<s:url value="%{filePath+fileName}"/>">Link</a>

【讨论】:

    【解决方案2】:
    <s:a href="?????????"> Click to the get the File </s:a>
    
    1. 如果文件位于可访问的文件夹中:

      <s:a href="http://server.with.files.com/path/to/file/fileName.txt"> 
          Click to the get the File 
      </s:a>
      
    2. 如果文件位于受保护的、不可访问的文件夹中,或者在 web 应用程序中,或者来自数据库等...你应该调用一个 Action(或一个 Servlet,如果不在 Struts2 中),那应该读取文件,并返回一个 Stream 结果。阅读:

    要理解 OGNL 语法,请改为使用read this answer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-27
      • 1970-01-01
      相关资源
      最近更新 更多