编程知识 cdmana.com

Spring MVC Basics

1, annotation
@Controller
Responsible for registering a bean To spring context in
@Controller("tmpController")
Class names must start with lowercase letters

@RequestMapping("/register")
@RequestMapping(value="",method ={"",""},headers={},params={"",""})
method Request method method=RequestMethod.POST GET,HEAD,POST,PUT,DELETE
headers General combination method = RequestMethod.POST Use
value request URL ? *
params Request parameters {"param1 !=value1","param2"}

@RequestMapping("/{userId}")
Public ModelAndView showDetail(@PathVariable("userId") String userId){}
take URL The placeholder parameter in is bound to the parameter of the controller processing method

@RequestParam(value="",required=true,defaultValue="")
value Parameter name
required Default true, This parameter must be included , If not, an exception is thrown
defaultValue Default parameters

@ResponseBody
Method declares annotations @ResponseBody , The return value will be directly output to the page

@ModelAttribute("xxx")
Apply to method parameters , Parameters can be obtained directly from the page
@SessionAttributes
Can only be declared on a class , Appoint ModelMap Which attributes in need to be transferred to session in
@CookieValue
obtain cookie Information
@RequestHeader
Get the header information of the request

2, Return to model and view
@RequestMapping(method=RequestMethod.GET)
public ModelAndView index(){
ModelAndView mv = new ModelAndView("/user/index");
mv.addObject("xxx",xxx);
return mv;
}
@RequestMapping(method=RequestMethod.GET)
public ModelAndView index(){
ModelAndView mv = new ModelAndView();
mv.addObject("xxx",xxx);
mv.setViewName("/user/index");
return mv;
}
@RequestMapping(method=RequestMethod.GET)
public Map<String,String> index(){
Map<String,String> map = new HashMap<String,String>();
map.put("name",lsf);
return map;
}
@RequestMapping(method=RequestMethod.GET)
public String index(Model model){
String retVal="user/index";
List users=userService.getUsers();
model.addAttribute("users",users);
return retVal;
}
about String Return value of type , Constant cooperation Model Use it together
@RequestMapping(value="/valid",method=RequestMethod.GET)
public @ResponseBody String valid(@RequestParam(value="userId",required=false) Integer userId,
@RequestParam(value="logName") String strLogName){
return String.valueOf(!userService.isLogNameExist(strLogName,userId));
}
String Return type matching @ResponseBody Treat content or objects as HTTP The corresponding text returns ( Suitable for validation )
@RequestMapping("/welcome")
public void index(){
ModelAndView mv = new ModelAndView();
mv.addObject("xxx",xxx);
}
The returned view page corresponds to the access address , This example is welcome
Use void,map,Model when , Returns the corresponding logical view name url by :prefix Prefix + View name +suffix Suffixes make up

3, Additions and deletions
@RequestMapping(value="/persion",method=RequestMethod.POST)
public String post(Model model){
// Add logic
return "/result.jsp";
}
@RequestMapping(value="/person/{name}",method=RequestMethod.GET)
public String get(@PathVariable("name") String name,Model model){
// Look up logic by name
return "/result.jsp";
}
@RequestMapping(value="/person/{name}",method=RequestMethod.DELETE)
public String delete(@PathVariable("name") String name,Model model){
// Delete logic
return "/result.jsp";
}
@RequestMapping(value="/person/{name}",method=RequestMethod.PUT)
public String put(@PathVariable("name") String name,Model model){
// Update logic
return "/result.jsp";
}

4, Forward redirection
return "redirect:/index.jsp";
return "forward:/index.jsp";
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=2...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=2...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...
https://github.com/search?q=%...

版权声明
本文为[A Jie]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201225151730611l.html

Scroll to Top