gpt4 book ai didi

SpringMVC的简单传值(实现代码)

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 26 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章SpringMVC的简单传值(实现代码)由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

之前学习SpringMVC时感觉他的传值很神奇:简便,快捷,高效.

今天写几个简单的传值与大家分享,希望能对大家有帮助.

1、 。

从后往前传:

(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() 是设置跳转到哪个页面 (success.jsp页面).

在success.jsp 页面里用${requestScope}或${ok}即可取出。是不是非常简便快捷.

还可以以这种方式传:

?
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;
  }
 
}

前端是一个简单的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" ;
 
}

2、 。

从前往后传:

(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;
 
   }

前端是这个样子:

<a href="hello/ant/Tom/topic/Cat">ant</a> 。

与value={"ant/{username}/topic/{topic}"}一一对应.

还可以以这种形式:

  。

?
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;
 
   }

前端是这个样子:

<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" ;
 
   }

前端是这个样子:

<a href="user/ok1?username=Tom">有键传值</a> 。

这是无键传值:

?
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" ;
 
   }

前端是这个样子:

<a href="user/ok2?username=Tom&password=111">无键传值</a> 。

有意思的是它可以准确的对应好两个值.

以上这篇SpringMVC的简单传值(实现代码)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我.

最后此篇关于SpringMVC的简单传值(实现代码)的文章就讲到这里了,如果你想了解更多关于SpringMVC的简单传值(实现代码)的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com