2017-11-26 6 views
-1

モデルドリブンインターフェイスを使用しているとき、ファイルアップロード操作が機能しません。Struts2モデルドリブンインターフェイスでファイルアップロードが動作しません

エラーが発生しておらず、ログも生成されていません。

私のコードはここに添付され、

私は、ファイルのために知ってほしい、私たちはモデル駆動型のインタフェースを使用したモデルまたはアクションでそのゲッター/セッターを生成する必要があります。

atleastはtempフォルダにアップロードする必要があります。これはstrutsの場合と同様に、デフォルトでtempにアップロードされます。

アクション ProductAction.java

package com.fileupload.action; 

import com.opensymphony.xwork2.ActionSupport; 
import com.opensymphony.xwork2.ModelDriven; 
import com.fileupload.model.FileUploadModel; 

/** 
* Action class to requests 
* @package com.fileupload.action 
*/ 
public class FileUploadAction extends ActionSupport implements ModelDriven<Object>{ 

    FileUploadModel fileModel = new FileUploadModel(); 
    @Override 
    public Object getModel() { 
     return fileModel; 
    } 

} 

モデルProductModel.java

パッケージcom.fileupload.model。

import java.io.File;当然の

/** 
* Model class to handle data 
* @package com.fileupload.model 
*/ 
public class FileUploadModel { 

    private String employeeName; 
    private File image; 
    private String imageFileName; 
    private String imageFileCotentType; 

    public File getImage() { 
     return image; 
    } 

    public void setImage(File image) { 
     this.image = image; 
    } 

    public String getImageFileName() { 
     return imageFileName; 
    } 

    public void setImageFileName(String imageFileName) { 
     this.imageFileName = imageFileName; 
    } 

    public String getImageFileCotentType() { 
     return imageFileCotentType; 
    } 

    public void setImageFileCotentType(String imageFileCotentType) { 
     this.imageFileCotentType = imageFileCotentType; 
    } 

    public String getEmployeeName() { 
     return employeeName; 
    } 

    public void setEmployeeName(String employeeName) { 
     this.employeeName = employeeName; 
    } 
} 

設定struts.xml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE struts PUBLIC 
     "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" 
     "http://struts.apache.org/dtds/struts-2.5.dtd"> 
<struts> 
    <constant name="struts.devMode" value="true"/> 
    <package name="FileUploadStruts" extends="struts-default"> 
     <action name="index"> 
      <result>/index.jsp</result> 
     </action> 
     <action name="submitForm" class="com.fileupload.action.FileUploadAction"> 
      <result name="success">/result.jsp</result> 
     </action> 
    </package> 
</struts> 

ビューCreateProduct.jsp

<%-- 
    Document : index 
    Created on : Nov 26, 2017, 12:50:38 PM 
    Author  : owner 
--%> 

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<%@ taglib prefix="s" uri="/struts-tags" %> 
<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>File upload example</title> 
    </head> 
    <body> 
     <h1>Fill the form below</h1> 
     <s:form action="submitForm" enctype="multipart/form-data" name="employeeform"> 
      <s:textfield name="employeeName"/> 
      <s:file name="image"/> 
      <s:submit value="Submit"/> 
     </s:form> 
    </body> 
</html> 

答えて

1

あなたがモデル を使用する場合は、あなたのファイルのアップロード操作が作業することができますあなたはいくつかのミスマックを作ったE:

public class FileUploadModel { 
    private String employeeName; 
    private File image; 
    private String imageFileName; 
    // YourCode was wrong : private String imageFileCotentType; 
    private String imageFileContentType; 
(get set)... 

そして、あなたは このようなアップロードのためのあなたのコードにコードの一部を追加する必要があります:あなたはベースの機能を実装している場合

@Namespace("/") 
@ParentPackage("struts-default") 
public class FileUploadAction extends ActionSupport implements ModelDriven<FileUploadModel> { 
    private static final long serialVersionUID = 1L; 
    FileUploadModel fileModel = new FileUploadModel(); 
    @Override 
    public FileUploadModel getModel() { 
     return fileModel; 
    } 
    @Action(value = "submitForm", results = { 
      @Result(name = "success", location = "/result.jsp") }) 
    public String submitForm() { 
     HttpServletRequest request = ServletActionContext.getRequest(); 
     String tomcatPath = ServletActionContext.getServletContext().getRealPath("/"); 
     String projectName = request.getContextPath(); 
     projectName = projectName.substring(1); 
     String filePath = tomcatPath.substring(0, tomcatPath.indexOf(projectName)) + "uploadFile"; 
     File dest = new File(filePath, fileModel.getImageFileName()); 
     try { 
      FileUtils.copyFile(fileModel.getImage(), dest); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return "success"; 
    } 
} 

私の答えは、 私に笑顔に返信してください。

+0

ありがとうございます。ContentTypeの更新と最大サイズの設定 –

関連する問題