4

我正在尝试使用 struts2 上传文件。

在jsp页面中有一个上传文件的按钮和一个Submit按钮。程序中没有显示错误消息,但是当我单击Submit按钮时没有任何反应。请参阅下面的代码:

动作类

package com.scrolls.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.io.File;
import java.util.Map;
import org.apache.commons.io.FileUtils;

public class UploadAction extends ActionSupport {

  private File upload;
  private String uploadContentType;
  private String uploadFileName;

  public String fileUpload() {
      try {
          String fullFileName = "c:/sample/mystruts/myfile.txt";
          File theFile = new File(fullFileName);
          FileUtils.copyFile(upload, theFile);
      } catch (Exception e) {
          System.out.println(e.toString());
          return ERROR;
      }

      return SUCCESS;
  }

  // Plus public getters/setters for upload properties.
}

JSP

<%@taglib uri="/struts-tags" prefix="s" %>
<html>
    <head>
        <s:head theme="ajax" />
    </head>

    <body>
        <s:form action="doUpload.action" enctype="multipart/form-data"/>
        <s:datetimepicker name="date" displayFormat="yyyy-MM-dd" />
        <s:file name="upload" />
        <s:submit value="submit"/>
    </body>
</html>

Struts 配置

<struts>
  <package name="register3" extends="struts-default">
    <action name="doUpload" class="com.scrolls.action.UploadAction" method="fileUpload">
      <result name="success">/suc.jsp</result>
      <result name="error">/fail.jsp</result>
    </action>
  </package>
</struts>
4

1 回答 1

3

你有一个自我关闭的表格......

试试这样:

<s:form action="doUpload.action" 
        method="POST" 
        enctype="multipart/form-data" >
    <s:datetimepicker label="Select Date" 
                     name="date" 
                     displayFormat="yyyy-MM-dd"  
                     required="true" />
    <s:file label="File:" name="upload" />
    <s:submit value="submit" />
</s:form>

感谢 Anu 的更正,+1...

于 2012-10-26T09:53:02.780 回答