简体   繁体   English

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

[英]How to pass values from Spring Controller to jsp page?( the passed values is to be used to intialise javascript variables in jsp page

Below is my Spring Controller code.My intention is to pass the values of variables int a, int b, int c to the Jsp page ADMINRESULTS. 下面是我的Spring Controller代码。我的意图是将变量int a,int b,int c的值传递给Jsp页面ADMINRESULTS。

Please note that the values of these variables are to be used to intialise javascript variables in adminhome jsp page 请注意,这些变量的值将用于在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
    }

Below is a code snippet from adminhome jsp page 下面是来自adminhome jsp页面的代码片段

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

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

My intention is to intialise the above variables as,bs,cs with int a, int b, int c(from the Spring Controller Mentioned above) 我的意图是使用int a,int b,int c将上述变量初始化为bs,cs(来自上面提到的Spring Controller)

Send json object from controller and assign to your hidden variable on html and use that object in your java script. 从控制器发送json对象,并分配给html上的隐藏变量,然后在Java脚本中使用该对象。 Below is code, just a sudo code below 下面是代码,下面是一个sudo代码

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 JSP

  \t
 <%@ 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. 希望这会有所帮助。

Found an easy answer,Thank you Sanjay for helping,Also Nishant Raj(check out his code too,as Im a beginner ,I couldn't implement his code) 找到了一个简单的答案,谢谢桑杰(Sanjay)的帮助,尼桑·拉吉(Nishant Raj)(也请查看他的代码,作为我的初学者,我无法实现他的代码)

In the adminhome jsp page 在adminhome jsp页面中

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

            var aa="${a}",bb="${b}",cc="${c}";
</script>
</body>
</html>

The above will fetch the values to javascript variables aa,bb,cc etc from the spring controller. 上面的代码将从spring控制器中获取值到javascript变量aa,bb,cc等。

But please note if the fetched values if need to be used as Integer it must be converted.Presently it is of String type. 但是请注意,如果获取的值需要用作整数,则必须将其转换为当前类型的String类型。

The code for it is. 它的代码是。

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

                var aa="${a}",bb="${b}",cc="${c}";
                var as=parseInt(aa),cs=parseInt(cc),bs=parseInt(bb);
    </script>
    </body>

</html>

The parseInt will convert it into integer for using it in some future functions if needed. 如果需要,parseInt会将其转换为整数,以供将来使用。 Thank You all, Issue solved :) 谢谢大家,问题已解决:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM