struts2使用了拦截器传参数问题,实例主要功能就是文件下载,我不使用拦截器时我可以得到参数的文件名,但使用了就得不到文件的名字。那一位高手帮我看一下,谢谢了
AuthorizationInterceptor.java
package com.tfc.action;

import java.util.Map;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

@SuppressWarnings(
"serial")
public class AuthorizationInterceptor extends AbstractInterceptor {

    @SuppressWarnings(
"unchecked")
    
public String intercept(ActionInvocation ai) throws Exception {
        Map session 
= ai.getInvocationContext().getSession();        
        String role 
= (String) session.get("username");
        System.out.println(
"AuthorizationInterceptor.username:\t" + role);
        
if (null != role) {
            Object o 
= ai.getAction();
            System.out.println(
"AuthorizationInterceptor:o\t" + o);
            
if (o instanceof RoleAware) {
                RoleAware action 
= (RoleAware) o;
                action.setRole(role);
            }
            
return ai.invoke();
        } 
else {
            
return Action.LOGIN;
        }
    }

}
LoginAction.java
package com.tfc.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.tfc.dao.DBUtil;

@SuppressWarnings(
"serial")
public class LoginAction extends ActionSupport implements ServletRequestAware {
    
private String username;
    
private String password;
    
private HttpServletRequest request;
    @SuppressWarnings(
"unchecked")
    
public String execute() throws Exception {
        
// TODO Auto-generated method stub
        DBUtil dbUtil = new DBUtil();
        List userinfolist 
= dbUtil
                .selectPepoleOne(
"select * from tfcuserinfo where username='"
                        
+ username + "'and password='" + password + "'");
        System.out.print(userinfolist.size());
        @SuppressWarnings(
"unused")
        
// loginInfo 0 未登陆,1登陆成功,2登陆失败
        int loginInfo = 0;
        
if (userinfolist.size() > 0) {
            loginInfo 
= 1;
            request.setAttribute(
"userinfoList", userinfolist);
            ActionContext.getContext().getSession().put(
"userinfolist", userinfolist);
            request.setAttribute(
"loginInfo", loginInfo);
            ActionContext.getContext().getSession().put(
"username", username);
            
return ActionSupport.SUCCESS;
        } 
else {
            loginInfo 
= 2;
            request.setAttribute(
"loginInfo", loginInfo);
            
return ActionSupport.INPUT;
        }
    }

    
public void setServletRequest(HttpServletRequest request) {
        
// TODO Auto-generated method stub
        this.request = request;
    }

    
public String getUsername() {
        
return username;
    }

    
public void setUsername(String username) {
        
this.username = username;
    }

    
public String getPassword() {
        
return password;
    }

    
public void setPassword(String password) {
        
this.password = password;
    }
}
DownloadAction.java
package com.tfc.action;

import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings(
"serial")
public class DownloadAction extends ActionSupport implements
        ServletRequestAware, RoleAware {
    
private String filename;
    
private String role;
    
private HttpServletRequest request;

    
public InputStream getDownloadFile() {
        System.out.println(
"DownloadFile2");
        
return ServletActionContext.getServletContext().getResourceAsStream(
                
"/images/" + "11.rar");
    }

    
public String execute() {
        System.out.println(
"execute1");
        System.out.println(
"/images/" + filename);
        System.out.println(
"/role/" + role);
        String username 
= (String) ActionContext.getContext().getSession().get(
                
"username");
        System.out.println(
"username:\t"+username);
        
return SUCCESS;
    }

    
public String getFilename() {
        
return filename;
    }

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

    
public void setRole(String role) {
        
this.role = role;
    }

    
public String getRole() {
        
return role;
    }

    
public void setServletRequest(HttpServletRequest request) {
        
// TODO Auto-generated method stub
        this.request = request;
    }
}
RoleAware.java
package com.tfc.action;

public interface RoleAware {
    
void setRole(String role);
}
配置文件,struts.xml
<struts>
    
<!-- 解决字符编码 -->
    
<constant name="struts.i18n.encoding" value="GB2312" />    
    
<package name="default" extends="struts-default">
        
<!-- 使用拦截器判断用户是否登陆,如果登陆则下载相应的附件     -->
        
<interceptors>
            
<interceptor name="auth"
                class
="com.tfc.action.AuthorizationInterceptor" />                
        
</interceptors>        
        
<action name="login" class="com.tfc.action.LoginAction">
            
<result type="chain">download</result>
            
<result name="success">/download.jsp</result>
            
<result name="input">/register.jsp</result>
            
<result name="error">/error.jsp</result>
        
</action>
        
<!-- 不使用栏拦截器下载
            <action name="download" class="com.tfc.action.DownloadAction">
            <result name="success" type="stream">
            <param name="contentType">application/rar</param>
            <param name="contentDisposition">
            filename=${filename}
            </param>
            <param name="inputName">downloadFile</param>
            </result>
            <result name="login">/login.jsp</result>
            <result name="input">/login.jsp</result>
            <result name="error">/error.jsp</result>
            </action>
-->
        
<!--使用拦截器下载 -->
        
<action name="download" class="com.tfc.action.DownloadAction">
            
<interceptor-ref name="auth" />
            
<result name="success" type="stream">
                
<param name="contentType">application/rar</param>
                
<param name="contentDisposition">
                    filename=${filename}
                
</param>
                
<param name="inputName">downloadFile</param>
            
</result>
            
<result name="login">/login.jsp</result>
            
<result name="error">/error.jsp</result>
        
</action>       
    
</package>
    
<include file="struts-default.xml"> </include> 
</struts>
页面测试:download.jsp
<%@ page language="java" pageEncoding="GB2312"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    
<head>
        
<title>四川省土壤肥料与生态建设信息网资源下载</title>
        
<!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    
-->

    
</head>
    
<%
        
int loginInfo = (Integer) request.getAttribute("loginInfo");
        
if (loginInfo == 1) {
    
%>
    
<script type="text/javascript">
              alert(
"登陆成功");
          
</script>
    
<%
        }
    
%>
    
<body>
        
<%--头部--%>
        
<table>
            
<tr>
                
<td></td>
            
</tr>
        
</table>
        
<table>
            
<tr>
                
<td>
                    ${sessionScope.username }您好!
                    
<href="u    serinfo.jsp">个人信息</a>
                
</td>
            
</tr>
        
</table>
        
<input type="hidden" name="fName" value="11.rar" />
        
<s:a href="download.action?filename=11.rar">11.rar</s:a>
        
<%--尾部--%>
        
<table>
            
<tr>
                
<td>
                    
                
</td>
            
</tr>
        
</table>
    
</body>
</html>
login.jsp
<%@ page language="java"  pageEncoding="GB2312"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
    
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    
<html>
        
<head>
        
<title>四川省土壤肥料与生态建设信息网用户登陆</title>
        
<script type="text/javascript">
            
function validate(){
                
var usernameValue = document.getElementById("username").value;
                
var passwordValue = document.getElementById("password").value;
                
if(null == usernameValue || usernameValue.length <= 0){
                    alert(
"用户帐号不能为空");
                    
return false;
                }    
                
else if(null == passwordValue || passwordValue.length <=0){
                    alert(
"用户密码不能为空");
                    
return false;
                }
                
return true;
            }
        
</script>          
        
</head>
        
<body>
            
<%--头部--%>
            
<table>
                
<tr>
                    
<td></td>
                
</tr>
            
</table>
            
<%--用户信息列表--%>
            
<table>
                
<tr>
                
<td>会员登陆</td><td>${requestScope.registerInfo}</td>
                
</tr>
            
</table>
            
<form action="login.action" method="post" onsubmit="return validate();">
                
<table border="0">
                    
<tr>
                        
<td>
                            用户帐号:
                        
</td>
                        
<td>
                            
<input type="text" name="username" id="username" />
                        
</td>                        
                    
</tr>
                    
<tr>
                        
<td>
                            用户密码:
                        
</td>
                        
<td>
                            
<input type="password" name="password" id="password" />
                        
</td>                        
                    
</tr>
                    
<tr>
                        
<td><input type="submit" name="su" value="登陆" /></td>
                        
<td><href="javascript:window.open('backPassword.jsp')">忘记密码</a></td>
                    
</tr>
                
</table>
                
<href="register.jsp" target="_blank">用户注册</a>
                
</form>
                    
<%--尾部--%>
                    
<table>
                        
<tr>
                            
<td></td>
                        
</tr>
                    
</table>
        
</body>
    
</html>

访问的地址:http:/127.0.0.1:8080/download.action
若用户没有进行登陆的时候直接访问下截地址,将跳到用户登陆对话框。现在的问题主要是在downloadaction里得不到文件的名称,那一位知道了原因请告诉我,谢谢了。我都解决好几天了.急哈!!