gpt4 book ai didi

java - Struts 2 Jquery 文件上传

转载 作者:行者123 更新时间:2023-12-01 09:30:44 25 4
gpt4 key购买 nike

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

最佳答案

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

以下是该项目的更多通用示例:link

I believe Struts 2 jquery helps to create an ajax request to action without refreshing the page[...]

我不这么认为。文件上传是一种特殊情况,目前该插件尚未处理。几周前我回答过类似的问题,也许再看一下example .

关于java - Struts 2 Jquery 文件上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39433817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com