【问题标题】:File upload not working with Struts2 Model-Driven Interface文件上传不适用于 Struts2 模型驱动接口
【发布时间】:2018-05-09 15:55:29
【问题描述】:

当我使用模型驱动接口时,我的文件上传操作不起作用。

它没有弹出任何错误,也没有生成任何日志。

特此附上我的代码,

我想知道对于文件,我们是否必须在 Model 或具有 Model-Driven 接口的 Action 中生成它的 getter/setter。

至少应该上传到 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;
    }

}

Model ProductModel.java

包 com.fileupload.model;

导入 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>

【问题讨论】:

    标签: struts2 ognl model-driven


    【解决方案1】:

    当然,您的文件上传操作在您使用模型时可以正常工作 驱动界面,但你犯了一些错误:

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

    如果您根据我的回答实现了该功能,请回复 我,微笑。

    【讨论】:

    • 谢谢,正在更新 ContentType 并设置最大大小
    猜你喜欢
    • 2014-09-13
    • 2013-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-02
    • 2015-03-31
    • 1970-01-01
    相关资源
    最近更新 更多