spring mvc請求controller訪問
1.一個Controller里含有不同的請求url
1
2
3
4
5
6
7
8
9
10
11
12
|
@Controller //類似Struts的Action public class TestController { @RequestMapping ( "test/login.do" ) // 請求url地址映射,類似Struts的action-mapping public String testLogin( @RequestParam (value= "username" )String username, String password, HttpServletRequest request) { // @RequestParam是指請求url地址映射中必須含有的參數(除非屬性required=false) // @RequestParam可簡寫為:@RequestParam("username") if (! "admin" .equals(username) || ! "admin" .equals(password)) { return "loginError" ; // 跳轉頁面路徑(默認為轉發),該路徑不需要包含spring-servlet配置文件中配置的前綴和后綴 } return "loginSuccess" ; } } |
2.采用一個url訪問
通過url參數來區分訪問不同的方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
@Controller @RequestMapping ( "/test2/login.do" ) // 指定唯一一個*.do請求關聯到該Controller public class TestController2 { @RequestMapping public String testLogin(String username, String password, int age) { // 如果不加任何參數,則在請求/test2/login.do時,便默認執行該方法 if (! "admin" .equals(username) || ! "admin" .equals(password) || age < 5 ) { return "loginError" ; } return "loginSuccess" ; } @RequestMapping (params = "method=1" , method=RequestMethod.POST) public String testLogin2(String username, String password) { // 依據params的參數method的值來區分不同的調用方法 // 可以指定頁面請求方式的類型,默認為get請求 if (! "admin" .equals(username) || ! "admin" .equals(password)) { return "loginError" ; } return "loginSuccess" ; } @RequestMapping (params = "method=2" ) public String testLogin3(String username, String password, int age) { if (! "admin" .equals(username) || ! "admin" .equals(password) || age < 5 ) { return "loginError" ; } return "loginSuccess" ; } } |
3.RequestMapping在Class上
可看做是父Request請求url,而RequestMapping在方法上的可看做是子Request請求url,父子請求url最終會拼起來與頁面請求url進行匹配
1
2
3
4
5
6
7
8
9
10
11
|
@Controller @RequestMapping ( "/test3/*" ) // 父request請求url public class TestController3 { @RequestMapping ( "login.do" ) // 子request請求url,拼接后等價于/test3/login.do public String testLogin(String username, String password, int age) { if (! "admin" .equals(username) || ! "admin" .equals(password) || age < 5 ) { return "loginError" ; } return "loginSuccess" ; } } |
4.在SpringMVC中常用的注解
還有@PathVariable,@RequestParam,@PathVariable標記在方法的參數上,利用它標記的參數可以利用請求路徑傳值
1
2
3
4
5
6
|
@Controller //類似Struts的Action public class TestController { @RequestMapping (value= "/comment/{blogId}" , method=RequestMethod.POST) public void comment(Comment comment, @PathVariable int blogId) throws IOException { } } |
springmvc請求一次,訪問多個controller方法
有一個需求:請求一次,訪問多個controller中的方法
比如:先執行查詢操作,再將查詢出來的內容更新(當然也可以將方法寫到bo中,在controller中直接調用bo的方法,這里只是舉個例子)
舉例
JSP頁面
1
2
3
4
5
6
7
8
9
10
|
<%@ page contentType="text/html;charset=UTF-8" language="java" %> < html > < head > < title >在一個action中執行兩個方法</ title > </ head > < body > 1 哈哈 7000 < a href = "${pageContext.request.contextPath}/emp/find?id=1" rel = "external nofollow" style = "text-decoration:none" >編輯</ a > </ body > </ html > |
Controller頁面
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
@Controller @RequestMapping ( "/emp" ) public class EmpAction { @RequestMapping (value= "/find" ) public String findEmpById( int id) throws Exception{ System.out.println( "查詢" +id+ "號員工信息" ); //轉發到EmpAction的另一個方法中去,即再次發送請求 // return "forward:/emp/update"; //重定向到EmpAction的另一個方法中去,即再次發送請求 return "redirect:/emp/update.action?id=" + id; } @RequestMapping (value= "/update" ) public String updateEmpById( int id,Model model) throws Exception{ System.out.println( "更新" + id + "號員工信息" ); model.addAttribute( "message" , "更新員工信息成功" ); return "success" ; } } |
結論
1. ModelAndView并不能實現兩個方法之間的數據傳遞;
2. 可以通過Session來進行傳遞。
有多種方法可以實現Session傳遞
方法1:將HttpServletRequest作為方法形參 通過request.getSession().addAttribute
方法2:將HttpSession作為方法形參
方法3:通過@SessionAttribute+@ModelAttribute來進行傳遞
使用HttpSession來存取數據,不過這樣又在springmvc中使用了servlet的內容,并不好
3. 使用轉發。在轉發情況下,共享request域對象,會將參數從第一個業務控制方法傳入第二個業務控制方法
1
|
return "forward:/emp/update.action" ; |
4. 重定向不共享參數,所以要帶參數才行
1
|
return "redirect:/emp/update.action?id=" + id; |
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/u011332918/article/details/50600958