@requestparam value = date spring boot

时间:2017-04-09 10:20:54

标签: spring spring-boot

@Controller
@RequestMapping(value="/reservations")
public class ReservationController {
    private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");

    @Autowired
    private ReservationService reservationService;

    @RequestMapping(method = RequestMethod.GET)
    public String getReservation(@RequestParam(value="date", required=false) String dateString, Model model){
        Date date = null;
        if(dateString != null){
            try {
                date = DATE_FORMAT.parse(dateString);
            } catch (ParseException pe) {
                date = new Date();
            }
        }else{
            date = new Date();
        }
        List<RoomReservation> roomReservationList = this.reservationService.getRoomReservationsForDate(date);
        model.addAttribute("roomReservations", roomReservationList);
        return "reservations";
    }
}

我知道@RequestParam注释用于将查询字符串的参数值绑定到控制器方法参数。例如,http://localhost:8080/reservations?date=2017-01-01。但是,值=&#34; date&#34;来自?我没有看到任何价值&#34; date&#34;在我的HTML页面内。

1 个答案:

答案 0 :(得分:0)

如果您提交表单作为方法:“GET”(不是POST)并且表单包含一个名为date的输入字段,那么提交此表单将会触及此处理程序方法。