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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM