初级学Spring MVC

根据视频学习的知识总结

1.RequestMapping

(1)注释类

(2)注释类中的方法

类注释,则类是相当于根目录 ; 若类不注释,则方法中的注释相对根目录

2.RequestMapping的映射参数

(1)value(请求URL)
(2)method(请求方法)
(3)params(请求参数)

params={
param1(表示必须包含此参数),
!param1(表示必须不能包含此参数)
param1 = str (表示必须包含这个参数,而且必须是str)
param1 != str (表示必须包含这个参数,但是不能是str)
}

(4)heads(请求头)
请求头类似于上面的,不做多写
package com.springmvc.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@RequestMapping(“/Hello”)
@Controller
public class HelloController {

    /*
     * (1)value(请求URL)
    *   (2)method(请求方法)
    *   (3)params(请求参数)
    *   (4)heads(请求头)
     */
    @RequestMapping(value="/home")  /*(1)*/
    public String index() {
        return "/home/index";
    }

    @RequestMapping(value="/home2",method=RequestMethod.POST)   /*(2)*/
    public String index2() {
        return "/home/index";
    }

    @RequestMapping(value="/home3",params={"username","age!=10"})   /*(2)*/
    public String index3() {
        return "/home/index";
    }
}

(5)Ant风格的正则表达式映射

  • “?” : 代表一个字符
  • “*” : 代表所有字符
  • “**”: 匹配多层路径

    package com.springmvc.controllers;

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;

    @RequestMapping(“/Hello”)
    @Controller
    public class HelloController {

    /**
     * ant风格的正则表达式映射
     */
    @RequestMapping("/home4/*/abc")
    public String index4(){
        return "/home/index";
    }
    

    }

(6)@PathVariable的使用,字段占位符
package com.springmvc.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@RequestMapping("/Hello")
@Controller
public class HelloController {

    /**
     * @PathVariable 中占位符的使用
     */
    @RequestMapping("/home5/{id}")
    public String index5(@PathVariable("id") Integer id) {
        System.out.println("@PathVariable"+id);
        return "/home/index";
    }

}

(7)REST风格的映射

曾删改查:
GET : order/1 //获取
DELETE: order/1 //删除
PUT : order/1 //修改
POST : order //增加

发送PUT或者DELETE的请求是,要多发送一个隐藏域name=”_method”,value=”DELETE/PUT”

  • 配置web.xml


    HiddenHttpMethodFilter
    org.springframework.web.filter.HiddenHttpMethodFilter


    HiddenHttpMethodFilter
    /*

  • 编写java类

    package com.springmvc.controllers;

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;

    @RequestMapping(“/Hello”)
    @Controller
    public class HelloController {

    /**
     * HiddenHttpMethodFilter 转换GET
     */
    @RequestMapping(value="/home6/{id}",method=RequestMethod.GET)
    public String index6(@PathVariable("id") Integer id) {
        System.out.println("filter Get:"+id);
        return "/home/index";
    }
    
    /**
     * HiddenHttpMethodFilter 转换POST
     */
    @RequestMapping(value="/home7",method=RequestMethod.POST)
    public String index7() {
        System.out.println("filter POST:");
        return "/home/index";
    }
    
    /**
     * HiddenHttpMethodFilter 转换DELETE
     */
    @RequestMapping(value="/home8/{id}",method=RequestMethod.PUT)
    public String index8(@PathVariable("id") Integer id) {
        System.out.println("filter PUT:"+id);
        return "/home/index";
    }
    
    /**
     * HiddenHttpMethodFilter 转换PUT
     */
    @RequestMapping(value="/home9/{id}",method=RequestMethod.DELETE)
    public String index9(@PathVariable("id") Integer id) {
        System.out.println("filter DELETE:"+id);
        return "/home/index";
    }
    

    }

(8)请求参数(@RequestParam),获取请求参数

package com.springmvc.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@RequestMapping("/Hello")
@Controller
public class HelloController {

    @RequestMapping("/home10")
    public String index10(@RequestParam("username") String un,@RequestParam(value="age",required=false,defaultValue="0") Integer age) {
        System.out.println("@RequestParam:"+un+","+age);
        return "/home/index";
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值