2015-10-18 16 views
1

我是Struts2的新手,正在開發帶有文件上傳選項的Web應用程序。一切都已配置,但是當我運行模塊HTTP狀態404錯誤:請求的資源不可用。 (鏈接:http://localhost:8080/File_Upload/index.jsp)。請在下面找到我的配置, 的index.jsp所需資源在struts2中不可用文件上傳

<%@ 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:

<?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:

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:

<?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> 

jar文件: Jar files

+0

此代碼的哪部分不起作用? –

+0

查看索引頁的一部分。 index.jsp文件。請檢查我的配置是否正確。 – kalidass

+0

您沒有針對索引頁的操作。 –

回答

0

你必須在你的struts.xml 動作添加裏面默認

<action name="index"> 
    <result>/index.jsp</result> 
</action> 

這樣,當你試圖打開http://localhost/myapp/index它將返回index.jsp