美文网首页
SpringMVC第二天

SpringMVC第二天

作者: 原来蜗牛不是牛 | 来源:发表于2017-07-22 10:05 被阅读0次

    回顾SpringMVC的工作原理

    客户发送的请求,首先由SpringMVC的前端控制器进行拦截,然后交给处理器映射器去找要请求的action,处理器映射器会返回具体的地址给前端控制器,前端控制器把具体的地址给处理器适配器,处理器适配器去调用(执行)具体的action.执行后会返回ModelandView,由前端控制器交给视图解析器进行解析返回View,视图渲染返回给客户。

    SpringMVC的参数绑定方式

    1. 使用request.getParameter();l来获取页面传过来的参数
    2. 自定义封装类(DTO)
      DTO:DTO的里面封装的属性主要是页面和web交互的数据,可能来自一个Model的属性,也可能来自多个Model的属性,如果页面提交数据很多,就避免使用多个request来得到属性
      注意点:页面上使用的具体控件字段的名称需要和封装类中的属性一致。
      在页面提交数据后,在Action里面可以这样写,在方法的参数列表里面传入一个DTO对象
    @RequestMapping("/addType.do")
        public ModelAndView addType(BookTypeDTO dto,HttpServletRequest request,HttpServletResponse response){
            ModelAndView mv = new ModelAndView();
            //获得页面上传递过来的数据
            String typename = request.getParameter("typename");
            String typedesc = request.getParameter("typedesc");
            System.out.println(typename+"\t"+typedesc);
            //从DTO中获取数据
            System.out.println("从DTO中获取:"+dto.getTypename()+"\t"+dto.getTypedesc());
            mv.setViewName("/index.jsp");
            return mv;
    

    关联对象:


    DTO关联对象
    @RequestMapping("/addType.do")
        public ModelAndView addType(HttpServletRequest request,HttpServletResponse response,BookTypeDTO dto){
            ModelAndView mv = new ModelAndView();
            //获得页面上传递过来的数据
            String typename = request.getParameter("typename");
            String typedesc = request.getParameter("typedesc");
            System.out.println(typename+"\t"+typedesc);
            //从DTO中获取数据
            System.out.println("从DTO中获取:"+dto.getTypename()+"\t"+dto.getTypedesc());
            //从DTO中得到来源
            System.out.println(dto.getUserDTO().getUname());
            mv.setViewName("/index.jsp");
            return mv;
            
        }
    

    重定向方式

    @RequestMapping("/redirect.action")
        protected String redirectTest(HttpServletRequest request,HttpServletResponse response){
            System.out.println("redirectTest");
            
            return "redirect:test.action";//地址栏改变
        }
    

    文件上传

    • 依赖包(当然也要导入spring的依赖了)
    <!-- 文件上传 -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.2.2</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.0.1</version>
        </dependency>
    
    • 表单类型
    <form action="upload.action" method="post" enctype="multipart/form-data" >
            用户名:<input type="text" name="name"/><br>
            照片:<input type="file" name="image"  /><br>
            <input type="submit" value="提交" />
        </form>
    

    注意 enctype="multipart/form-data"

    • 在spring配置文件中配置文件解析器
    <!-- 配置文件解析器 -->
        <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" >
            <property name="maxUploadSize" value="10240000"></property>
        </bean>
    
    • action接收页面提交数据
    @Controller
    public class UploadAction {
        
        @RequestMapping("upload.action")
        protected ModelAndView upload(HttpServletRequest request,HttpServletResponse response,@RequestParam("image")MultipartFile mf){
            ModelAndView mav = new ModelAndView();
            System.out.println(request.getParameter("name"));
            //得到上传文件名
            String fileName = mf.getOriginalFilename();
            //得到上传目录
            String realPath = request.getServletContext().getRealPath("upload");
            System.out.println(realPath);
            //定义上传的目标文件
            File file = new File(realPath+"\\"+fileName);
            try {
                mf.transferTo(file);
                System.out.println("上传成功!!");
            } catch (IllegalStateException | IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            mav.setViewName("/index.jsp");
            return mav;
        }
    } 
    

    @RequestParam("image"):接收页面指定name的标签传过来的数据

    JSON处理

    • 导入依赖
    <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.7.4</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.7.4</version>
        </dependency>       
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.7.4</version>
        </dependency>
    
    • 使用注解ResponseBody,使方法的返回值为转为json
        @RequestMapping("/show.action")
        @ResponseBody
        protected  List<TCar> showAllCar(HttpServletRequest request,HttpServletResponse response){
            
            return tCarService.findAllCar();
        }
    

    访问该接口直接得到json数据

    总结

    本章主要介绍的是基于SpringMVC的一些实用功能

    相关文章

      网友评论

          本文标题:SpringMVC第二天

          本文链接:https://www.haomeiwen.com/subject/eezekxtx.html