2016-09-11 35 views
0

私はstruts2の初心者です。 Struts2 Jqueryプラグインを使用してstruts 2にファイルをアップロードしようとしています。私はファイルのアップロード時にページをリフレッシュさせたくありません。私はStruts 2 jqueryがページをリフレッシュすることなくアクションにajaxリクエストを作成するのに役立つと信じていますが、これにもかかわらずページはリフレッシュされます。私が間違っている場合は、私に訂正するか、ファイルをアップロードするための別の方法を提案してください。以下は、私の現在のプログラムです:Struts 2 Jquery File Upload

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; 
    } 
} 

は、私は、ブラウザのコンソールで取得していますエラーが

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

あるのweb.xml:

Uncaught TypeError: Cannot read property 'bind' of undefined upload:23 

これがどこにあるか、このエラーの土地:

Error

答えて

1

JSPコードでは、<sj:head>タグを追加していませんでした。 jQueryを自分で追加する必要はありません。<sj:head>がそれを処理します。ここで

は、一般的な使用のためのプロジェクトの多くの例があります:link

私は

[...] Strutsの2 jqueryのは、ページを更新せずにアクションにAJAXリクエストを作成するのに役立ちます信じています私はそうは思わない。ファイルのアップロードは特別なケースです。現在、このプラグインでは処理されていません。私はちょうど数週間前に同様の質問に答えました。おそらく別のexampleのためにそれを見てください。

+0

私は全体の質問を読んだことはありませんが、欠落している「」は、すべてが起こっている可能性が最も高いです。 +1 –

+0

私はを追加し、それがすべてのjsファイル自体を追加したのを見ることができました。私は、ページをリフレッシュしたくないので、私がstruts 2でajaxファイルのアップロードを実装する方法を提案できます。 – Pragya

+0

ちょっと@Pragya、他の[質問](https://stackoverflow.com/questions/39207207/how-to-retrieve-uploaded-file-using-ajax-on-java-server-side/39221365#39221365)非同期ファイルアップロード(struts2-jqueryプラグインなしでも)の例があります。あなたがアクションからjsまで必要なものはすべてありますが、 – beendr