我想在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中使用Java声明的类和函数。
鉴于此,您有3个主要选择: