之前學(xué)習(xí)SpringMVC時(shí)感覺(jué)他的傳值很神奇:簡(jiǎn)便,快捷,高效。
今天寫幾個(gè)簡(jiǎn)單的傳值與大家分享,希望能對(duì)大家有幫助。
一、
從后往前傳:
(1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
@Controller @RequestMapping (value={ "/hello" }) public class HelloController { @RequestMapping (value={ "sub" }) public ModelAndView submit(HttpServletRequest request) throws Exception { // TODO Auto-generated method stub ModelAndView m= new ModelAndView(); m.addObject( "ok" , "hello" ); m.setViewName( "success" ); return m; } } |
把想要傳遞的東西放在addObject(String,Object)里,值是Object類型,什么都可以放。
setViewName() 是設(shè)置跳轉(zhuǎn)到哪個(gè)頁(yè)面 (success.jsp頁(yè)面)。
在success.jsp 頁(yè)面里用${requestScope}或${ok}即可取出。是不是非常簡(jiǎn)便快捷。
還可以以這種方式傳:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
@Controller @RequestMapping (value={ "/user" }) public class UserController { @RequestMapping (value={ "/get" }) public ModelAndView user(User user) throws Exception { ModelAndView mv= new ModelAndView(); mv.addObject( "ok" ,user.getUsername()+ "--" +user.getPassword()); mv.setViewName( "success" ); return mv; } } |
前端是一個(gè)簡(jiǎn)單的form表單:
1
2
3
4
5
6
7
8
9
|
< form action = "user/get" method = "post" > < input type = "text" name = "username" id = "username" > < input type = "text" name = "password" id = "password" > < input type = "submit" > </ form > |
(2)返回值也可以不是ModelAndView
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@RequestMapping (value={ "/map" }) public String ok(Map map,Model model,ModelMap modelmap,User user) throws Exception { map.put( "ok1" , user); model.addAttribute( "ok2" ,user); modelmap.addAttribute( "ok3" , user); return "show" ; } |
二、
從前往后傳:
(1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
@RequestMapping (value={ "ant/{username}/topic/{topic}" },method={RequestMethod.GET}) public ModelAndView ant( @PathVariable (value= "username" ) String username, @PathVariable (value= "topic" ) String topic ) throws Exception { // TODO Auto-generated method stub ModelAndView m= new ModelAndView(); System.out.println(username); System.out.println(topic); return m; } |
前端是這個(gè)樣子:
<a href="hello/ant/Tom/topic/Cat">ant</a>
與value={"ant/{username}/topic/{topic}"}一一對(duì)應(yīng)。
還可以以這種形式:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
@RequestMapping (value={ "/regex/{number:\\d+}-{tel:\\d+}" }) public ModelAndView regex( @PathVariable (value= "number" ) int number, @PathVariable (value= "tel" ) String tel ) throws Exception { // TODO Auto-generated method stub ModelAndView m= new ModelAndView(); System.out.println(number); System.out.println(tel); return m; } |
前端是這個(gè)樣子:
<a href="hello/regex/100-111">regex(正則)</a>
(2)這是有鍵傳值:
1
2
3
4
5
6
7
8
9
|
@RequestMapping (value={ "/ok1" }) public String ok1( @RequestParam (value= "username" ) String username) throws Exception { System.out.println(username); return "show" ; } |
前端是這個(gè)樣子:
<a href="user/ok1?username=Tom">有鍵傳值</a>
這是無(wú)鍵傳值:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
@RequestMapping (value={ "/ok2" }) public String ok2( @RequestParam String password, @RequestParam String username) throws Exception { System.out.println(username); System.out.println(password); return "show" ; } |
前端是這個(gè)樣子:
<a href="user/ok2?username=Tom&password=111">無(wú)鍵傳值</a>
有意思的是它可以準(zhǔn)確的對(duì)應(yīng)好兩個(gè)值。
以上這篇SpringMVC的簡(jiǎn)單傳值(實(shí)現(xiàn)代碼)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。