添加访问路径
@RequestMapping("/ttt")本类的访问路径,本类中的方法使用路径/ttt来访问
@RequestMapping()中的参数
@RequestMapping(value={"/aaa","/bbb"})这样aaa.do和bbb.do都可以访问该方法
@RequestMapping(value="/fff/aaa",method=RequestMethod.POST),请求方式必须是post
@RequestMapping(value="/fff/bbb",params=“id”)请求必须有id参数没有的话404
@RequestMapping(value="/fff/ccc",params={“id”,“name”,“img”})请求必须有id,name,img,没有访问不到404
@RequestMapping(value="/fff/ddd",headers=“cookie”)请求头里有cookie
方法中的参数 /注意JavaBean对象的get,set方法要标准
参数的注解形式
@RequestMapping("/aaa")
public String f1(
@RequestParam("xxx") String name,
@RequestParam(name="yyy",required=false,defaultValue="22") int age
){
System.out.println("日志2...方法中参数 接收 "+name+"\t"+age);
return "redirect:/test1.jsp";
}
把xxx的值分装到name中,xxx必须要有否则404
yyy可以没有,默认是22,封装到age中
@RequestMapping("/bbb")
public String f1(
@CookieValue("img") String img ,
@CookieValue("JSESSIONID") String sid,
String name
) {
System.out.println("日志2...方法中参数 接收 "+img+"\t"+sid+"\t"+name);
return "redirect:/test1.jsp";
}//
接收cookie的值,另外String name可以为空
@RequestMapping("/ccc/{xxx}/{yyy}")
public String f1( @PathVariable("xxx") String name , @PathVariable("yyy") String id) {
System.out.println("日志2...方法中参数 接收 "+name+"\t"+id);
return "redirect:/test1.jsp";
}//
<a href="<%=path %>/ttt/ccc/aa2/10.do">ttt/ccc/xxx/yyy</a><br/>
@RequestMapping("/ddd/{xxx}/{yyy}/uuu")
public String f2( @PathVariable("xxx") String name , @PathVariable("yyy") String id) {
System.out.println("日志3...方法中参数 接收 "+name+"\t"+id);
return "redirect:/test1.jsp";
}//
<a href="<%=path %>/ttt/ddd/aa3/20/uuu.do">ttt/ddd/xxx/yyy/uuu</a><br/>
在请求的路径中发送参数
访问web资源request,session,application
使用Model,ModelMap,ModelAndView,默认保存在request中
//ModelAndView mv = new ModelAndView(); 这个不用写在f3()形参中,可以new
@RequestMapping("/aaa")
public String f1(Model md){
System.out.println(this.getClass() + "日志1... f1()");
md.addAttribute("kkk", "String in Model & HttpServletRequest");
return "test2";
}
@RequestMapping("/bbb")
public String f2(ModelMap mdMap){
System.out.println(this.getClass() + "日志2.1.. f2()");
mdMap.addAttribute("kkk", "String in ModelMap & HttpServletRequest");
return "test2";
}
@RequestMapping("/ccc")
public ModelAndView f3(ModelAndView mv){
System.out.println(this.getClass() + "日志3.1... f3() & HttpServletRequest");
//ModelAndView mv = new ModelAndView(); 这个不用写在f3()形参中,可以new
mv.addObject("kkk", "String in ModelAndView");
mv.setViewName("test2");
return mv;
}
检测model/modelMap/modelAndView中若包含kkk,uuu,vvv, 再给Session中保存一份
@RequestMapping("/aaa")
public String f1(Model md){
System.out.println(this.getClass() + "日志1... f1()");
md.addAttribute("kkk", "String in Model & HttpServletRequest");
return "test2";
}
@RequestMapping("/bbb")
public String f2(ModelMap mdMap){
System.out.println(this.getClass() + "日志2.1.. f2()");
mdMap.addAttribute("kkk", "String in ModelMap & HttpServletRequest");
return "test2";
}
@RequestMapping("/ccc")
public ModelAndView f3(ModelAndView mv){
System.out.println(this.getClass() + "日志3.1... f3() & HttpServletRequest");
//ModelAndView mv = new ModelAndView(); 这个不用写在f3()形参中,可以new
mv.addObject("kkk", "String in ModelAndView & HttpServletRequest ");
mv.setViewName("test2");
return mv;
}
重定向和转发
来源:CSDN
作者:土豆泥@
链接:https://blog.csdn.net/qq_43674416/article/details/104533809