Access javascript data from Java (maven application) -
i hope right subforum post this. i'm new maven, vaadin , java applications in general, hoped me i'm not sure what's best way go it. have maven project (java 7) which, using javascript, creates popup window form inside, allows upload file, display content in textarea , send (the content of file string) server via ajax request. easy part. want access data sent through ajax (the string containing data of uploaded file) in java because need run through validation. had around, including book of vaadin, , net in general , considering have never done before, seems 1 way have connector, looks little complicated , appears - understand book of vaadin https://vaadin.com/docs/-/part/framework/gwt/gwt-overview.html - won't able implement in project given structure have - different what's in there. so, question guys is, given project have (just normal maven project) easiest way me access data java? here code project, put things context:
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 main div label label = new label("hello vaadin user. use application upload files."); ... //here javascript creating , instantiating popup , 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 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 popup +"$('.popuptriggerbtn').click(function(){" +"/*console.log('button clicked!');*/" +"var $body = $('body');" +"$htmlpopup.appendto($body);" +"});" //here 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 file string*/;" +"/*console.log('here file 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 ' + responsedata);" +"console.log('text status ' + textstatus);" +"console.log('the data submitted ' + 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); }
link pastebin here http://pastebin.com/msejq0ht so, postdata contains string passed server , i'd access in java. came across earlier on, may or may not way deal vaadin ajax. guys think? appreciated, thanks
well, firstly need express deep concern using wrong tools in strange way achieve desired effect. won't go topic lets indicate main purpose of vaadin framework allow developer write components in java (similarly swing components) avoid javascript mess. yes, can run javascript using vaadin framework should avoid long possible.
ok lets hacking. have 2 options catch file data (which string said) server:
a) rid ofconstructing xmlhttprequest hand. let vaadin handle you. instead of
+"$.ajax({" +"type:'post'," +"url:'http://localhost:8080/apptest/'," ...
just call javascript function, lets say
sendfilecontenttotheserver(postdata)
now next thing need register javascript callback on server side (read: vaadin). somewhere in code (doesn't matter where, make sure code called @ least once - ideally once) put:
javascript.getcurrent().addfunction("sendfilecontenttotheserver", new javascriptfunction() { public void call(jsonarray arguments) throws jsonexception { system.out.println(arguments.getstring(0)); //do whatever want data - under arguments.getstring(0) } });
that's it. vaadin manage rpc call you. page won't reloaded , data on server side.
b) second approach it's way more tricky. technically possible construct xmlhttprequest hand , receive data on server using vaadin. need register javaservlet (not vaadinservlet). send data javaservlet. through proxy or reference call existed vaadinui. it's compilcated way of doing things you've made tricky won't go deeper that.
Comments
Post a Comment