controller层中,参数的获取方式以及作用域的问题

 1 package com.krry.web;
 2 
 3 import javax.servlet.http.HttpServletRequest;
 4 
 5 import org.springframework.stereotype.Controller;
 6 import org.springframework.ui.ModelMap;
 7 import org.springframework.web.bind.annotation.ModelAttribute;
 8 import org.springframework.web.bind.annotation.PathVariable;
 9 import org.springframework.web.bind.annotation.RequestMapping;
10 import org.springframework.web.servlet.ModelAndView;
11 
12 import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
13 
14 import bean.User;
15 
16 
17 @Controller
18 @RequestMapping("/model")
19 public class ModelMapController extends BaseController {
20     /********参数获取的方式**************************/
21 
22     //http://localhost/krryxa/model/hanlder/1.html
23     @RequestMapping("/handler/{id}")
24     public String handler(@PathVariable("id")Integer id){
25     //获得参数id为1
26         return "redirect:/success.jsp";
27     }
28     
29     //http://localhost/krryxa/model/handler2.html?id=5
30     @RequestMapping("/handler2")
31     public String handler2(Integer id){
32     //获得参数id为5
33         System.out.println(id);
34         return "redirect:/success.jsp";
35     }
36     
37     //通过对象的的注入方式最好
38     //http://localhost/krryxa/model/handler3.html?username=1351
39     @RequestMapping("/handler3")
40     public String handler3(User user){
41     //获得参数username为1351
42         System.out.println(user.getUsername());
43         return "redirect:/success.jsp";
44     }
45     
46     //http://localhost/krryxa/model/handler4.html?id=5
47     @RequestMapping("/handler4")
48     public String handler4(@ModelAttribute("teacher")User user){//若sessiong域中teacher改变了,这里也会改变
49     //获得参数id为5
50         System.out.println(request.getParameter("id"));
51         return "redirect:/success.jsp";
52     }
53     
54     
55 
56     /**作用域的问题reuqest session application 以下作用域的范围都是:request**/
57 
58     //在index页面直接用${message}获取
59     @RequestMapping("/handler7")
60     public String handler7(ModelMap map){
61         //这里是map的addAttribute设置
62         map.addAttribute("message", "我爱你吗。你们爱我我吗");
63         return "model/index";
64     }
65     
66     //在index页面直接用${message}获取
67     @RequestMapping("/handler5")
68     public String handler5(){
69         request.setAttribute("message", "我爱你吗。你们爱我我吗");
70         return "model/index";
71     }
72     
73     //在index页面直接用${message}获取
74     @RequestMapping("/handler6")
75     public ModelAndView handler6(){
76         //视图和作用域融合体
77         ModelAndView modelAndView = new ModelAndView();
78         modelAndView.setViewName("model/index"); //跳转到这个页面
79         modelAndView.addObject("message", "是打发是大法师的发送到发士大夫阿什顿");
80         return modelAndView;
81     }
82     //在index页面直接用${user.username}获取
83     @RequestMapping("/handler8")
84     public String handler8(@ModelAttribute("user")User user){
85         user.setUsername("ModelAttribute 我爱你吗。你们爱我吗");
86         return "model/index";
87     }
88     
89     
90 }

 

转载于:https://www.cnblogs.com/ainyi/p/8535535.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值