Spring MVC 学习 之 - URL参数传递

Spring MVC 学习 之 - URL参数传递

 

 

 @Controller:

         在类上注解,则此类将编程一个控制器,在项目启动 Spring 将自动扫描此类,并进行对应URL路由映射。

首先基本的写法:

 @RestController:作用也是进行对应URL路由映射

两者区别:就是RestController 可返回数据,Controller只能返回页面,

简单来说:如果你要 return 返回一个对象或集合 就用RestController  就可以了

 

@RequestMapping

         指定URL映射路径,如果在控制器上配置 RequestMapping  ,具体请求方法也配置路径则映射的路径为两者路径的叠加 常用映射如:RequestMapping("/aaa")

        配置映射路径:

 

@Controller 注解   这个注解只能跳转页面

@Controller
@RequestMapping("/emp")        //如果加在类上,用get的方式访问,会自动匹配get
public class Manage3Controller {

    @RequestMapping(method = RequestMethod.GET)    //get的请求
    public String selectAll(Model model) {
        List<Staff> staffList = managerServiceFromMock.selectAll();
        System.out.println(staffList);
        model.addAttribute("stafflist", staffList);
        return "home";                     //要返回的页面
    }

 

 //也可以这样写
  @RequestMapping(value ="/emp",method = RequestMethod.GET)
  public String tt(){
   return "home";
  }

 

//这样也可以
@GetMapping("/emp")
public String input(Book book) {
    return "book_input";
}

get方法:接收url带参数

 @RequestMapping("/del/{id}")
    public String del(@PathVariable("id") int id) {
        staffMapper.deleteByPrimaryKey(id);
        return "redirect:/emp1";
    }

这样也可以:

   @RequestMapping("/del")
    public String del(int id) {
        staffMapper.deleteByPrimaryKey(id);
        return "redirect:/emp1";
    }

 

 

post方法 第一种:

  @RequestMapping(method = RequestMethod.POST)
    public String insert() {return "redirect:/emp1";
    }

 第二种:

 @PostMapping("/book")
    public String submit(@Valid Book book, BindingResult result) {
        if (result.hasErrors())
            return "book_input";

        System.out.println("正常业务。。。。");
        return "book_home";
    }

 

 
 
 
 
 
 
 
posted @ 2018-12-31 16:08 梓鸿 阅读(...) 评论(...) 编辑 收藏
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值