SpringMVC笔记(2)注解及参数

孤街浪徒 提交于 2020-02-28 05:46:59

添加访问路径

@RequestMapping("/ttt")本类的访问路径,本类中的方法使用路径/ttt来访问
008.PNG

009.PNG

010.PNG

@RequestMapping()中的参数

011.PNG

@RequestMapping(value={"/aaa","/bbb"})这样aaa.do和bbb.do都可以访问该方法

012.PNG

@RequestMapping(value="/fff/aaa",method=RequestMethod.POST),请求方式必须是post

013.PNG

@RequestMapping(value="/fff/bbb",params=“id”)请求必须有id参数没有的话404

014.PNG

@RequestMapping(value="/fff/ccc",params={“id”,“name”,“img”})请求必须有id,name,img,没有访问不到404

015.PNG

@RequestMapping(value="/fff/ddd",headers=“cookie”)请求头里有cookie

016.PNG

方法中的参数 /注意JavaBean对象的get,set方法要标准

017.PNG

参数的注解形式

@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

018.PNG

使用Model,ModelMap,ModelAndView,默认保存在request中

019.PNG

//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;
	}

重定向和转发

020.PNG

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!