如何将值从 Spring Controller 传递到 jsp 页面?

下面是我的 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);


我的意图是将上述变量初始化为,bs,cs 与 int a,int b,int c(来自上面提到的 Spring 控制器)


千万里不及你
浏览 214回答 2
2回答

江户川乱折腾

找到了一个简单的答案,感谢 Sanjay 的帮助,还有 Nishant Raj(也请查看他的代码,因为我是初学者,我无法实现他的代码)在 adminhome jsp 页面中<html><head> </head><body><script type="text/javascript">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var aa="${a}",bb="${b}",cc="${c}";</script></body></html>以上将从spring控制器获取值到javascript变量aa,bb,cc等。但请注意,如果需要将获取的值用作 Integer,则必须对其进行转换。目前它是 String 类型。它的代码是。&nbsp;<html>&nbsp; &nbsp; <head> </head>&nbsp; &nbsp; <body>&nbsp; &nbsp; <script type="text/javascript">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var aa="${a}",bb="${b}",cc="${c}";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var as=parseInt(aa),cs=parseInt(cc),bs=parseInt(bb);&nbsp; &nbsp; </script>&nbsp; &nbsp; </body></html>如果需要, parseInt 会将其转换为整数,以便在将来的某些函数中使用它。谢谢大家,问题解决了:)

四季花海

从控制器发送 json 对象并分配给 html 上的隐藏变量,并在您的 java 脚本中使用该对象。下面是代码,下面只是一个sudo代码内部控制器List<Someclass> list = new ArrayList<>();&nbsp; &nbsp; &nbsp;Someclass someClass = new Someclass();&nbsp; &nbsp; &nbsp;someClass.setKey("a");&nbsp; &nbsp; &nbsp;someClass.setValue(1);&nbsp; &nbsp; &nbsp;list.add(someClass);&nbsp; &nbsp; &nbsp;Gson gson = new Gson();&nbsp; &nbsp; &nbsp;ModelAndView modelAndView = new ModelAndView("adminhome");&nbsp; &nbsp; &nbsp;modelAndView.addObject("list", gson.toJson(someClass));JSP&nbsp; &nbsp;&nbsp;<%@ page language="java" contentType="text/html; charset=ISO-8859-1"&nbsp; &nbsp; pageEncoding="ISO-8859-1"%><%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>&nbsp; &nbsp; <html>&nbsp; &nbsp; <script type="text/javascript">&nbsp; &nbsp; &nbsp; &nbsp; $(document).ready(function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var someClassJson = $('#"admin"');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list&nbsp; &nbsp; &nbsp; &nbsp; admins = JSON.parse(someClassJson.val());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (var i = 0; i < admins.lenth; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var item = admins[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(someClass.key);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(someClass.value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; </script>&nbsp; &nbsp; <head>&nbsp; &nbsp; <title>Admin Example</title>&nbsp; &nbsp; <script src="jquery-1.11.3.min.js"></script>&nbsp; &nbsp; <script type="text/javascript" src="personScript.js"></script>&nbsp; &nbsp; </head>&nbsp; &nbsp; <body>&nbsp; &nbsp; &nbsp; &nbsp; <h1>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Admin/h1> <input type="hidden" id="admin"&nbsp; &nbsp; &nbsp; &nbsp;value="${list}"/>&nbsp; &nbsp; </body>&nbsp; &nbsp; </html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java