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 }
复制代码

 

posted @   筱月  阅读(1565)  评论(0编辑  收藏  举报
努力加载评论中...
编辑推荐:
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
阅读排行:
· 手把手教你更优雅的享受 DeepSeek
· AI工具推荐:领先的开源 AI 代码助手——Continue
· 探秘Transformer系列之(2)---总体架构
· V-Control:一个基于 .NET MAUI 的开箱即用的UI组件库
· 乌龟冬眠箱湿度监控系统和AI辅助建议功能的实现
点击右上角即可分享
微信分享提示