How to set Struts 2 object inside a Struts 2 tag (a href)

683 views Asked by At

I have File object which looks like this

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;
}

Of course there are lot other objects in my struts action response, which I am not listing here. After the action completes, the filePath will be populated with the actual path of the file where it resides so that the file can be downloaded. I want to display the fileName and filePath in a <s:a> tag.

Goal is to have href point to filePath. I tried play with OGNL i.e #, %{}, $() and none seems to display the link properly.

Eg:

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

There are 2 answers

0
coding_idiot On BEST ANSWER

The question is a bit unclear but from what I understand, you're looking for s:url tag.

<a href="<s:url value="%{filePath+fileName}"/>">Link</a>
0
Andrea Ligios On
<s:a href="?????????"> Click to the get the File </s:a>
  1. If the file is in an accessible folder:

    <s:a href="http://server.with.files.com/path/to/file/fileName.txt"> 
        Click to the get the File 
    </s:a>
    
  2. If the file is in a folder that is protected, not accessible, or is inside the webapplication, or comes from a database, etc... you should call an Action (or a Servlet, if not in Struts2), that should read the file, and return a Stream result. Read:

To understand the OGNL syntax, instead, read this answer.