ajax modelmap,Spring MVC: How to Access ModelMap Values in a JSP? Get values into JSP from Controlle...

本文详细指导了如何在Spring MVC中设置HelloWorld示例,包括如何更新Controller和JSP,以便在页面上展示ModelMap中的数据。通过实例步骤,解决如何从Spring Controller传递多个值到JSP的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

5c323eb30318d5ba23315cf3ea25358f.png

Do you have one of below questions?

How do I access ModelMap in a JSP?

My ModelMap is not passing beans to JSP.

How can I pass multiple values from Spring Controller to JSP?

in JSP how to get multiple values from Spring MVC Controller?

Here is a simple working solution for all of above questions.

Please go through above example which has all detailed steps on how to setup your 1st Hello World Spring MVC example..

Once you are done with that we will modify below two files to see how ModelMap works.

CrunchifyHelloWorld.java

welcome.jsp

Here is a code we need to change for this example.

Step-1. Update CrunchifyHelloWorld.java

CrunchifyHelloWorld.java

Java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

packagecom.crunchify.controller;

importorg.springframework.stereotype.Controller;

importorg.springframework.ui.ModelMap;

importorg.springframework.web.bind.annotation.RequestMapping;

importorg.springframework.web.bind.annotation.RequestMethod;

/**

* @author Crunchify.com

*

*/

@Controller

@RequestMapping("/welcome")

publicclassCrunchifyHelloWorld{

@RequestMapping(method=RequestMethod.GET)

publicStringprintWelcome(ModelMapmodel){

Stringheading="Spring MVC: How to Access ModelMap Values in a JSP?";

Stringresult1="Hey.. I'm result1.. You are seeing me on welcome page.";

Stringresult2="Hey.. I'm result2.. ";

Stringcredit="Demo by Crunchify. Click here for more than 350 Java Examples.";

// you can add any Collection Objects to ModelMap

// including JSON, String, Array, Map, List, etc...

model.addAttribute("heading",heading);

model.addAttribute("result1",result1);

model.addAttribute("result2",result2);

model.addAttribute("credit",credit);

return"welcome";

}

}

Step-2. Update welcome.jsp

welcome.jsp

XHTML

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

pageEncoding="ISO-8859-1"%>

body{

background-image:url('https://cdn.crunchify.com/bg.png');

}

Spring MVC: How to Access ModelMap Values in a JSP? -

Crunchify.com

${heading}

{result1}

${result2}


${credit}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值