springmvc学习笔记

xiaoxiao2021-02-28  9

1.web.xml配置dispatchServlet和xml文件地址(这里就不说了) 2.springmvc的注解 @Controller @Service @RequestMapping

@ResponseBody @RequestMapping("/user") public User get(){ User u = new User(); u.setId(1); u.setName("jayjay"); u.setBirth(new Date()); return u; }

@ResponseBody 该注解用于读取Request请求的body部分数据,使用系统默认配置的HttpMessageConverter进行解析,然后把相应的数据绑定到要返回的对象上 ,再把HttpMessageConverter返回的对象数据绑定到 controller中方法的参数上 @ModelAttribute      在方法定义上使用 @ModelAttribute 注解:Spring MVC 在调用目标处理方法前,会先逐个调用在方法级上标注了@ModelAttribute 的方法   在方法的入参前使用 @ModelAttribute 注解:可以从隐含对象中获取隐含的模型数据中获取对象,再将请求参数 –绑定到对象中,再传入入参将方法入参对象添加到模型中 @RequestParam    在处理方法入参处使用 @RequestParam 可以把请求参 数传递给请求方法   

@RequestMapping(value="/param") public String testRequestParam(@RequestParam(value="id") Integer id, @RequestParam(value="name")String name){ System.out.println(id+" "+name); return "/hello"; }

@PathVariable   绑定 URL 占位符到入参

@RequestMapping(value="/user/{id}",method=RequestMethod.GET) public String get(@PathVariable("id") Integer id){ System.out.println("get"+id); return "/hello"; }

@ExceptionHandler   注解到方法上,出现异常时会执行该方法   

@ExceptionHandler public ModelAndView exceptionHandler(Exception ex){ ModelAndView mv = new ModelAndView("error"); mv.addObject("exception", ex); System.out.println("in testExceptionHandler"); return mv; } @RequestMapping("/error") public String error(){ int i = 5/0; return "hello"; }

@ControllerAdvice   使一个Contoller成为全局的异常处理类,类中用@ExceptionHandler方法注解的方法可以处理所有Controller发生的异常   

处理全局异常(所有Controller) 复制代码 @ControllerAdvice public class testControllerAdvice { @ExceptionHandler public ModelAndView exceptionHandler(Exception ex){ ModelAndView mv = new ModelAndView("error"); mv.addObject("exception", ex); System.out.println("in testControllerAdvice"); return mv; } }

3.自动匹配参数//match automatically

@RequestMapping("/user") public String user(String name,Long age){ System.out.println(name+" "+age); return "hello"; }

自动装箱

//boxing automatically @RequestMapping("/user1") public String user1(User u){ System.out.println(u.getName()+" "+u.getAge()); return "hello"; }

4.文件上传 需要comons-fileupload.jar和commons-io.jar

<!-- upload settings --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="102400000"></property> </bean> <form action="mvc/upload" method="post" enctype="multipart/form-data"> <input type="file" name="file"><br> <input type="submit" value="submit"> </form> @RequestMapping(value="/upload",method=RequestMethod.POST) public String upload(HttpServletRequest req) throws Exception{ MultipartHttpServletRequest mreq = (MultipartHttpServletRequest)req; MultipartFile file = mreq.getFile("file"); String fileName = file.getOriginalFilename(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); FileOutputStream fos = new FileOutputStream(req.getSession().getServletContext().getRealPath("/")+ "upload/"+sdf.format(new Date())+fileName.substring(fileName.lastIndexOf('.'))); fos.write(file.getBytes()); fos.flush(); fos.close(); return "hello"; }

5.自定义拦截器 implement HandlerInterceptor 类

<!-- interceptor setting --> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/mvc/**"/> <bean class="test.SpringMVC.Interceptor.MyInterceptor"></bean> </mvc:interceptor> </mvc:interceptors>

6.springmvc的异常处理 见注解那部分

7.springmvc对时间Date的处理

转载请注明原文地址: https://www.6miu.com/read-2400392.html

最新回复(0)