springmvc面试复习

一,什么是springmvc?

  SpringMVC是一种基于Java的实现MVC设计模式的请求驱动类型的轻量级Web框架,使用了MVC架构模式的思想,将web层进行职责解耦,基于请求驱动指的就是使用请求-响应模型,框架的目的就是帮助我们简化开发。

 

二,springmvc请求流程

 

整个架构的具体流程:

(1)用户发送请求 ——> DispatcherServlet ,前端控制器拿到请求后并不自己处理,而是交给其他处理器进行处理,这里只是作为一个统一访问点,进行全局流程的控制。

(2)DispatcherServlet ——> HandlerMapping,处理器映射器会将请求映射为HandlerExecutionChain对象返回给DispatcherServlet,该对象里面包含了一个Handler处理器对象和多个HandlerInterceptor拦截器对象(如果设置了拦截器则生成拦截器对象,没有则不生成)。

(3)DispatcherServlet ——> HandlerAdapter,处理器适配器将传递过来的处理器包装成适配器,从而很容易支持了多种类型的处理器,这也是适配器设计模式的应用。

(4)HandlerAdapter ——> 调用执行处理器相应功能,处理方法(就是需要我们自己写的Controller类和方法),并且返回一个ModelAndView对象(包含了模型数据、逻辑视图名),再由HandlerAdapter返回给前端控制器。

扫描二维码关注公众号,回复: 6974154 查看本文章

        ModelAndView :Model部分是业务对象返回的模型数据,View部分是逻辑视图名。

(5)DispatcherServlet ——> ViewResolver,视图解析器将逻辑视图名解析为具体的View(不包含模型数据)。首先根据逻辑视图名解析成物理视图名即具体的页面地址,再生成View视图对象。

(6)DispatcherServlet ——> View,此时View进行视图渲染,即将模型数据进行渲染。此时的Model实际上是一个Map数据结构。

(7)DispatcherServlet ——> 响应用户。

 

三,springmvc注解

   @Controller :在对应的方法上,视图解析器可以解析return 的jsp,html页面,并且跳转到相应页面;

   @RestController :相当于@Controller+@ResponseBody

   @ResponseBody:返回json字符串

   @RequestMapping: 设置请求路径

   @RequestMapping(value="/getName",method=RequestMethod.GET,params={"id=123","name","!age")

 //上述规则定义了,只能响应get请求,并且请求的参数必须包含id=123,必须包含name,不能包含age//根据上述规则此地址合法:http://localhost:8080/xx?id=123&name=abc

   前端参数获取

  1,可以直接用属性名相同隐射。public String f(String name,String age){}

  2,将前端的传递的数据封装为一个对象,name值和对象字段名相同即可。public String f(Person person){}

  3,使用注解@RequestParma(value=“name”,defaultValue="tc"):获取name值,并设置默认值。public String f(@RequestParma(“name”)String name){}

  4,使用PathVariable:本注解将请求数据绑定在请求路径中

    请求路径:RequestMapping("../{name}/{age}")

   public String f(@PathVariable("name")String name,@PathVariable("age")String age){return “redirect:”/  “forward:”}

   redirect:重定向;forward:请求转发;

  5,@RequestBody:接收json数据;public String f(@RequestBody Person person){}

四,文件上传

/*
采用三种方式来接收上传文件,分别用三个方法试验
每个方法里,有程序执行时间记录
实际运用时,三者选其一
第一种最慢,第三种最快,一般选用第二种
*/
@Controller
public class FileController { /* * 通过流的方式上传文件 * * @RequestParam("file") 将name=file控件得到的文件封装成CommonsMultipartFile 对象 */ @RequestMapping("fileUpload") public String fileUpload(@RequestParam("file") CommonsMultipartFile file) throws IOException { // 用来检测程序运行时间 long startTime = System.currentTimeMillis(); try { // 获取输出流 OutputStream os = new FileOutputStream("E:/" + new Date().getTime() + file.getOriginalFilename()); // 获取输入流 CommonsMultipartFile 中可以直接得到文件的流 InputStream is = file.getInputStream(); int temp; // 一个一个字节的读取并写入 while ((temp = is.read()) != (-1)) { os.write(temp); } os.flush(); os.close(); is.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } long endTime = System.currentTimeMillis(); System.out.println("方法一的运行时间:" + String.valueOf(endTime - startTime) + "ms"); return "/success"; } /* * 采用file.Transto 来保存上传的文件 */ @RequestMapping("fileUpload2") public String fileUpload2(@RequestParam("file") CommonsMultipartFile file) throws IOException { long startTime = System.currentTimeMillis(); String path = "E:/" + new Date().getTime() + file.getOriginalFilename(); File newFile = new File(path); // 通过CommonsMultipartFile的方法直接写文件(注意这个时候) file.transferTo(newFile); long endTime = System.currentTimeMillis(); System.out.println("方法二的运行时间:" + String.valueOf(endTime - startTime) + "ms"); return "/success"; } /* * 采用spring提供的上传文件的方法 */ @RequestMapping("springUpload") public String springUpload(HttpServletRequest request) throws IllegalStateException, IOException { long startTime = System.currentTimeMillis(); // 将当前上下文初始化给 CommonsMutipartResolver (多部分解析器) CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext()); // 检查form中是否有enctype="multipart/form-data" if (multipartResolver.isMultipart(request)) { // 将request变成多部分request MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; // 获取multiRequest 中所有的文件名 Iterator iter = multiRequest.getFileNames(); while (iter.hasNext()) { // 一次遍历所有文件 MultipartFile file = multiRequest.getFile(iter.next().toString()); if (file != null) { String path = "E:/springUpload" + file.getOriginalFilename(); // 上传 file.transferTo(new File(path)); } } } long endTime = System.currentTimeMillis(); System.out.println("方法三的运行时间:" + String.valueOf(endTime - startTime) + "ms"); return "/success"; } }

 前端页面:

<!-- 三个form分别对应controller里三个上传方法 -->
    <form name="Form1" action="fileUpload.v" method="post" enctype="multipart/form-data"> <h1>采用流的方式上传文件</h1> <input type="file" name="file"> <input type="submit" value="upload" /> </form> <form name="Form2" action="fileUpload2.v" method="post" enctype="multipart/form-data"> <h1>采用multipart提供的file.transfer方法上传文件</h1> <input type="file" name="file"> <input type="submit" value="upload" /> </form> <form name="Form3" action="springUpload.v" method="post" enctype="multipart/form-data"> <h1>使用spring mvc提供的类的方法上传文件</h1> <input type="file" name="file"> <input type="submit" value="upload" /> </form>

---------------------
参考链接
https://blog.csdn.net/weixin_42621338/article/details/87204868

https://www.jianshu.com/p/fbc6953e5af4

 

 

猜你喜欢

转载自www.cnblogs.com/kobe24vs23/p/11316464.html