在javafx8使用ajax
js代码:
function getCheckDate() {
var request=new XMLHttpRequest();
var url=document.getElementById("ctx").value+"/user/list";
request.open("POST",url,false);
request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
request.send("");
var charString;
charString=request.responseText;
return charString;
}
getCheckDate();
java代码:
public class QueryProductionStatus extends Application {
private static final WebView browser = new WebView();
private static final WebEngine webEngine = browser.getEngine();
private Proc proc;
private static String[] parame;
/**
* 数组里面的参数顺序依次是id,代号,版本,登陆用户
* @param args
*/
public static void main(String[] args) {
parame=new String[]{"aa","bb","cc","dd"};
launch(args);
//System.out.println("main方法:"+Thread.currentThread());
}
@Override
public void start(Stage stage) throws Exception {
setProc();
String url="http://localhost:8888/shop/user/list";
//url=getUrl(url);
System.out.println(url);
stage.setTitle("材料窗口");
stage.setWidth(1350);
stage.setHeight(700);
webEngine.load(url);
webEngine.getLoadWorker().stateProperty().addListener(
new ChangeListener<State>() {
public void changed(ObservableValue ov, State oldState, State newState) {
if(newState==ov.getValue()) {
closeWindow();
} else if(oldState==ov.getValue()) {
}
}
}
);
Group root=new Group();
root.getChildren().add(browser);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public void closeWindow() {
EventListener listener = new EventListener() {
@Override
public void handleEvent(Event evt) {
getPlanChar();
Platform.exit();
}
};
Document doc = webEngine.getDocument();
try {
Element ele = doc.getElementById("close");
((EventTarget) ele).addEventListener("click",listener,false);
} catch(NullPointerException e) {
e.getMessage();
e.toString();
}
}
public void getPlanChar() {
String jsCode=this.getJsCode();
//System.out.println(jsCode);
String charString= (String) webEngine.executeScript(jsCode);
System.out.println(charString);
}
/**
* 使用了反射,更加灵活的应对了Proc实体类的改变,而不用改动业务类的代码
* @return
* @throws InvocationTargetException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
private String getUrl(String url) {
try {
String className=proc.getClass().getName();
Class<?> procClz=Class.forName(className);
Method[] methods=procClz.getDeclaredMethods();
for(Method method:methods) {
String methodName=method.getName();
if(!methodName.startsWith("get")) continue;
String fieldName=methodName.substring(3);
int firstLetterAscii=fieldName.charAt(0);
//把第一个字母转换成小写
char firstLetter;
//大写字母的ascii码在65到90之间
if(firstLetterAscii<=90&&firstLetterAscii>=65) {
//大写字母+32就是小写字母
firstLetter=(char)(firstLetterAscii+32);
fieldName=fieldName.replace((char) firstLetterAscii,firstLetter);
}
Object value=method.invoke(proc);
url+=fieldName+"="+value+"&";
}
url=url.substring(0,url.length()-1);
//System.out.println(url);
return url;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return null;
}
@SuppressWarnings("resource")
private String getJsCode() {
String code="";
try {
String jsPath=getClass().getResource("plan.js").getPath();
FileReader fr=new FileReader(jsPath);
BufferedReader br=new BufferedReader(fr);
String c=null;
while((c=br.readLine())!=null) {
code+=c.trim();
}
return code;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private void setProc() {
proc=new Proc();
proc.setItemId(parame[0]);
proc.setProcNo(parame[1]);
proc.setProcRev(parame[2]);
proc.setUsername(parame[3]);
}
}