美文网首页
springMVC(1)

springMVC(1)

作者: 七宝qb | 来源:发表于2017-04-17 18:38 被阅读6次
    • web.xml
    <filter>
            <filter-name>hiddenHttpMethodFilter</filter-name>
            <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>hiddenHttpMethodFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
    • jsp
    <a href="hello/hiddenHttpMethodFilterGet/123">hello/hiddenHttpMethodFilterGet</a>
    <form action="hello/hiddenHttpMethodFilterPost/233" method="post">
        <input type="submit" value="hiddenHttpMethodFilterPost">
    </form>
    <form action="hello/hiddenHttpMethodFilterPut/233" method="post">
        <input type="hidden" name="_method" value="PUT">
        <input type="submit" value="hiddenHttpMethodFilterPut">
    </form>
    <form action="hello/hiddenHttpMethodFilterDelete/233" method="post">
        <input type="hidden" name="_method" value="Delete">
        <input type="submit" value="hiddenHttpMethodFilterDelete">
    </form>
    <br>
    <hr>
    <br>
    <form action="hello/testPojo" method="post">
        name <input name="name" value="zhangsan"> <br>
        age <input name="age" value="13"> <br>
        address <input name="address.provence" value="shandong">
        <input type="submit">
    </form>
    <br>
    <br>
    <br>
    <hr>
    <form action="hello/testModelAttribute" method="post">
        name <input name="name" value="zhangsan"> <br>
        age <input name="age" value="13"> <br>
        address <input name="address.provence" value="shandong">
        <input type="submit" value="testModelAttribute">
    </form>
    
    • controller.java
    /**
         * PathVariable 注解 映射URl绑定的占位符
         */
        @RequestMapping("/testPathVariable/{id}")
        public String testPathVariable(@PathVariable("id") Integer id) {
            System.out.println(id);
            return SUCCESS;
        }
    
        /**
         * rest 风格
         * -/order/1 http GET : 得到id = 1 的order
         * -/order http post : 提交一个order
         * -/order/1 http PUT : 更新id = 1 的order
         * -/order/1 http DELETE : 删除id = 1 的order
         *
         * 如何发送PUT 和DELETE请求?
         * 1.配置HiddenHttpMethodFilter
         * 2.发送POST请求
         * 3.在发送POST请求时携带name = “_method” 的隐藏域,值为DELETE或PUT
         *
         * RequestParams
         * value 请求参数的name   defaultValue require(默认true)
         */
    
    
        /**
         * 几种方法  get post delete put
         * requestHeader 参数注解 请求头的相关参数,同requestParam 可获得请求头的参数
         * cookieValue 注解参数 同 requestParam 可获得cookie的值 属性同requestParam
         * (cookie)相关操作。
         */
        @RequestMapping(value = "/hiddenHttpMethodFilterGet/{id}", method = RequestMethod.GET)
        public String hiddenHttpMethodFilterGet(@PathVariable("id") Integer id,
                                                @RequestParam(value = "name", required = false) String name) {
            System.out.println("hiddenHttpMethodFilterGet : id = " + id + ", name = " + name);
            return SUCCESS;
        }
    
        @RequestMapping(value = "/hiddenHttpMethodFilterPost/{id}", method = RequestMethod.POST)
        public String hiddenHttpMethodFilterPost(@PathVariable("id") Integer id) {
            System.out.println("hiddenHttpMethodFilterPost : id = " + id);
            return SUCCESS;
        }
    
        @RequestMapping(value = "/hiddenHttpMethodFilterDelete/{id}", method = RequestMethod.DELETE)
        public String hiddenHttpMethodFilterDelete(@PathVariable("id") Integer id) {
            System.out.println("hiddenHttpMethodFilterDelete : id = " + id);
            return SUCCESS;
        }
    
        @RequestMapping(value = "/hiddenHttpMethodFilterPut/{id}", method = RequestMethod.PUT)
        public String hiddenHttpMethodFilterput(@PathVariable("id") Integer id) {
            System.out.println("hiddenHttpMethodFilterPut : id = " + id);
            return SUCCESS;
        }
    
        /**
         * 请求参数名和java类属性名进行自动匹配,自动为对象填充属性,支持级联属性。
         */
        @RequestMapping(value = "/testPojo")
        public String testPojo(User user) {
            System.out.println("user:" + user);
            return SUCCESS;
        }
    
        /**
         * 使用原生 servlet
         */
        @RequestMapping(value = "/testServlet")
        public void testServlet(HttpServletRequest request, HttpServletResponse response, Writer writter) throws IOException {
            System.out.println("request: " + request + " ,response :" + response);
            response.setContentType("text/html;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            writter.write("你好,这是writer");
        }
    
        /**
         * 处理模型数据
         * springmvc 提供以下几种途径输出模型数据
         * <p/>
         * -modelAndView
         * -Map 及 Model
         * -ModelAttribute
         * -SessionAttribute
         */
        /**
         * modelAndview
         */
        @RequestMapping("/modelAndview")
        public ModelAndView modelAndview() {
            ModelAndView modelAndView = new ModelAndView(SUCCESS);
            modelAndView.addObject("date", new Date());
            modelAndView.addObject("list", Arrays.asList("jerry","and","tom"));
            System.out.println("modelAndview");
            return modelAndView;
        }
    
        /**
         * map
         */
        @RequestMapping("/mapModel")
        public String mapModel(Map<String,Object> map){
            map.put("date",new Date());
            map.put("list",Arrays.asList("jerry","tom"));
            return SUCCESS;
        }
        /**
         * @sessionAttribute 除了可以通过属性名指定需要放到会话中的属性外(value属性)
         * 还可以通过模型属性的的对象类型指定哪些模型属性可以放到会话中(types属性)
         *
         * 注意:该注解只能放到类名上面,不能修饰方法
         */
        @RequestMapping("testSessionAtttribute")
        public String testSessionAtttribute(Map<String,Object> map){
    //        map.put("sessionName","七宝");
            map.put("sessionName",Arrays.asList("张三","李四"));
            return SUCCESS;
        }
    
        /**
         * 由 @ModelAttribute 标记的方法,会在每个目标方法执行之前被mvc调用。
         * 运行流程 :
         * 1.从数据库取出对象,放入map中,键为user
         * 2.springMVC 从map中取出user对象,并把表单请求参数赋给User对象的对应属性。
         * 3.springMVC把上述对象传入目标的方法的参数
         *
         * 注意: 在 modelAttribute 注解修饰的方法中,放入到map中的键需要和目标方法入参类型的第一个字母小写的字符串一致。
         */
        @ModelAttribute
        public void getUser(Map<String,Object> map){
            User user = new User();
            user.setName("first");
            user.setAddress(new Address());
            user.getAddress().setCity("sandong!");
            System.out.println("数据库的user:"+user);
            map.put("user",user);
        }
        @RequestMapping(value = "testModelAttribute",method = RequestMethod.POST)
        public String testModelAttribute(User user){
            System.out.println("最终的user:"+user);
            return SUCCESS;
        }
    

    相关文章

      网友评论

          本文标题:springMVC(1)

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