[英]The requested resource is not available in struts2 File upload
I am new to Struts2 and am developing a web application with file upload option. 我是Struts2的新手,正在开发带有文件上传选项的Web应用程序。 Everything has been configured but when i run the module HTTP Status 404 error: The requested resource is not available.
一切都已配置,但是当我运行模块HTTP Status 404错误时:请求的资源不可用。 (Link:
http://localhost:8080/File_Upload/index.jsp
). (链接:
http://localhost:8080/File_Upload/index.jsp
)。 please find below my config, index.jsp 请在我的配置下找到index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-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=UTF-8">
<title>File Upload</title>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery-1.11.3.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/ajaxfileupload.js"></script>
<script type="text/javascript">
function ajaxFileUpload()
{
$("#loading")
.ajaxStart(function(){
$(this).show();
})//
.ajaxComplete(function(){
$(this).hide();
});//
$.ajaxFileUpload
(
{
url:'fileUploadAction.action',//
secureuri:false,//false
fileElementId:'myFile',//id <input type="file" id="file" name="file" />
dataType: 'json',// json
success: function (data, status) //
{
//alert(data.message);//jsonmessage,messagestruts2
$("#file_name").attr("value",data.myFile);
$("#file_path").attr("value",data.myFileFileName);
if(typeof(data.error) != 'undefined')
{
if(data.error != '')
{
alert(data.error);
}else
{
alert(data.myFile);
}
}
},
error: function (data, status, e)//
{
alert(e);
}
}
)
return false;
}
</script>
</head>
<body>
<form action="upload" method="post" enctype="multipart/form-data">
<label for="myFile">Upload your file</label>
<input type="file" name="myFile" id="myFile"/>
<input type="button" value="" onclick="return ajaxFileUpload();">
<input type="hidden" name="file_name[]" id="file_name[]" value=""/>
<input type="hidden" name="file_path[]" id="file_path[]" value=""/>
<input type="submit"/>
</form>
<div id="loading"><span style="position: fixed;left: 50%;top: 50%; transform: translate(-50%, -50%); color:#f3cbbb ">
<img src="<%=request.getContextPath()%>/images/ajax-loader.gif" alt="loading" width="200" height="200" style="padding-left:85px">
<br /><br />
<span style=" font-weight: 700; font-size: 18px">Please wait,File is being uploaded
</span></span>
</div>
</body>
</html>
struts.xml: 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>
<package name="default" extends="struts-default">
<action name="fileUploadAction" class="com.tutorialspoint.struts2.MultipleFileUploadAction">
<result type="json" name="success">
<param name="contentType">text/html</param>
</result>
<result type="json" name="error">
<param name="contentType">text/html</param>
</result>
</action>
</package>
</struts>
Action.class: Action.class:
package com.tutorialspoint.struts2;
import java.io.File;
import org.apache.commons.io.FileUtils;
import java.io.IOException;
import com.opensymphony.xwork2.ActionSupport;
public class MultipleFileUploadAction extends ActionSupport {
private File[] myFile;
private String[] myFileContentType;
private String[] myFileFileName;
private String destPath;
public String execute()
{
/* Copy file to a safe location */
destPath = "E:/kalidass/Upload/";
try{
for(int i=0;i<myFile.length;i++)
{
System.out.println("Src File name: " + myFile[i]);
System.out.println("Dst File name: " + myFileFileName[i]);
File destFile = new File(destPath, myFileFileName[i]);
FileUtils.copyFile(myFile[i], 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: web.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
You have to add in your struts.xml this action inside your default package 您必须在默认包中的struts.xml文件中添加此操作
<action name="index">
<result>/index.jsp</result>
</action>
In this way when you'll try to open http://localhost/myapp/index it will return the index.jsp
这样,当您尝试打开http:// localhost / myapp / index时 ,它将返回
index.jsp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.