我想在HTML文件中调用Java方法,但我不怎么做。

这是Java类:

package GET;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.collections4.MultiValuedMap;
import org.apache.commons.collections4.multimap.ArrayListValuedHashMap;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
import java.util.*;

public class ProductController {

    public MultiValuedMap<String, MultiValuedMap<String, String>> fahrradlenker= new ArrayListValuedHashMap<>();
    public MultiValuedMap<String, String> flatbarlenker= new ArrayListValuedHashMap<>();
    public MultiValuedMap<String, String> rennradlenker= new ArrayListValuedHashMap<>();
    public MultiValuedMap<String, String> bullhornlenker= new ArrayListValuedHashMap<>();

    @SuppressWarnings("Duplicates")
    public void lenkertyp() throws  IOException {
        ObjectMapper mapper = new ObjectMapper();


        URL urlForGetRequest = new URL("https://www.maripavi.at/produkt/lenkertyp");
        String readLine = null;
        HttpURLConnection connection = (HttpURLConnection) urlForGetRequest.openConnection();
        connection.setRequestMethod("GET");
        int responseCode = connection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(connection.getInputStream()));
            StringBuffer response = new StringBuffer();
            while ((readLine = in .readLine()) != null) {
                response.append(readLine);
            } in .close();

            String lenker =response.toString();
            List<String> list = mapper.readValue(lenker, new TypeReference<List<String >>() {});

            for (int i=0; i<list.size();i++){
                String s = list.get(i);
                if (s.contains("Flatbarlenker")){
                    fahrradlenker.put("Flatbarlenker", flatbarlenker);
                }else if (s.contains("Bullhornlenker")){
                    fahrradlenker.put("Bullhornlenker", bullhornlenker);
                }else{
                    fahrradlenker.put("Rennradlenker", rennradlenker);
                }
            }

        } else {
            System.out.println("GET NOT WORKED");
        }
    }



这是HTML文件,我想从上面调用Java方法:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Home</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <p>Configure your handlebar <a href="../js/call.js" >here</a></p>
    <More-sharebar><a href="../js/call.js" onclick="javascript:f()">More options</a></More-sharebar>
</body>
</html>


而且我创建了一个JavaScript文件来调用Java方法,但这也不起作用:

function f() {
    var lenkertyps = new Packages.GET.ProductController.lenkertyp();
}


这是我的项目的结构:

javascript - 从Javascript/HTML调用Java-LMLPHP

最佳答案

您不可能直接在JavaScript中使用Java声明的类和函数。

鉴于此,您有3个主要选择:


用JavaScript重新编写Java类和函数。除非您需要任何DB / FS操作或存储机密信息(例如API机密),否则可能是可行的。
将您的Java功能拆分为可重复使用的一次性操作(例如:将字符串写入数据库表),将这些功能公开为API,并在带有JS的Frontend中使用它。从头开始很难正确实现此选项,因此,如果需要它,您很可能希望使用某些框架。
根据您的目的,您可能还想使用WebAssembly

关于javascript - 从Javascript/HTML调用Java,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59489874/

10-10 22:46