【问题标题】:Struts 2 Jquery File Upload [duplicate]Struts 2 Jquery文件上传[重复]
【发布时间】:2016-09-11 07:26:08
【问题描述】:

我是struts2的初学者。我正在尝试使用 Struts2 Jquery 插件在 struts 2 中上传文件。我不希望页面在文件上传时刷新。我相信 Struts 2 jquery 有助于在不刷新页面的情况下创建 ajax 操作请求,但尽管如此,页面还是会刷新。如果我错了,请帮助纠正我或建议任何替代方法来上传文件。以下是我目前的程序:

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>File Upload</title>
<script type="text/javascript" src="jquery-1.12.3.js"></script>
</head>
<body>
<form action="upload" method="post" enctype="multipart/form-data">
      <label for="myFile">Upload your file</label>
      <input type="file" name="myFile" />
      <sj:submit value="Submit Form" targets="myAjaxTarget"/>
   </form>
<div id="myAjaxTarget">
    </div>
</body>
</html>

Struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
   <constant name="struts.devMode" value="true" />
   <constant name="struts.multipart.maxSize" value="1000000" />

   <package name="default" extends="struts-default">
   <action name="upload" class="com.upload.FileUpload">
      <result name="success">index.jsp</result>
       <!--  <result name="error">/error.jsp</result> -->
   </action>
   </package>
</struts>

文件上传操作

package com.upload;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

import com.opensymphony.xwork2.ActionSupport;

public class FileUpload extends ActionSupport {
    private File myFile;
    private String myFileContentType;
    private String myFileFileName;
    private String destPath;

    public String execute() {
        /* Copy file to a safe location */
        destPath = "C:/apache-tomcat-6.0.33/work/";

        try {
            System.out.println("Src File name: " + myFile);
            System.out.println("Dst File name: " + myFileFileName);

            File destFile = new File(destPath, myFileFileName);
            FileUtils.copyFile(myFile, destFile);

        } catch (IOException e) {
            e.printStackTrace();
            return ERROR;
        }

        return SUCCESS;
    }

    public File getMyFile() {
        return myFile;
    }

    public void setMyFile(File myFile) {
        this.myFile = myFile;
    }

    public String getMyFileContentType() {
        return myFileContentType;
    }

    public void setMyFileContentType(String myFileContentType) {
        this.myFileContentType = myFileContentType;
    }

    public String getMyFileFileName() {
        return myFileFileName;
    }

    public void setMyFileFileName(String myFileFileName) {
        this.myFileFileName = myFileFileName;
    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <display-name>Struts 2</display-name>
   <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>
   <filter>
      <filter-name>struts2</filter-name>
      <filter-class>
         org.apache.struts2.dispatcher.FilterDispatcher
      </filter-class>
   </filter>

   <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
   </filter-mapping>
</web-app>

我在浏览器控制台中遇到的错误是:

Uncaught TypeError: Cannot read property 'bind' of undefined    upload:23

这是错误所在:

Error

【问题讨论】:

    标签: java jquery ajax struts2 struts2-jquery


    【解决方案1】:

    在您的 JSP 代码中,您错过了添加 &lt;sj:head&gt;-标签。并且你不需要自己添加 jQuery,&lt;sj:head&gt; 会处理的。

    这里有更多通用项目示例:link

    我相信 Struts 2 jquery 有助于在不刷新页面的情况下创建 ajax 操作请求[...]

    我不这么认为。文件上传是一种特殊情况,该插件目前未处理。几周前我已经回答了一个类似的问题,也许可以看看另一个example

    【讨论】:

    • 我还没有阅读整个问题,但缺少&lt;sj:head /&gt; 很可能是所有事情发生的罪魁祸首。 +1
    • 我添加了 并且可以看到它添加了所有 js 文件本身。可以建议任何我可以在struts 2中实现ajax文件上传的方法,因为我不希望页面刷新。
    • 嘿@Pragya,在另一个question 中有一个异步文件上传的完整示例(即使没有struts2-jquery 插件),请看一下。从action到js都有你需要的,
    猜你喜欢
    • 1970-01-01
    • 2014-10-17
    • 2013-05-13
    • 2011-12-14
    • 1970-01-01
    • 1970-01-01
    • 2017-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多