File upload not working with Struts2 Model-Driven Interface

371 Views Asked by At

When I am using Model driven interface, my file upload operation is not working.

It is not popping any error, nor it is generating any log.

My code is attached hereby,

I want to know for file, do we have to generate its getters/setters in Model or Action with Model-Driven interface.

Atleast it should get uploaded to temp folder, as with struts by default uploads to temp.

Action 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;
    }

}

Model ProductModel.java

package 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;
    }
}

Configuration 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>

View 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

There are 1 best solutions below

1
On

Of,course,your file upload operation can working When you using Model driven interface but you made a few mistake:

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

And then,you need to add a piece of code to your code for upload like this:

@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";
    }
}

if you implements the function based on my answer,please reply to me,smile.