gpt4 book ai didi

javascript - 如何将值从Spring Controller 传递到jsp页面?(传递的值将用于初始化jsp页面中的javascript变量

转载 作者:太空宇宙 更新时间:2023-11-04 09:50:42 24 4
gpt4 key购买 nike

下面是我的Spring Controller 代码。我的目的是将变量int a、int b、int c的值传递给Jsp页面ADMINRESULTS。

请注意,这些变量的值将用于初始化 adminhome jsp 页面中的 javascript 变量

@RequestMapping("/adminresults")  //this is called by form action. This does not refer to adminhome jsp page

public String adminhome(Map<String, Object> model) {




ArrayList<Block> blockChain = NoobChain.getBlockChain();
Map<String, Integer> dataMap = new HashMap<String, Integer>();
if (!blockChain.isEmpty()) {

if (!NoobChain.isChainValid(blockChain)) { //if not valid to print the data.
model.put("tampermsg", "Unathorized acess detected and vote data is attacked.Correct values are ");

dataMap = NoobChain.validChainData(blockChain);
} else {
dataMap = blockChain.get(0).getData();

}
}
String blockchainJsonFromFile = new GsonBuilder().setPrettyPrinting().create().toJson(blockChain);
System.out.println("after.." + blockchainJsonFromFile);

model.put("message", "\n" + dataMap);
System.out.println("Before extracting DATA is "+dataMap);//to check the format of data map
int a=0;
int b=0;
int c=0;
if (dataMap.containsKey("A"))
{
a = dataMap.get("A");
System.out.println("value for key \"A\" is:- " + a);
}
if (dataMap.containsKey("B"))
{
b = dataMap.get("B");
System.out.println("value for key \"B\" is:- " + b);
}
if (dataMap.containsKey("C"))
{
c = dataMap.get("C");
System.out.println("value for key \"C\" is:- " + c);
}
model.put("a", a);
model.put("b", b);
model.put("c", c);



return "adminhome"; //significance of this code is to return adminhome jsp page
}

下面是 adminhome jsp 页面的代码片段

<html>
<head> </head>
<body>
<script type="text/javascript">

var as=8,cs=1,bs=4;
</script>
</body>
</html>

我的目的是将上述变量初始化为,bs,cs,并带有 int a、int b、int c(来自上面提到的 Spring Controller )

最佳答案

Send json object from controller and assign to your hidden variable on html and use that object in your java script. Below is code, just a sudo code below

Inside Controller

List<Someclass> list = new ArrayList<>();
Someclass someClass = new Someclass();
someClass.setKey("a");
someClass.setValue(1);
list.add(someClass);
Gson gson = new Gson();
ModelAndView modelAndView = new ModelAndView("adminhome");
modelAndView.addObject("list", gson.toJson(someClass));

JSP

  	
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<html>
<script type="text/javascript">
$(document).ready(function() {
var someClassJson = $('#"admin"');
list admins = JSON.parse(someClassJson.val());
for (var i = 0; i < admins.lenth; i++) {
var item = admins[i];
console.log(someClass.key);
console.log(someClass.value);
}
});
</script>
<head>
<title>Admin Example</title>
<script src="jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="personScript.js"></script>
</head>
<body>
<h1>
<Admin/h1> <input type="hidden" id="admin" value="${list}"/>
</body>
</html>

hope this will help.

关于javascript - 如何将值从Spring Controller 传递到jsp页面?(传递的值将用于初始化jsp页面中的javascript变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54789350/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com