Struts2 使用redirectAction结果类型传递参数时,会跳转到错误的路径

我希望通过点击下载按钮来跳转到DownloadCount.action并传递参数filename来在数据库中进行一次下载计数,再让filename参数通过redirectAction方式传参给downfile.action来进行下载,但是DownloadCount.action执行完成后会跳转到http://downfile.action/?filename=1511398398834.xlsx,问下各位为什么会出现这种情况以及如何解决,谢谢!
private.jsp

 <html>
<head>
    <title>Title</title>
</head>
<body>
<p><a href='<s:url value="upload.jsp"></s:url>'>上传文件</a></p>
<table>
    <tr><td>文件id</td><td>文件名</td><td>文件类型</td><td>文件大小</td><td>上传者</td><td>最后下载时间</td><td>下载次数</td><td>最后下载用户</td><td>文件操作</td></tr>
    <s:iterator value="filelist" var="file" id="file" >
        <tr>
            <td><s:property value="#file.fileid"/></td>
            <td><s:property value="#file.filename"/></td>
            <td><s:property value="#file.filetype"/></td>
            <td><s:property value="#file.filesize"/></td>
            <td><s:property value="#file.uploader"/></td>
            <td><s:property value="#file.lastdtime"/></td>
            <td><s:property value="#file.downtimes"/></td>
            <td><s:property value="#file.lastdownloader"/></td>
            <td><a href='<s:url value="DownloadCount.action"><s:param name="filename" value="#file.filename"></s:param></s:url>'>下载</a>&nbsp;
                <a href='<s:url value="FileDelete.action"><s:param name="filename" value="#file.filename"></s:param><s:param name="uname" value="uname"></s:param></s:url>'>删除</a>
            </td>
        </tr>
    </s:iterator>
</table>
<s:debug></s:debug>
</body>
</html>

DownloadCountAction.java

 package com.action;

import com.opensymphony.xwork2.ActionSupport;
import com.util.DBTools;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

@Namespace("/")
@ParentPackage("struts-default")
public class DownloadCountAction extends ActionSupport {
    private String filename;

    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    @Action(value = "DownloadCount",results = {
            @Result(name = "success",location = "/downfile.action?filename=${filename}",type = "redirectAction")
    })
    public String DownloadCount() throws Exception{
        DBTools db=new DBTools();
        db.FileDownload(filename);
        return SUCCESS;
    }
}

struts.xml中关于downfile.action的配置

         <action name="downfile" class="com.action.DownFileAction" method="downloadFile">
            <result name="success" type="stream">
                <param name="bufferSize">4096</param>
                <param name="contentDisposition">
                    attachment;filename="${filename}"
                </param>
                <param name="inputName">downFile</param>
            </result>
        </action>

DownFileAction.java

 package com.action;

import com.opensymphony.xwork2.ActionSupport;
import com.util.DBTools;
import org.apache.struts2.ServletActionContext;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

public class DownFileAction  extends ActionSupport{

    private String filename;

    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) throws Exception {

        this.filename = new String(filename.getBytes("ISO8859-1"),"utf-8");
    }
    public String downloadFile() throws Exception {
        return SUCCESS;
    }
    public InputStream getDownFile()throws Exception{
        String path=ServletActionContext.getServletContext().getRealPath("/WEB-INF/uploadlist");
        File file=new File(path+"/"+this.getFilename());
        InputStream inputStream=new FileInputStream(file);
        return inputStream;
    }
}

2个回答

robinhf2006_csdn
Edward-Chai 解决了 我写成<result><param></param></result>然后里面填actionName和namespace就好了 万分感谢!
2 年多之前 回复
robinhf2006_csdn
Edward-Chai 那请问下我应该如何修改路径才正确呢
2 年多之前 回复

我记得struts2有个chain事可以转发到action里的,你试试

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问