springmvc--requestmapping--restful

 

 

1 RequestMapping

@RequestMapping

注解用于映射控制器类或者一个特定的处理程序的方法。可以用于类或方法上。用于类上表示类中所有的方法都以该地址为父路径。

package com.wang.controller;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/test")
public class EnCoderController {

    @RequestMapping("/come/")
    public  String debugEncoding(Model model){
        model.addAttribute("msg","我水水");
        return "jiayou";
    }
//    url:http://localhost:8080/test/come1/
    @RequestMapping("/come1")
    public  String come1(Model model){
        model.addAttribute("msg","come1  加油");
        return  "jiayou";
    }

}

 

2 restful风格

 

资源定位及资源操作的风格。

@PathVariable 标识了url传递的变量,可以实现restful风格
package com.wang.controller;


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

import javax.jws.WebParam;

@Controller
public class RequestMappingController {

//    http://localhost:8080/add?a=10&b=100
    @RequestMapping("/add")
    public String testAdd(int a, int b, Model model){
        model.addAttribute("msg" ,"a+b="+(a+b));
        return "jiayou";
    }

//    这就是restful风格请求url  @PathVariable指定了url变量a,b,可以从前端传过来
//    http://localhost:8080/add/5/2
    @RequestMapping("/add/{a}/{b}")
    public String testRestful(@PathVariable int a ,@PathVariable int b ,Model model){

        model.addAttribute("msg","a+b="+(a+b));
        return "jiayou";
    }


    //    这就是restful风格请求url
//    可以指定处理方式,method=?
//    由于前台请求方式是GET,此处会出错405  http://localhost:8080/add1/5/4
    @RequestMapping(value= "/add1/{a}/{b}",method = RequestMethod.POST)
    public String testRestfulMethod(@PathVariable int a ,@PathVariable int b ,Model model){
        model.addAttribute("msg","post:a+b="+(a+b));
        return "jiayou";
    }
//    当method改成GET,可以执行成功 http://localhost:8080/add2/5/4
    @RequestMapping(value= "/add2/{a}/{b}",method = RequestMethod.GET)
    public String testRestfulMethod1(@PathVariable int a ,@PathVariable int b ,Model model){
        model.addAttribute("msg","get:a+b="+(a+b));
        return "jiayou";
    }


//    可以通过GetMapping ,标识get方式  http://localhost:8080/add3/5/4
    @GetMapping(value= "/add3/{a}/{b}")
    public String testRestfulMethod2(@PathVariable int a ,@PathVariable int b ,Model model){
        model.addAttribute("msg","getmapping:a+b="+(a+b));
        return "jiayou";
    }





}

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值