Я новичок в стойках2. Я пытаюсь загрузить файл в struts 2 с помощью плагина Struts2 JQuery. Я не хочу, чтобы страница обновлялась при загрузке файла. Я считаю, что Struts 2 jquery помогает создать aa ajax-запрос к действию без обновления страницы, но, несмотря на это, страница обновляется. Если я ошибаюсь, помогите мне исправить или предложить альтернативный метод для загрузки файла. Ниже моя текущая программа:Struts 2 JQuery Загрузка файла
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
это где эта земля ошибка:
Я не прочитал весь вопрос, но недостающий ' ', скорее всего, является виновником всего происходящего. +1 –
Я добавил и мог видеть, что он добавил все файлы js. Может предложить любой метод, через который я могу реализовать загрузку файла ajax в struts 2, потому что я не хочу, чтобы страница обновлялась. –
Pragya
Эй @ Прага, в другом [вопрос] (https://stackoverflow.com/questions/39207207/how-to-retrieve-uploaded-file-using-ajax-on-java-server-side/39221365#39221365) есть целый пример для загрузки файла async (даже без плагина struts2-jquery), просто взгляните. Есть все, что вам нужно от действия до js, – beendr