springmvc04RestFul风格

RestFul就是一个资源定位及资源操作的风格,基于这种风格设计的软件可以更简洁,更有层次,更易于实现缓存机制。

@Controller
public class RestFulController {
	//第一种在RequestMapping中加入method参数
    @RequestMapping(value = "/add/{a}/{b}",method = RequestMethod.POST)
    public String test(@PathVariable int a, @PathVariable int b, Model model){
        int result = a+b;
        model.addAttribute("msg","结果为:"+result);
        return "test";
    }
    //第二种直接使用XXXMaping注解
    @GetMapping("/add/{a}/{b}")
    public String test2(@PathVariable int a, @PathVariable int b, Model model){
        int result = a+b;
        model.addAttribute("msg","结果为2 :"+result);
        return "test";
    }
}

默认走的是get方法,post方法的话可以增加一个a.jsp实现

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="/add/1/2" method="post">
    <input type="submit">
</form>
</body>
</html>

虽然后续路径都是localhost:8080/add/1/2,但是提交方式确实不同的。

无视图解析器时的转发和重定向

@Controller
public class ModelTest01 {
    @RequestMapping("/m1/t1")
    public String test(Model model){
        //没有视图解析器的情况下
        //转发
        model.addAttribute("msg","modelTest01");
//        return "forward:/WEB-INF/jsp/test.jsp";
        //重定向
        return "redirect:/index.jsp";
    }
}

有视图解析器默认是走的转发,url不发生变化

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值