Path Variable不映射Spring MVC

时间:2014-08-14 07:54:12

标签: java spring-mvc path-variables

所以我有以下问题。我有一个控制器,我映射到" / reservationQuery / *"。我继续使用#34; delete_1"方法,它的工作原理,但每当我尝试继续" delete_2"方法它说没有该URI的映射。但为什么会这样呢?是不是所有的URI都使用/ reservationQuery /并由我的ReservationQueryController映射?

如果我想使用第二个URI进行删除,我是否必须制作另一个控制器并将其映射到/ reservationQuery / delete / *?

@Controller
@RequestMapping("/reservationQuery/*")
public class ReservationQueryController{
private ReservationService reservationService;

@Autowired
public ReservationQueryController(ReservationService reservationService) {
    this.reservationService = reservationService;
}


@RequestMapping(value="/reservationQuery/{reservationID}", method=RequestMethod.GET)
public String delete_1(@PathVariable("reservationID") int reservationID, Model model){
    reservationService.deleteReservation(reservationID);
    return "reservationQuery";
}


@RequestMapping(value="/reservationQuery/delete/{reservationID}",     method=RequestMethod.GET)
public String delete_2(@PathVariable("reservationID") int reservationID, Model model){
    reservationService.deleteReservation(reservationID);
    return "reservationQuery";
}




}

2 个答案:

答案 0 :(得分:2)

您应该使用"/reservationQuery"映射您的控制器,并且您的方法delete_1应与"/{reservationID}"映射,方法delete_2应与"/delete/{reservationID}"映射。无需在方法中重复控制器映射。

答案 1 :(得分:2)

您可以在方法级别删除“reservationQuery”的额外值,因为您已经在Controller中映射了“reservationQuery”,这是类级别。

所以正在发生的事情是,在删除时,您的URL将以/ reservationQuery / reservationQuery /开始。

我认为这是错误的原因,它应该是HTTP错误404