使用JxBrowser以及JavaScript和HTML,我遇到了实现表单数据输入机制的问题。
上述文档说明了如何实现这一点,但是在实现时我无法获得正确的结果。
我的课程如下:
String sourceString, targetString;
int insertCost, deleteCost, subCost;
final Browser browser = new Browser();
BrowserView view = new BrowserView(browser);
JFrame frame = new JFrame("User Input");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(view, BorderLayout.CENTER);
frame.setSize(500, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
browser.addLoadListener(new LoadAdapter() {
@Override
public void onFinishLoadingFrame(FinishLoadingEvent event) {
if (event.isMainFrame()) {
System.out.println("INNNN");
final Browser browser = event.getBrowser();
JSValue value = browser.executeJavaScriptAndReturnValue("window");
System.out.println(value.toString());
value.asObject().setProperty("Parameter", new Parameter());
}
}
});
InputStream urlStream = getClass().getResourceAsStream("../web/UserInputForm.html");
String html = null;
try (BufferedReader urlReader = new BufferedReader(new InputStreamReader (urlStream))) {
StringBuilder builder = new StringBuilder();
String row;
while ((row = urlReader.readLine()) != null) {
builder.append(row);
}
html = builder.toString();
} catch (IOException e) {
throw new RuntimeException(e);
}
final String loadHTMLPath = html;
Browser.invokeAndWaitFinishLoadingMainFrame(browser, new Callback<Browser>(){
@Override
public void invoke(Browser arg0) {
browser.loadHTML(loadHTMLPath);
}
});
}
public static class Parameter {
public void save(String sourceString, String targetString, int insertRadioValue, int deleteRadioValue, int subRadioValue,
int insertCostValue, int deleteCostValue, int subCostValue){
System.out.println("INNNN");
System.out.println("Source" + sourceString);
System.out.println(targetString);
System.out.println(insertRadioValue);
System.out.println(deleteRadioValue);
System.out.println(subRadioValue);
System.out.println(insertCostValue);
System.out.println(deleteCostValue);
System.out.println(subCostValue);
}
}
JavaScript位于我的HTML文件的头部,看起来像这样
<script type="text/javascript">
function sendFormData() {
window.alert("HEKELEW");
var sourceStringValue = myForm.elements['source'].value;
window.alert(sourceStringValue);
var targetStringValue = myForm.elements['target'].value;
var insertRadioValue = myForm.elements['insertRadio'].value;
var deleteRadioValue = myForm.elements['deleteRadio'].value;
var subRadioValue = myForm.elements['subRadio'].value;
var insertCostValue = myForm.elements['insert_cost'].value;
var deleteCostValue = myForm.elements['delete_cost'].value;
var subCostValue = myForm.elements['substitute_cost'].value;
Paramater.save(sourceStringValue, targetStringValue, insertRadioValue, deleteRadioValue, subRadioValue, insertCostValue, deleteCostValue, subCostValue);
}
</script>
我无法访问我的Parameter类中的变量,因为我应该能够访问。我确定原因很简单。是否与这行代码有关,我不明白需要从executeJavaScriptAndReturnValue命令传递什么。
JSValue value = browser.executeJavaScriptAndReturnValue("window");
任何帮助将不胜感激。
编辑:
</head>
<body>
<form id="myForm">
Source String: <input name = "source" type="text"> <br>
Target String: <input name="target" type="text" ><br>
Insertion <input type="radio" name="insertion_radio" name = "insertRadio"><br>
Deletion <input type="radio" name="deletion_radio" name = "deleteRadio"><br>
Substitute <input type="radio" name="substitution_radio" name = "subRadio"><br>
Insert Cost <input type="number" name="insert_cost"><br>
Delete Cost <input type="number" name="delete_cost"><br>
Substitute Cost <input type="number" name="substitute_cost"><br>
<div>
<input type="button" value="enter" onclick="sendFormData();"/><br>
</form>
</body>
</html>
答案 0 :(得分:0)
以下是包含所有修改的完整工作示例。
爪哇:
public class BrowserSample {
public static void main(String[] args) {
final Browser browser = new Browser();
BrowserView view = new BrowserView(browser);
JFrame frame = new JFrame("User Input");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(view, BorderLayout.CENTER);
frame.setSize(500, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
browser.addScriptContextListener(new ScriptContextAdapter() {
@Override
public void onScriptContextCreated(ScriptContextEvent event) {
JSValue value = browser.executeJavaScriptAndReturnValue(event.getJSContext().getFrameId(),"window");
value.asObject().setProperty("Parameter", new Parameter());
}
});
browser.addConsoleListener(new ConsoleListener() {
@Override
public void onMessage(ConsoleEvent event) {
System.out.println("event.getMessage() = " + event.getMessage());
}
});
InputStream urlStream = BrowserSample.class.getResourceAsStream("web/UserInputForm.html");
String html;
try (BufferedReader urlReader = new BufferedReader(new InputStreamReader(urlStream))) {
StringBuilder builder = new StringBuilder();
String row;
while ((row = urlReader.readLine()) != null) {
builder.append(row);
}
html = builder.toString();
} catch (IOException e) {
throw new RuntimeException(e);
}
final String loadHTMLPath = html;
Browser.invokeAndWaitFinishLoadingMainFrame(browser, new Callback<Browser>(){
@Override
public void invoke(Browser arg0) {
browser.loadHTML(loadHTMLPath);
}
});
}
public static class Parameter {
public void save(String sourceString, String targetString, String insertRadioValue, String deleteRadioValue, String subRadioValue,
String insertCostValue, String deleteCostValue, String subCostValue){
System.out.println("INNNN");
System.out.println("Source" + sourceString);
System.out.println(targetString);
System.out.println(insertRadioValue);
System.out.println(deleteRadioValue);
System.out.println(subRadioValue);
System.out.println(insertCostValue);
System.out.println(deleteCostValue);
System.out.println(subCostValue);
}
}
}
HTML:
<html>
<head>
<script type="text/javascript">
function sendFormData() {
window.alert("HEKELEW");
var sourceStringValue = myForm.elements['source'].value;
window.alert(sourceStringValue);
var targetStringValue = myForm.elements['target'].value;
var insertRadioValue = myForm.elements['insertRadio'].value;
var deleteRadioValue = myForm.elements['deleteRadio'].value;
var subRadioValue = myForm.elements['subRadio'].value;
var insertCostValue = myForm.elements['insert_cost'].value;
var deleteCostValue = myForm.elements['delete_cost'].value;
var subCostValue = myForm.elements['substitute_cost'].value;
Parameter.save(sourceStringValue, targetStringValue, insertRadioValue, deleteRadioValue, subRadioValue, insertCostValue, deleteCostValue, subCostValue);
}
</script>
</head>
<body>
<form id="myForm">
Source String: <input name = "source" type="text"> <br>
Target String: <input name="target" type="text" ><br>
Insertion <input type="radio" name = "insertRadio"><br>
Deletion <input type="radio" name = "deleteRadio"><br>
Substitute <input type="radio" name = "subRadio"><br>
Insert Cost <input type="number" name="insert_cost"><br>
Delete Cost <input type="number" name="delete_cost"><br>
Substitute Cost <input type="number" name="substitute_cost"><br>
<div>
<input type="button" value="enter" onclick="sendFormData();"/><br>
</form>
</body>
</html>