发表于: 2019-10-26 23:59:54

1 1025


今天完成的事情:

将报名帖SSM转换为Rest风格。

@Controller
public class CardController {
@Autowired
   CardService cardService;
   @GetMapping("/cards")
public ModelAndView cardList() {
//取到查询
       List<User> cardList = cardService.getAll();
       ModelAndView mav = new ModelAndView();
       //将数据放置到ModelAndViewview,第二个参数可以是任何java
       mav.addObject("cardList", cardList);
       mav.setViewName("cardList");
       return mav;
   }
@GetMapping("/cards/card")
public ModelAndView addGo(ModelAndView mav) {
mav.setViewName("add");
       return mav;
   }
@PostMapping("/cards")
public ModelAndView add(User user) {
cardService.add(user);
       ModelAndView mav = new ModelAndView("redirect:/cards");
       return mav;
   }
@DeleteMapping("/cards/{id}")
public ModelAndView del(@PathVariable Integer id) {
cardService.del(id);
       ModelAndView mav = new ModelAndView("redirect:/cards");
       return mav;
   }
@GetMapping("/cards/{id}")
public ModelAndView updateGo(@PathVariable Integer id) {
User user = cardService.selectById(id);
       ModelAndView mav = new ModelAndView("update");
       mav.addObject("user", user);
       return mav;
   }
@PutMapping("/cards/{id}")
public ModelAndView update(User user) {
cardService.update(user);
       ModelAndView mav = new ModelAndView("redirect:/cards");
       return mav;
   }
}



明天计划的事情:  

继续任务二。                        
遇到的问题: 

改为Rest风格时,@RequestMapping中的值改变,相应的jsp中的请求地址也要跟着改变。

发送数据和接收数据时的方法要一致,Controller方法中为POST,但我开始时没注意,jsp文件中为GET,导致出错。                      
收获:
                    


返回列表 返回列表
评论

    分享到