【问题标题】:JavaScript - And Creating and using Java ObjectsJavaScript - 以及创建和使用 Java 对象
【发布时间】:2012-01-19 19:22:23
【问题描述】:

我正在创建一个需要使用 jar 或类文件中的 Java 对象的 Web 应用程序。如何桥接我的 Java 类和 JavaScript? (总的来说,我对 JavaScript 和 Web 开发还很陌生)。

(我得到的关于这个主题的大部分搜索结果都来自那些没有意识到 Java 和 JavaScript 彼此无关的人。这里不是这种情况。)

我想要完成的事情:

PropertiesEditor 对象首先加载一个属性文件模板并将属性拆分为一个 ArrayList。然后在网页上为每个属性生成一个表单,并允许用户编辑值并使用新文件名提交。然后将这些值传递给 PropertiesEditor 对象并创建属性文件,并与其他属性文件一起保存。这个网络应用程序将允许非编程用户从现有模板创建新的属性文件;就我而言,用于文本语言的本地化。

Java 类:

public class PropertiesEditor {
private File propertiesFile;
private ArrayList<Property> propertyList;
private Scanner scan;

/**
 * 
 */
public void load(String fileName) {
    try {
        propertiesFile = new File(fileName);
        scan = new Scanner(propertiesFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    propertyList = new ArrayList<Property>();
    try{
    while (scan.hasNext()){
        String string = scan.nextLine();
            if (!(string.startsWith("#"))){                 
                String[] array = string.split("=");
                String key = array[0];
                String value = array[1];
                Property property = new Property(key, value);
                propertyList.add(property);
            }
    } 
    }catch (NoSuchElementException nse){
    }

}

public void save(String fileName){
    Properties properties = new Properties();
    for (Property current : propertyList){
            properties.setProperty(current.getKey(), current.getValue());
    }
        try {   
            File file = new File(fileName + ".properties");
            FileOutputStream fileOut = new FileOutputStream(file);
            properties.store(fileOut, fileName);
            fileOut.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
}

public int getNumberOfProperties(){
    return propertyList.size();
}

public String getPropertyKey(int index){
    return propertyList.get(index).getKey();
}

public String getPropertyValue(int index){
    return propertyList.get(index).getValue();
}

public void setPropertyValue(int index, String value){
    propertyList.get(index).setValue(value);
}
}

我认为 JavaScript 应该是什么样子:

<script type='text/javascript'>
       var pe = <PropertiesEditorObject>;//Obviously where I want to create the Java Object
       pe.load("en-us.properties");

    function formValidator(){
        for (i=0;i<pe.getNumberOfProperties();i++){
            var current = document.getElementById(i);
            pe.setPropertyValue(i, current);
        }
        pe.save(document.getElementById('fileName');
    }

    function createForm(){
        document.write("<form onsubmit='return formValidator()' >")
        for (i=0;i<pe.getNumberOfProperties();i++){
            document.write( pe.getPropertyValue(i) + "<input type='text' id='" + i + "' /><br />"
        }
        document.write("Properties File Name: <input type='text' id='fileName' /><br /><input type='submit' value='Check Form' /><br /></form>");
    }
    </script>

【问题讨论】:

  • 你不能混合和匹配 java 对象和 javascript 对象。
  • Java 和 Javascript 不相关。如果您有一个现有的 Java 代码库需要某种方式的 Javascript 前端,那么让它们相互交互的一种方法是开发一个 Web 应用程序,该应用程序提供一个您的 Javascript 可以与之交互的 Web 服务。
  • 我提到如果你阅读整篇文章,我意识到 Java 和 JavaScript 没有关系。

标签: java javascript jsp web-applications


【解决方案1】:

我是一名 java 开发人员,所以我无法具体回答您的问题,但请记住 JavaScript 是在客户端(用户的浏览器)的上下文中运行的。因此,即使做你想做的事情在技术上是可行的(出于某些明显的原因,我认为不可行),类/jar/其他资源也需要在远程用户的系统上。

JSP 和适当的应用程序服务器可以弥补这一差距。属性文件保留在服务器上,可以使用JSP对其进行修改。

【讨论】:

  • 我将研究使用 JSP。谢谢。
【解决方案2】:

查看 GWT 及其 RPC 方法以了解您正在尝试的复杂性。通常,Javascript 代码永远无法访问 Java 对象。它们在不同机器上不同进程的不同虚拟机上运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-03
    • 2015-02-25
    • 1970-01-01
    • 2015-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多