JavaScript—以及创建和使用Java对象

JavaScript—以及创建和使用Java对象,java,javascript,jsp,web-applications,Java,Javascript,Jsp,Web Applications,我正在创建一个需要使用jar或类文件中的Java对象的web应用程序。如何连接Java类和JavaScript?(一般来说,我对JavaScript和Web开发相当陌生) (我在这个主题上得到的大多数搜索结果都来自那些没有意识到Java和JavaScript之间没有任何关系的人。这里的情况并非如此。) 我想要完成的是: PropertiesEditor对象首先加载属性文件模板,并将属性拆分为ArrayList。然后在网页上为每个属性生成一个表单,允许用户编辑值并使用新文件名提交。然后将值传递给P

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

(我在这个主题上得到的大多数搜索结果都来自那些没有意识到Java和JavaScript之间没有任何关系的人。这里的情况并非如此。)

我想要完成的是:

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

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);
}
}
公共类属性编辑器{
私有文件属性文件;
私有ArrayList propertyList;
私人扫描仪扫描;
/**
* 
*/
公共无效加载(字符串文件名){
试一试{
propertiesFile=新文件(文件名);
扫描=新扫描仪(属性文件);
}catch(filenotfounde异常){
e、 printStackTrace();
}
propertyList=新的ArrayList();
试一试{
while(scan.hasNext()){
String String=scan.nextLine();
if(!(string.startsWith(“#”){
String[]数组=String.split(“”);
字符串键=数组[0];
字符串值=数组[1];
属性=新属性(键、值);
propertyList.add(属性);
}
} 
}捕获(非接触元素异常nse){
}
}
公共作废保存(字符串文件名){
属性=新属性();
用于(当前属性:propertyList){
properties.setProperty(current.getKey(),current.getValue());
}
试试{
文件=新文件(文件名+“.properties”);
FileOutputStream fileOut=新的FileOutputStream(文件);
properties.store(fileOut,fileName);
fileOut.close();
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}
public int getNumberOfProperties(){
返回propertyList.size();
}
公共字符串getPropertyKey(int索引){
返回propertyList.get(index.getKey();
}
公共字符串getPropertyValue(int索引){
返回propertyList.get(index.getValue();
}
public void setPropertyValue(int索引,字符串值){
propertyList.get(索引).setValue(值);
}
}
我认为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>

var-pe=//显然,我想在哪里创建Java对象
pe.荷载(“美国财产”);
函数formValidator(){

对于(i=0;i研究GWT及其RPC方法,以了解您尝试的内容的复杂性。一般来说,Javascript代码永远无法访问Java对象。它们在不同机器上不同进程的不同VM上运行。

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


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

您不能将java对象与javascript对象混合匹配。java和javascript没有关联。如果您现有的java代码库需要某种方式的javascript前端,可以通过一种方式将它们交互我们的目标是开发一个web应用程序,该应用程序提供一个Javascript可以交互的web服务。我提到,如果你阅读了整篇文章,我意识到Java和Javascript没有关系。我将研究如何使用JSP。谢谢。