@RequestMappingjava
這個註解標註在方法名上,如web
1 /** 2 * 攔截全部請求: 3 * @RequestMapping(value="/*", method = {RequestMethod.GET}) 4 * http://localhost:8080/SpringMVCTag/helloworld/xd 5 */ 6 //@AuthPassport 7 @RequestMapping(value="/*", method = {RequestMethod.GET}) 8 public ModelAndView urlTest(){ 9 ModelAndView modelAndView = new ModelAndView(); 10 //跳轉到 urltest.jsp或者其餘後綴的頁面,這個要看springmvc的配置文件裏是怎麼設置的, 11 modelAndView.setViewName("urltest"); 12 return modelAndView; 13 }
@RequestMapping(value="/*")spring
1 就是攔截全部請求sql
結合上面的一小段代碼就是攔截:數據庫
相似於http://localhost:8080/SpringMVCTag/helloworld/我是任意字符,mvc
http://localhost:8080/SpringMVCTag/helloworld/comaosdfjoaapp
這樣的請求。jsp
2 攔截多個(多個)請求url
例如:spa
1 /** 2 * 攔截多種請求 3 * 如: @RequestMapping(value={"/index","/hello"}) 4 * 訪問URL:http://localhost:8080/SpringMVCTag/helloworld/index 5 * @return 6 * @throws SQLException 7 */ 8 //@AuthPassport 9 @RequestMapping(value={"/index","/hello"}) 10 public ModelAndView index() throws SQLException{ 11 12 //throw new SQLException("數據庫異常。"); 13 14 ModelAndView modelAndView = new ModelAndView(); 15 //在jsp中能夠經過 ${message} 的形式來獲取綁定的值 16 modelAndView.addObject("message", "Hello World!"); 17 modelAndView.setViewName("index"); 18 return modelAndView; 19 }
3 @RequestMapping 註解結合 @PathVariable註解能夠在控制器(也就是具體的方法)的入參裏
獲取到URL中的佔位參數
例如:
1 /** 2 * @RequestMapping 結合 @PathVariable 註解能夠獲取URL中帶佔位的那個參數值, 3 * 具體到這個例子中,在方法名中Integer id這個入參的值就是URL中的值, 4 * 假如URL爲: 5 * http://localhost:8080/SpringMVCTag/helloworld/detail/yangdandan 6 * 那麼在跳轉到的detail.jsp中經過 7 * ${cxrr}的形式就能夠獲取到 yangdandan 這個字符串 8 * @param id 9 * @return 10 */ 11 @RequestMapping(value="/detail/{thepara}", method = {RequestMethod.GET}) 12 public ModelAndView getDetail(@PathVariable(value="thepara") String thepara){ 13 14 ModelAndView modelAndView = new ModelAndView(); 15 modelAndView.addObject("crxx", thepara); 16 modelAndView.setViewName("detail"); 17 return modelAndView; 18 }
明天繼續填坑。
項目所在位置以下圖:
BaseController.java:
1 package com.demo.web.controllers; 2 3 import java.sql.SQLException; 4 import javax.servlet.http.HttpServletRequest; 5 import org.springframework.web.bind.annotation.ExceptionHandler; 6 /** 7 * 8 * @author Wei 9 * @time 2017年4月20日 下午3:56:51 10 */ 11 public abstract class BaseController { 12 13 @ExceptionHandler 14 public String exception(HttpServletRequest request, Exception e) { 15 16 request.setAttribute("exceptionMessage", e.getMessage()); 17 18 // 根據不一樣的異常類型進行不一樣處理 19 if(e instanceof SQLException) 20 return "testerror"; 21 else 22 return "error"; 23 } 24 25 }
HelloWorldController.java
1 package com.demo.web.controllers; 2 3 import java.sql.SQLException; 4 5 import org.springframework.stereotype.Controller; 6 import org.springframework.web.bind.annotation.PathVariable; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.bind.annotation.RequestMethod; 9 import org.springframework.web.servlet.ModelAndView; 10 11 /** 12 * 13 * @author Wei 14 * @time 2017年4月20日 下午4:56:43 15 */ 16 @Controller 17 @RequestMapping(value = "/helloworld") 18 public class HelloWorldController extends BaseController { 19 /** 20 * 攔截全部請求: 21 * @RequestMapping(value="/*", method = {RequestMethod.GET}) 22 * http://localhost:8080/SpringMVCTag/helloworld/xd 23 */ 24 //@AuthPassport 25 @RequestMapping(value="/*", method = {RequestMethod.GET}) 26 public ModelAndView urlTest(){ 27 ModelAndView modelAndView = new ModelAndView(); 28 //跳轉到 urltest.jsp或者其餘後綴的頁面,這個要看springmvc的配置文件裏是怎麼設置的, 29 modelAndView.setViewName("urltest"); 30 return modelAndView; 31 } 32 /** 33 * 攔截多種請求 34 * 如: @RequestMapping(value={"/index","/hello"}) 35 * 訪問URL:http://localhost:8080/SpringMVCTag/helloworld/index 36 * @return 37 * @throws SQLException 38 */ 39 //@AuthPassport 40 @RequestMapping(value={"/index","/hello"}) 41 public ModelAndView index() throws SQLException{ 42 43 //throw new SQLException("數據庫異常。"); 44 45 ModelAndView modelAndView = new ModelAndView(); 46 //在jsp中能夠經過 ${message} 的形式來獲取綁定的值 47 modelAndView.addObject("message", "Hello World!"); 48 modelAndView.setViewName("index"); 49 return modelAndView; 50 } 51 /** 52 * @RequestMapping 結合 @PathVariable 註解能夠獲取URL中帶佔位的那個參數值, 53 * 具體到這個例子中,在方法名中Integer id這個入參的值就是URL中的值, 54 * 假如URL爲: 55 * http://localhost:8080/SpringMVCTag/helloworld/detail/yangdandan 56 * 那麼在跳轉到的detail.jsp中經過 57 * ${cxrr}的形式就能夠獲取到 yangdandan 這個字符串 58 * @param id 59 * @return 60 */ 61 @RequestMapping(value="/detail/{thepara}", method = {RequestMethod.GET}) 62 public ModelAndView getDetail(@PathVariable(value="thepara") String thepara){ 63 64 ModelAndView modelAndView = new ModelAndView(); 65 modelAndView.addObject("crxx", thepara); 66 modelAndView.setViewName("detail"); 67 return modelAndView; 68 } 69 70 @RequestMapping(value="/reg/{name:\\w+}-{age:\\d+}", method = {RequestMethod.GET}) 71 public ModelAndView regUrlTest(@PathVariable(value="name") String name, @PathVariable(value="age") Integer age){ 72 73 ModelAndView modelAndView = new ModelAndView(); 74 modelAndView.addObject("name", name); 75 modelAndView.addObject("age", age); 76 modelAndView.setViewName("regurltest"); 77 return modelAndView; 78 } 79 80 @RequestMapping(value="/paramstest", params="example!=AAA", method = {RequestMethod.GET}) 81 public ModelAndView paramsTest(){ 82 83 ModelAndView modelAndView = new ModelAndView(); 84 modelAndView.setViewName("paramstest"); 85 return modelAndView; 86 } 87 88 }