为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

千万里不及你 2022-06-30 11:44:58
下面是我的 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 控制器)
查看完整描述

2 回答

?
江户川乱折腾

TA贡献1851条经验 获得超5个赞

找到了一个简单的答案,感谢 Sanjay 的帮助,还有 Nishant Raj(也请查看他的代码,因为我是初学者,我无法实现他的代码)


在 adminhome jsp 页面中


<html>

<head> </head>

<body>

<script type="text/javascript">


            var aa="${a}",bb="${b}",cc="${c}";

</script>

</body>

</html>

以上将从spring控制器获取值到javascript变量aa,bb,cc等。


但请注意,如果需要将获取的值用作 Integer,则必须对其进行转换。目前它是 String 类型。


它的代码是。


 <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>

如果需要, parseInt 会将其转换为整数,以便在将来的某些函数中使用它。谢谢大家,问题解决了:)


查看完整回答
反对 回复 2022-06-30
?
四季花海

TA贡献1811条经验 获得超5个赞

从控制器发送 json 对象并分配给 html 上的隐藏变量,并在您的 java 脚本中使用该对象。下面是代码,下面只是一个sudo代码


内部控制器


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>


查看完整回答
反对 回复 2022-06-30
  • 2 回答
  • 0 关注
  • 213 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号