Struts 2 Jquery文件上传

时间:2016-09-11 07:26:09

标签: java jquery ajax struts2 struts2-jquery

我是struts2的初学者。我正在尝试使用Struts2 Jquery插件在struts 2中上传文件。我不希望页面在文件上传时刷新。我相信Struts 2 jquery有助于在不刷新页面的情况下创建aa 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>

FileUpload操作

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

1 个答案:

答案 0 :(得分:1)

在您的JSP代码中,您错过了添加<sj:head> - 标记。而且你不需要自己添加jQuery,<sj:head>会处理它。

以下是一般用法项目的更多示例:link

  

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

我不这么认为。文件上传是一种特殊情况,目前在该插件中没有处理。几个星期前我回答了类似的问题,或许可以看一下example