一尘不染

Struts2文件上传

jsp

我想以一种形式上传多个文件,而文件的数量因用户而异,例如。一个用户可以上传2个文件,另一个用户可以上传12个文件。我想知道如何创建jsp
struts2形式的循环或其他内容,以便当用户上传文件时,它将其名称添加到列表中,并且他/她可以上传另一个文件并添加到列表中,依此类推。


阅读 240

收藏
2020-06-08

共1个答案

一尘不染

我通过使用两个Struts2插件Struts2 JQuery插件和Struts2 Scope插件来实现此目的

答案如下:

在您的Maven POM文件中包括这些依赖项:

        <dependency>
            <groupId>com.jgeppert.struts2.jquery</groupId>
            <artifactId>struts2-jquery</artifactId>
            <version>3.2.0</version>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>com.googlecode.struts2scopeplugin</groupId>
            <artifactId>struts2-scope-plugin</artifactId>
            <version>1.0.4</version>
        </dependency>

我们创建一个类来准备表单,如下所示:

 public class PrepareUpload extends ActionSupport{
     @In (scope=ScopeType.CONVERSATION)
      @Out (scope=ScopeType.CONVERSATION)
      private List<File> fileUploadi = new ArrayList<File>();

      @In (scope=ScopeType.CONVERSATION)
      @Out (scope=ScopeType.CONVERSATION)
        private List<String> fileUploadContentTypei = new ArrayList<String>();

      @In (scope=ScopeType.CONVERSATION)
      @Out (scope=ScopeType.CONVERSATION)
        private List<String> fileUploadFileNamei = new ArrayList<String>();

    @Action(value = "/prepareupload", results = {
            @Result(location = "upload.jsp", name = "success")
          })
          @End
    public String execute() throws Exception

      {
        return SUCCESS;

      }

    public List<File> getFileUploadi() {
        return fileUploadi;
    }

    public void setFileUploadi(List<File> fileUploadi) {
        this.fileUploadi = fileUploadi;
    }

    public List<String> getFileUploadContentTypei() {
        return fileUploadContentTypei;
    }

    public void setFileUploadContentTypei(List<String> fileUploadContentTypei) {
        this.fileUploadContentTypei = fileUploadContentTypei;
    }

    public List<String> getFileUploadFileNamei() {
        return fileUploadFileNamei;
    }

    public void setFileUploadFileNamei(List<String> fileUploadFileNamei) {
        this.fileUploadFileNamei = fileUploadFileNamei;
    }

}

以及用于添加到列表和处理ajax调用的类,如下所示:

public class UploadAction extends ActionSupport {

  private static final long serialVersionUID = 7968544374444173511L;
  private static final Log  log              = LogFactory.getLog(UploadAction.class);


  private String            echo;
  @In (scope=ScopeType.CONVERSATION)
  @Out (scope=ScopeType.CONVERSATION)
  private List<File> fileUploadi = new ArrayList<File>();
  private List<File> fileUpload = new ArrayList<File>();
  @In (scope=ScopeType.CONVERSATION)
  @Out (scope=ScopeType.CONVERSATION)
    private List<String> fileUploadContentTypei = new ArrayList<String>();
  private List<String> fileUploadContentType = new ArrayList<String>();
  @In (scope=ScopeType.CONVERSATION)
  @Out (scope=ScopeType.CONVERSATION)
    private List<String> fileUploadFileNamei = new ArrayList<String>();
  private List<String> fileUploadFileName = new ArrayList<String>();
  @Action(value = "/uploada", results = {
    @Result(location = "simpleecho.jsp", name = "success")
  })
  @Begin
  public String execute() throws Exception
  {

fileUploadi.addAll( fileUpload);
fileUploadFileNamei.addAll(fileUploadFileName);
fileUploadContentTypei.addAll(fileUploadContentType);
   for (int i = 0;  i < fileUploadFileNamei.size();  i++)
   {
       if (echo == null)
           echo =   fileUploadFileNamei.get(i);
       else
      echo +=  "<br>" + fileUploadFileNamei.get(i);
    log.info(echo);
   }
    return SUCCESS;
  }

  public String getEcho()
  {
    return echo;
  }


  public List<File> getFileUpload() {
        return fileUpload;
    }

    public void setFileUpload(List<File> fileUpload) {
        this.fileUpload = fileUpload;
    }

    public List<String> getFileUploadContentType() {
        return fileUploadContentType;
    }

    public void setFileUploadContentType(List<String> fileUploadContentType) {
        this.fileUploadContentType = fileUploadContentType;
    }

    public List<String> getFileUploadFileName() {
        return fileUploadFileName;
    }

    public void setFileUploadFileName(List<String> fileUploadFileName) {
        this.fileUploadFileName = fileUploadFileName;
    }

    public List<File> getFileUploadi() {
        return fileUploadi;
    }

    public void setFileUploadi(List<File> fileUploadi) {
        this.fileUploadi = fileUploadi;
    }

    public List<String> getFileUploadContentTypei() {
        return fileUploadContentTypei;
    }

    public void setFileUploadContentTypei(List<String> fileUploadContentTypei) {
        this.fileUploadContentTypei = fileUploadContentTypei;
    }

    public List<String> getFileUploadFileNamei() {
        return fileUploadFileNamei;
    }

    public void setFileUploadFileNamei(List<String> fileUploadFileNamei) {
        this.fileUploadFileNamei = fileUploadFileNamei;
    }




    }

最后,jsp应该类似于:

<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>

<html>
<head>
<sj:head jqueryui="true" defaultLoadingText="Please wait ..."/>
<s:head />
</head>

<body>
<h1>File Upload:</h1>

<s:form action="uploada" 
method="POST" enctype="multipart/form-data" theme="xhtml" >

<s:file label="File:" name="fileUpload" size="40" />

 <div id="result" >Add Files Below:</div>
<sj:submit 
            targets="result" 
            button="true" 
            validate="true" 
            value="Add File" 
            indicator="indicator"
            parentTheme="xhtml"
            />
<sj:submit 
            button="true" 
            validate="true" 
            value="Submit" 
            indicator="indicator"
            parentTheme="xhtml"
            />

</s:form>

</body>
</html>
2020-06-08