springMVC(1)

  • web.xml

        hiddenHttpMethodFilter
        org.springframework.web.filter.HiddenHttpMethodFilter
    
    
        hiddenHttpMethodFilter
        /*
    
  • jsp
hello/hiddenHttpMethodFilterGet



name
age
address




name
age
address
  • 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 提供以下几种途径输出模型数据
     * 

* -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 map){ map.put("date",new Date()); map.put("list",Arrays.asList("jerry","tom")); return SUCCESS; } /** * @sessionAttribute 除了可以通过属性名指定需要放到会话中的属性外(value属性) * 还可以通过模型属性的的对象类型指定哪些模型属性可以放到会话中(types属性) * * 注意:该注解只能放到类名上面,不能修饰方法 */ @RequestMapping("testSessionAtttribute") public String testSessionAtttribute(Map 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 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))