@Controller ajax
標註在Bean的類定義處spring
@RequestMappingsession
真正讓Bean具有 Spring MVC Controller 功能的是 @RequestMapping 這個註解mvc
@RequestMapping 能夠標註在類定義處,將 Controller 和特定請求關聯起來;app
還能夠標註在方法簽名處,以便進一步對請求進行分流post
配套的屬性有:this
value 須要跳轉的地址url
method 基於RestFul的跳轉參數,有RequestMethod.get post put delete等spa
params 符合某個參數的時候才調用該方法.net
Headers 符合頭信息的時候才調用
@SessionAttributes
將結果放入session內
@ModelAttribute
存儲在響應內容ModelMap或者ModelAndView進行保存值傳到前臺,當若是你須要保存值比較少
的時候能夠採用這種方式進行保存值而且保存到前臺顯示
在默認狀況下,ModelMap 中的屬性做用域是 request 級別,至關於HttpServletRequest中的request.setAttribute() 同樣,在 JSP 視圖頁面中經過 request.getAttribute(「attribute name」) 或者經過
${ attribute name } EL 表達式訪問模型對象中的 屬性對象
若是但願在ModelMap 的做用域範圍爲 session,能夠有選擇地指定 ModelMap 中的哪些屬性須要轉存到 session 中,以便下一個請求屬對應的 ModelMap 的屬性列表中還能訪問到這些屬性。這一功能是經過類定義處標註 @SessionAttributes 註解來實現 如:
@Controller
@RequestMapping("/login.do")
@SessionAttributes("currUser")
public class BbtForumController {。。。。。}
@ResponseBody
標註後 返回String對象的結果爲response內容體,不標註的話 做爲dispatcher url使用
@PathVariable
容許將請求路徑的制定內容當作求情的參數使用
請求處理方法入參的可選類型 說明
void 此時邏輯視圖名由請求處理方法對應的 URL 肯定,如如下的方法:
@RequestMapping("/welcome.do")
public void welcomeHandler() {
}
對應的邏輯視圖名爲「welcome」
String 此時邏輯視圖名爲返回的字符,如如下的方法:
@RequestMapping(method = RequestMethod.GET)
public String setupForm(@RequestParam("ownerId") int ownerId, ModelMap model) {
Owner owner = this.clinic.loadOwner(ownerId);
model.addAttribute(owner);
return "ownerForm";
}
對應的邏輯視圖名爲「ownerForm」
ModelMap 和返回類型爲 void 同樣,邏輯視圖名取決於對應請求的 URL,
以下面的例子:
@RequestMapping("/vets.do")
public ModelMap vetsHandler() {
return new ModelMap(this.clinic.getVets());
}
對應的邏輯視圖名爲「vets」,返回的 ModelMap 將被做爲請求對應的模型對象,
能夠在 JSP 視圖頁面中訪問到。
ModelAndView
1 使用無返回方法跳轉,若是使用返回方法進行跳轉的話,則會經過視圖解析器進行以
prefix(前綴)+方法名+suffix(後綴)組成的頁面文件名稱.
2 使用一個返回的字符串方法做爲跳轉,使用字符串跳轉的話好處就是在return的時候可
以本身指定返回的名字,JSP組成是prefix(前綴)+返回的字符串+suffix(後綴)
3 返回一個ModelAndView類型,使用setViewName方法則能夠跳轉到指定的頁面.
1、單一Controller 對應 單一的請求路徑
2、單一Controller 對應多個請求路徑
3、單一Controller 對應多個請求路徑,且路徑內能夠含有參數的形式
@Controller
@RequestMapping("/login.do")
public class SinglePathWithController {}
@Controller
@SessionAttributes(types = {UserBean.class,String.class},value={"currentUser","message"})
public class AdapterMultiPathController {}
@Controller
@RequestMapping(value = "/rest")
public class RestWithController {}
//無返回值 無參數返回的是根據 prefix前綴+@RequestMapping value +suffix
後綴組成
@RequestMapping("/springmvc/common")
public voidnovoid(HttpServletRequest request) {
request.setAttribute("message", "novoid方法被調用");
}
一、 做爲視圖路徑方式
//根據路徑直接匹配
@RequestMapping("/springmvc/multiReqPath1.do")
public String multiReqPath1(HttpServletRequest request){
request.setAttribute("message", "multiReqPath1方法被調用");
return "springmvc/common";
}
@RequestMapping("/springmvc/multiReqPath2.do")
public String multiReqPath2(HttpServletRequest request){
request.setAttribute("message", "multiReqPath2方法被調用");
return "/springmvc/common";
}
//根據參數匹配
@RequestMapping(params = "m=method1",method = RequestMethod.GET)
public String method1(){
return "login/success";
}
//有參數 參數名和請求url內的變量名一致
@RequestMapping(params = "m=method2")
public String method2(String name,String pwd){
return name;
}
//有參數 參數名和請求url內的變量名不一致
@RequestMapping(params = "m=method3",method = RequestMethod.GET)
public String method3(@RequestParam("loginName")Stringname,@RequestParam("loginPwd")String pwd,HttpServletRequest request){
request.setAttribute("message",(name + " " + pwd));
return "login/"+name;
}
二、 做爲Response內容方式
//無參數
@ResponseBody
@RequestMapping(params = "m=method4")
public String method4(){
return "hello,guys";
}
//處理方法入參如何綁定 URL 參數
@ResponseBody
@RequestMapping(params = "m=method5",method = RequestMethod.GET)
public String method5(String name,String pwd,int delay){
return "name:"+name+","+"pwd:"+pwd+","+"delay:"+delay;
}
@ResponseBody
@RequestMapping(params = "m=method6",method = RequestMethod.GET)
public String method6(@RequestParam("userName")String name,DnTest test){
return "DnTest:"+test.toString();
}
URL 參數: userName參數將綁定到name上 其餘與DnTest類內屬性名稱一致的參數將綁定到test的對應的屬性上,若是參數不全 也不會報錯
@RequestMapping("/springmvc/modelAndView")
public ModelAndView modelAndView(){
ModelAndView mav = new ModelAndView();
mav.setViewName("/springmvc/common");
mav.addObject("message", "modelAndView 方法被調用");
return mav;
}
@RequestMapping("/springmvc/modelMap")
public ModelMap modelMap(ModelMap modMap){
List<String> names = new ArrayList<String>();
names.add("Rick");
names.add("Austin");
modMap.put("names", names);
modMap.put("message", "hello guys");
modMap.put("comment", "hello guys");
return modMap;
}
@RequestMapping("/springmvc/modelMap")
public ModelMap modelAndView(ModelMap modMap){
List<String> names = new ArrayList<String>();
names.add("Rick");
names.add("Austin");
modMap.put("hello", "hello guys");
modMap.put("names", names);
return modMap;
}
//註解方式
@Controller
@SessionAttributes(types = {UserBean.class,String.class},value={"currentUser","message"})
public class AdapterMultiPathController {}
//方法體
@RequestMapping("/springmvc/modelMap2")
public ModelMap modelMapWithSession(ModelMap modMap,HttpServletRequest request){
List<String> names = new ArrayList<String>();
names.add("Rick");
names.add("Austin");
modMap.put("names",names);
modMap.put("message", "hello guys");
modMap.put("comment", "hello guys");
UserBean user = new UserBean();
user.setName("Rick");
user.setMobile("18938900256");
user.setTelephone(request.getParameter("userPhone"));
user.setNumber(request.getParameter("userNumber"));
modMap.put("currentUser", user);
return modMap;
}
//初次請求
@ResponseBody
@RequestMapping(params = "m=method7",method = RequestMethod.GET)
public String method7(String name,String pwd,int delay,HttpServletRequest req){
req.startAsync();
Date startTime = new Date();
try {
Thread.currentThread().sleep(delay);
} catch (InterruptedException e) {
e.printStackTrace();
}
Date entTime = new Date();
return "name:"+name+","+"pwd:"+pwd+","+"delay:"+delay+",startTime:"+
DateUtils.formatDate(startTime, "yyyy-MM-dd HH:mm:ss:SSS")+",endTime:"+
DateUtils.formatDate(entTime, "yyyy-MM-dd HH:mm:ss:SSS");
}
@Controller
@RequestMapping(value = "/rest")
public class RestWithController {}
@ResponseBody
@RequestMapping(value = "/{msg}", method = RequestMethod.GET)
public String restString(@PathVariable String msg) {
return msg;
}
@ResponseBody
@RequestMapping(value = "/{path}/{value}", method = RequestMethod.GET)
public String restXml(@PathVariable String path,@PathVariable String value) {
return "path:"+path+",value:"+value;
}
@ResponseBody
@RequestMapping(value = "/xml/{filename}", method = RequestMethod.GET)
public String restFile(@PathVariable String filename) {
if (filename!=null) {
ProjectInits init = ProjectInits.getInstance();
String dir = init.get("resource.dir", "C:/Projects/VoyagerWeb/resources");
FileUtility fUtil = new FileUtility();
String content = fUtil.readFile(dir+"/"+filename+".xml");
return content;
}
else
return "Invalid xml file name ["+filename+"]";
}
//驗證 是否支持Overload
@ResponseBody
@RequestMapping(value = "/validate/overload1", method = RequestMethod.GET)
public String overloadMethod(String name){
return name;
}
@ResponseBody
@RequestMapping(value = "/validate/overload2", method = RequestMethod.GET)
public String overloadMethod(String name,DnTest test){
return "DnTest:"+test.toString();
}
/驗證 是否支持Overload
@ResponseBody
@RequestMapping(params = "m=method11")
public String method11(String name){
return name;
}
@ResponseBody
@RequestMapping(params = "m=method11")
public String method11(int age,DnTest test){
return "DnTest:"+test.toString();
}