これを投稿するには、これが正しいサブフォーラムであることを願っています。 私は、一般的に、Maven、Vaadin、Javaアプリケーションの新機能ですから、これについて私が助けてくれることを願っています。 基本的に私はjavascriptを使ってポップアップウィンドウを作成し、ファイルをアップロードし、その内容をテキストエリアに表示して(ファイルの内容を文字列として)送ることができるmavenプロジェクト(java 7)を持っています)をajaxリクエストを介してサーバーに送信します。それは簡単な部分でした。 私が今したいことは、Javaでいくつかの検証を実行する必要があるため、ajax(アップロードされたファイルのデータを含む文字列)を介して送信されたデータにアクセスすることです。 私はvaadinの本や一般的なネットを含めて周りの見栄えが良く、かつてこれをやったことがないと考えると、ある方法ではコネクタを持つことができるようですが、それは、私が持っている構造を考えれば私のプロジェクトでそれを実装することができないことを、私がバダンの書物https://vaadin.com/docs/-/part/framework/gwt/gwt-overview.htmlの本から理解しているところから現れます。 私の質問は皆さん、私が持っているプロジェクト(普通のメイプルプロジェクト)を見れば、Javaからこのデータにアクセスする最も簡単な方法は何でしょうか?ここで は、コンテキストに物を置くためのプロジェクトからいくつかのコードです:Javaからのjavascriptデータへのアクセス(mavenアプリケーション)
import javax.servlet.annotation.WebServlet;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.annotations.Widgetset;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.JavaScript;
import com.vaadin.ui.Label;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.client.ui.*;
@Theme("mytheme")
@Widgetset("my.vaadin.apptest.MyAppWidgetset")
@com.vaadin.annotations.JavaScript({"https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"
})
public class MyUI extends UI {
@Override
protected void init(VaadinRequest vaadinRequest) {
final VerticalLayout layout = new VerticalLayout();
layout.addStyleName("myLayout");//add class to main div
Label label = new Label("Hello Vaadin user. Use this application to upload files.");
...
//HERE IS THE JAVASCRIPT CREATING AND INSTANTIATING THE POPUP AND THE AJAX CALL
//CREATING POPUP
JavaScript.getCurrent().execute(""
+"var $HTMLpopup = $('<div class=\"popupContainer\">"
+"<span class=\"cancelBtn big\"></span>"
+"<div class=\"wrapper\">"
+"<form action=\"\" id=\"fileForm\">"
+"<div class=\"mask\">"
+"<input type=\"file\" title=\" \"name=\"uploadFile\" class=\"uploadFile\" accept=\".mol,.sdf\">/*filters files to upload*/"
+"<span class=\"pseudoBtn\">Browse</span>"
+"<input type=\"text\" name=\"displayFile\" class=\"displayFile\" placeholder=\"no file loaded\">"
+"<span class=\"cancelBtn small\"></span>"
+"</div>"
+"<textarea class=\"fileResult\"></textarea>"
+"<button type=\"submit\" class=\"submitBtn\">Upload</button>"
+"<div class=\"clear\"></div>"
+"</form>"
+"</div>"
+"</div>');"
//INSTANTIATING THE POPUP
+"$('.popupTriggerBtn').click(function(){"
+"/*console.log('button clicked!');*/"
+"var $body = $('body');"
+"$HTMLpopup.appendTo($body);"
+"});"
//HERE IS THE AJAX BIT
+"var $submitBtn = $HTMLpopup.find('.submitBtn');"
+"$submitBtn.click(function(e){"
+"e.preventDefault();/*prevent submission*/"
+"if(isFileUploadEmpty()){/*IF EMPTY*/"
+"/*alert('submit clicked');*/"
+"removeError();"
+"showError('empty');"
+ "}"
+"else{/*IF NOT EMPTY*/"
+"/*AJAX OPS*/"
+"if (window.XMLHttpRequest){/*XMLHttpRequest SUPPORT?*/"
+"console.log('XMLHttpRequest supported!');"
+"var postData = returnFileAsString();/*returns the file as a string*/;"
+"/*console.log('here is the file as a string ' + postData);*/"
+"$.ajax({"
+"type:'post',"
+"url:'http://localhost:8080/apptest/',"
+"data:postData,"
+"contentType: 'application/x-www-form-urlencoded',"
+"success: function(responseData, textStatus, jqXHR){"
+"/*alert('data saved');*/"
+"console.log('responseData is ' + responseData);"
+"console.log('text status is ' + textStatus);"
+"console.log('the data submitted is ' + postData);"
+"},"
+"error: function(jqXHR, textStatus, errorThrown){"
+"console.log(errorThrown);"
+"alert('an error has occurred!');"
+"}"
+"});"
+"}"
+"}"
+"});"
+"");
//ADDING COMPONENTS
layout.addComponents(label, button);
layout.setMargin(true);
layout.setSpacing(true);
setContent(layout);
}
リンクをだからここhttp://pastebin.com/mSEJq0HT をペーストビンし、POSTDATAは、私は、サーバーに渡され、私はJavaでアクセスしたいのですが、その文字列が含まれています。 私は先にこれを見つけました。それはそれを扱う別の方法かもしれません。vaadin with ajax。皆さんはどう思いますか? ご協力いただきありがとうございます。ありがとうございました。ありがとうございます。
Vaddinクライアントを混乱させないでください。<->サーバー通信。 java側の対応するフィールドに 'valueChangeListener'を追加するだけで、値が変更されたときに通知されます。 –
これは削減された例ですか?そうでない場合は、その時点でvaadinを気にするのはなぜですか?あなたがしているのは、ファイルを別のエンドポイントに送信するためにいくつかのjqueryを操作するために、さもなければ、なぜvaadin Window、Upload、TextArea ...を使用しないのですか? – cfrick
すべての有効なポイントの人たちですが、なぜ私が学習しているので、私がvaadinを使用している理由はJSとは少しばかり自信があります。それはアップロード、textAreasなどでより良いと同意するが、それは私が少し自信を持って感じた後、それをリファクタリングするときに来る。今のところ、私はRPCなどの使用の原則を理解すると思っていたので、それが最良の方法ではないにしても、この方法でやっています。 – antobbo