1

我在 SO 上阅读了有关此问题的所有问题,并仔细遵循了官方 Struts 2 文件上传文档,但我仍然遇到问题。动作完成没有错误,但我似乎无法捕捉上传的文件,因为动作中的 3 个文件属性始终为空。就像 fileUploadInterceptor 没有完成它的工作一样。这是我的代码:

动作映射:

<action name="merchantSaveOrUpdate" class="merchantSaveOrUpdateAction">
    <interceptor-ref name="fileUpload">
        <param name="allowedTypes">image/jpeg,image/gif,image/png</param>
    </interceptor-ref> 
    <result name="success" type="redirectAction">merchantList</result>
</action>

jsp:

<s:form action="merchantSaveOrUpdate" method="POST" enctype="multipart/form-data">
    <label>Merchant Name</label> <input type="text" value="${merchant.name}" name="name"><br />
    <label>Merchant Logo</label> <s:file name="logo" /> <br />
    <s:submit class="btn submit" />
</s:form>

行动:

public class MerchantSaveOrUpdateAction extends ActionSupport {

    private File logo;
    private String logoContentType;
    private String logoFileName;
    private String name;

    public File getLogo () {
        return logo;
    }

    public void setLogo ( File logo ) {
        this.logo = logo;
    }

    public String getLogoContentType () {
        return logoContentType;
    }

    public void setLogoContentType ( String logoContentType ) {
        this.logoContentType = logoContentType;
    }

    public String getLogoFileName () {
        return logoFileName;
    }

    public void setLogoFileName ( String logoFileName ) {
        this.logoFileName = logoFileName;
    }

    public String getName () {
        return name;
    }

    public void setName ( String name ) {
        this.name = name;
    }

    public String execute() throws Exception {

        String result = super.execute();

        // the problem is here - name is populated, but the 3 logo properties are null

        return result;
    }
}
4

2 回答 2

2

当你为一个动作定义拦截器时,你必须为那个动作定义所有的拦截器。

换句话说,您的操作当前只有一个拦截器正在运行,即文件拦截器。

请注意,链接页面上的所有配置均不包括文件拦截器。

包括defaultStack文件拦截器。动作可以这样配置:

<action name="merchantSaveOrUpdate" class="merchantSaveOrUpdateAction">
    <interceptor-ref name="defaultStack">
        <param name="fileUpload.allowedTypes">image/jpeg,image/gif,image/png</param>
    </interceptor-ref> 
    <result name="success" type="redirectAction">merchantList</result>
</action>
于 2013-01-09T13:33:43.580 回答
0

关于您的问题,您可以在 struts.xml 中包含文件 upload.xml。

<struts>

    <include file="fileupload.xml"></include>
   <package name="default" extends="struts-default">
        <!-- file upload  -->
        <action name="upload" class="com.action.FileUpload">
    <result  name="success">success.jsp</result>
        <result name="error">error.jsp</result>
        </action>

    </package>
</struts>

此文件 upload.xml 应包含拦截器引用...

<struts>
    <package name="fileUploadPackage" extends="struts-default">
        <interceptors>
                      -------------
                      -------------
      <interceptor-ref name="fileUpload">
        <param name="maximumSize">your file size</param>
    <param name="allowedTypes">image/jpeg,image/gif,image/png</param>
    </interceptor-ref>
                     ------------------
                     ------------------
                </interceptors>


    </package>
</struts>

也可以通过其他方式完成,请检查此 url文件上传

于 2013-01-10T05:57:58.787 回答