SpringMVC(四):RequestMapping
全部标签2023-01-19一、@RequestMapping注解位置1、书写在类上面(1)作用:为当前类设置映射URL(2)注意:不能单独使用,需要与方法上的@RequestMapping配合使用2、书写在方法上面(1)作用:为当前方法设置映射URL(2)注意:可以单独使用3、示例代码(1)day09_springMVC/src/main/java/“com.hh.controller.EmployeeController”@Controller@RequestMapping("/EmpController")publicclassEmployeeController{@RequestMapping
2023-01-19一、@PathVariable注解基本使用1、获取URL中占位符2、占位符语法:{}3、实例代码:@RequestMapping("testPathVariable/{empId}")publicStringtestPathVariable(@PathVariable("empId")IntegerempId){System.out.println("empId="+empId);returnSUCCESS;}ath:href="@{/EmpController/testPathVariable/1001}">测试testPathVariablea>br>二、@PathVa
2023-01-19一、@PathVariable注解基本使用1、获取URL中占位符2、占位符语法:{}3、实例代码:@RequestMapping("testPathVariable/{empId}")publicStringtestPathVariable(@PathVariable("empId")IntegerempId){System.out.println("empId="+empId);returnSUCCESS;}ath:href="@{/EmpController/testPathVariable/1001}">测试testPathVariablea>br>二、@PathVa
2023-01-20一、SpringMVC处理响应数据1、处理响应数据方式一(1)语法:使用ModelAndView对象作为返回值类型,处理响应数据(2)底层实现原理①数据共享到request域②跳转路径方式:转发(3)示例代码@RequestMapping("/testModelAndView")publicModelAndViewtestModelAndView(){ModelAndViewmv=newModelAndView();//设置modelmv.addObject("stuName","zhangsan");//设置viewmv.setViewName("response_suc
2023-01-20一、SpringMVC处理响应数据1、处理响应数据方式一(1)语法:使用ModelAndView对象作为返回值类型,处理响应数据(2)底层实现原理①数据共享到request域②跳转路径方式:转发(3)示例代码@RequestMapping("/testModelAndView")publicModelAndViewtestModelAndView(){ModelAndViewmv=newModelAndView();//设置modelmv.addObject("stuName","zhangsan");//设置viewmv.setViewName("response_suc
2023-01-20一、springMVC中视图及视图解析器对象1、视图解析器对象(ViewResolver)(1)概述:SpringMVC中所有视图解析器对象均实现ViewResolver接口(2)作用:使用ViewResolver,将View从ModelAndView中解析出来注:在springMVC中无论方法返回的是ModelAndView还是String,最终底层封装为ModelAndView2、视图对象(View)(1)概述:SpringMVC中所有视图对象(view)均实现的View接口(2)作用:视图渲染①将数据共享到域中(request、session、application(
2023-01-20一、springMVC中视图及视图解析器对象1、视图解析器对象(ViewResolver)(1)概述:SpringMVC中所有视图解析器对象均实现ViewResolver接口(2)作用:使用ViewResolver,将View从ModelAndView中解析出来注:在springMVC中无论方法返回的是ModelAndView还是String,最终底层封装为ModelAndView2、视图对象(View)(1)概述:SpringMVC中所有视图对象(view)均实现的View接口(2)作用:视图渲染①将数据共享到域中(request、session、application(
2023-01-20一、SpringMVC消息转换器概述1、HttpMessageConverter消息转换器作用:(1)将java对象与请求报文及响应报文进行相互转化(2)使用HttpMessageConverter将请求信息转化并绑定到处理方法的入参中或将响应结果转为对应类型的响应信息,Spring提供了两种途径:①使用@RequestBody/@@ResponseBody对处理方式进行标注。②使用HttpEntity/ResponseEntity作为处理方法的入参或返回值。二、使用消息转换器处理请求报文1、使用@RequestBody获取请求体(1)语法finalstaticString
2023-01-20一、SpringMVC消息转换器概述1、HttpMessageConverter消息转换器作用:(1)将java对象与请求报文及响应报文进行相互转化(2)使用HttpMessageConverter将请求信息转化并绑定到处理方法的入参中或将响应结果转为对应类型的响应信息,Spring提供了两种途径:①使用@RequestBody/@@ResponseBody对处理方式进行标注。②使用HttpEntity/ResponseEntity作为处理方法的入参或返回值。二、使用消息转换器处理请求报文1、使用@RequestBody获取请求体(1)语法finalstaticString
2023-01-21一、文件下载1、实现文件下载步骤(1)准备文件下载相关步骤(2)将ResponseEntity对象,作为方法返回值(3)为ResponseEntity对象,设置三个参数 2、示例代码@RequestMapping("/fileDownloadController")publicResponseEntitybyte[]>fileDownload(HttpServletRequestrequest,Stringfilename){ResponseEntitybyte[]>responseEntity=null;try{//获取文件位置//获取文件真实路径【(request|se