YUAN_AI 2009-05-27 14:34
浏览 319
已采纳

如何在Struts2 的Action的拦截器中设置Action的参数

[color=red] 问题描述如下:[/color]

1. Action的配置如下:
<action name="download" class="download">

        <interceptor-ref name="defaultStack" />
        [color=blue]<interceptor-ref name="downloadInter" />[/color]
        [color=blue]<param name="inputPath">/WEB-INF/uploadFiles/article.pdf</param>[/color]
        <result name="success" type="stream">
            <param name="contentType">application/pdf</param>
            <param name="inputName">targetFile</param>
            <param name="contentDisposition">
                attachment;filename="yourArticle.pdf"
            </param>
            <param name="bufferSize">2048</param>
        </result>
    </action>

2. Action部分代码如下:

public class FileDownloadAction extends ActionSupport {
private String inputPath;
    //inputPath的getter与setter略

3. 我想在[color=blue]downloadInter[/color]拦截器中设置Action的i[color=blue]nputPath[/color]参数,以便将从数据库中读取的  pdf文件,提供给读者下载。

4. downloadInter代码如下:

[size=x-large] public class FileDownloadInterceptor extends AbstractInterceptor {

private SpringJdbcHelper springJdbcHelper;
private LobHandler lobHandler;

@Override
public String intercept(ActionInvocation invocation) throws Exception {
    System.out.println("FileDownloadInterceptor---intercept...");

    invocation.addPreResultListener(new PreResultListener() {

        @SuppressWarnings("unchecked")
        public void beforeResult(ActionInvocation invocation,
                String resultCode) {
            System.out.println("FileDownloadInterceptor---beforeResult...");

            HttpSession session = ActionHelper.getHttpSession();
            final String year = (String) session.getAttribute("year");
            final String month = (String) session.getAttribute("month");
            final String articleNo = (String) session
                    .getAttribute("articleNo");

            @SuppressWarnings("unused")
            Map<String, ResultConfig> resultMap = invocation.getProxy()
                    .getConfig().getResults();
            ActionConfig config = invocation.getProxy().getConfig();
            ResultConfig finalResultConfig = resultMap.get(resultCode);

            final String sqlStr = " SELECT pubchpapername,pubfile FROM periodical_show_article "
                    + " WHERE pubyear = ? AND pubmonth = ? AND pubpaperno = ? ";
            @SuppressWarnings("unused")
            ArrayList<ShowArticle> pdfsList = (ArrayList<ShowArticle>) getSpringJdbcHelper()
                    .getJdbcTemplate().query(sqlStr,
                            new Object[] { year, month, articleNo },
                            new RowMapper() {

                                public Object mapRow(ResultSet rs,
                                        int rowNum) throws SQLException {
                                    String name = rs.getString(1);
                                    byte[] pubpdf = getLobHandler()
                                            .getBlobAsBytes(rs, 2);
                                    final ShowArticle showArticle = new ShowArticle();
                                    showArticle.setPubChPaperName(name);
                                    showArticle.setPubFile(pubpdf);
                                    return showArticle;
                                }

                            });
            String dir = null;
            File file = null;
            String realDir = null;
            String newFile = year + month + articleNo + "_"
                    + new Date().getTime() + ".pdf";
            BufferedOutputStream bos = null;
            FileOutputStream fos;
            try {
                dir = new File(".").getCanonicalPath();
                System.out.println("dir = " + dir);
                file = new File(dir, newFile);
                realDir = file.getCanonicalPath();
                System.out.println("realDir = " + realDir);
                fos = new FileOutputStream(file);
                bos = new BufferedOutputStream(fos);
                bos.write(pdfsList.get(0).getPubFile());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            [color=blue]// 我想在此设置inputPath参数[/color]
            [color=blue]config.addParam("inputPath", realDir);[/color]
            finalResultConfig.addParam("contentType", "application/pdf");
            finalResultConfig.addParam("contentDisposition", "attachment");
            System.out.println("FileDownloadInterceptor---addParam...");
        }

    });
    return invocation.invoke();
}

public SpringJdbcHelper getSpringJdbcHelper() {
    return springJdbcHelper;
}

public void setSpringJdbcHelper(SpringJdbcHelper springJdbcHelper) {
    this.springJdbcHelper = springJdbcHelper;
}

public LobHandler getLobHandler() {
    return lobHandler;
}

public void setLobHandler(LobHandler lobHandler) {
    this.lobHandler = lobHandler;
}

}
[/size]

5. [color=red]如果有更好的方案,希望不吝赐教,谢谢![/color]
  • 写回答

1条回答 默认 最新

  • jianfeng008cn 2009-05-27 22:30
    关注

    actioninvocation.getaction.getvaluestack watch一下就知道怎么获取了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示