• SpringMVC丶


    官方

    https://docs.spring.io/spring-framework/docs/current/reference/html/web.html

    image-20201124102042933

    创建项目

    • 父工程依赖
    • 设置web支持

    image-20201124091527272

    • lib套路(与classes同级)

    image-20201124104315409

    • web.xml

    • SpringMVC配置文件springmvc-servlet.xml

    • 新建页面

    • XxxController

    @Controller//   @RestController不走视图解析器
    @RequestMapping("/hello")
    public class HelloController {
        @RequestMapping("/h1")
        //@ResponseBody//不走视图解析器,直接返回字符串(配合@Controller使用的)
        public String hello(Model model) {
            //封装数据
            model.addAttribute("msg","hello springmvcAnnotation");
            return "hello";
        }
    }
    

    SpringMVC执行流程

    img

    RestFul风格

    @Controller
    public class ControllerTest4 {
        @RequestMapping("/add/{a}/{b}")
        public String test1(@PathVariable int a, @PathVariable int b, Model model) {
            //@PathVariable 与 @RequestParam 的区别
            int result = a + b;
            model.addAttribute("msg","结果为:"+result);
            return "test";
        }
    }
    

    整合SSM

    源码

    拦截器

    mvc配置文件

    <!--拦截器配置-->
    <mvc:interceptors>
        <mvc:interceptor>
            <!--包括请求下的所有请求-->
            <mvc:mapping path="/**"/>
            <bean class="com.hello.config.MyInterceptor"/>
        </mvc:interceptor>
    </mvc:interceptors>
    

    实现 HandlerInterceptor 接口

    public class MyInterceptor implements HandlerInterceptor {
        //return true 放行  执行下一个拦截器
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            System.out.println("============处理前==============");
            return true;
        }
    }
    

    文件上传

    依赖

    <!--文件上传-->
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.3.3</version>
    </dependency>
    <!--这个包与 javax.servlet-->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
    </dependency>
    
    <form action="${pageContext.request.contextPath}/upload" enctype="multipart/form-data" method="post">
        <input type="file" name="file"><br><br>
        <input type="submit" value="上传">
    </form>
    

    javax.servlet-api对应代码

    @Controller
    public class FileController {
    
        @RequestMapping("/upload")
        public String fileUpload(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException {
            String uploadFilename = file.getOriginalFilename();
            if ("".equals(uploadFilename)) {
                return "redirect:/index.jsp";
            }
            System.out.println("上传文件名:" + uploadFilename);
            String path = request.getServletContext().getRealPath("/upload");
            File realPath = new File(path);
            if (!realPath.exists()) {
                realPath.mkdir();
            }
            System.out.println("上传文件保存地址:" + realPath);
    
            InputStream is = file.getInputStream();
            FileOutputStream os = new FileOutputStream(new File(realPath, uploadFilename));
    
            int len = 0;
            byte[] buffer = new byte[1024];
            while ((is.read(buffer)) != -1) {
                os.write(buffer, 0, len);
                os.flush();
            }
            os.close();
            is.close();
            return "redirect:/index.jsp";
    
        }
    
        @RequestMapping("/upload2")
        public String fileUpload2(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException {
            String path = request.getServletContext().getRealPath("/upload");
            File realPath = new File(path);
            if (!realPath.exists()) {
                realPath.mkdir();
            }
            System.out.println("上传文件保存地址:" + realPath);
            file.transferTo(new File(realPath + "/" + file.getOriginalFilename()));
            return "redirect:/index.jsp";
        }
    }
    
    击石乃有火,不击元无烟!!
  • 相关阅读:
    程序员是脑力劳动还是体力劳动
    我理解的技术管理的核心工作----搭班子和带团队
    Ubuntu系统下在PyCharm里用virtualenv集成TensorFlow
    我理解的技术管理的核心工作----定战略
    数据分析师岗位的一点理解
    python中读写excel并存入mysql
    mac里用PyCharm中引用MySqlDB始末
    贝叶斯网络的通俗解读
    将sqlserver导出的csv数据导入到ubuntu和mac上的mysql
    Java之Spring Cloud概念介绍(非原创)
  • 原文地址:https://www.cnblogs.com/rain2020/p/14026333.html
Copyright © 2020-2023  润新知