前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >controller层中,参数的获取方式以及作用域的问题

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

作者头像
Krry
发布2018-09-10 17:59:14
8600
发布2018-09-10 17:59:14
举报
文章被收录于专栏:KrryblogKrryblog
代码语言:javascript
复制
 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 }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-03-09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档