SpringMVC URL传参方式

  1. ModalAndView

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    @Controller
    public class HelloWorldController {
    @RequestMapping("/helloWorld")
    public ModelAndView helloWorld() {
    ModelAndView mav = new ModelAndView();
    mav.setViewName("helloWorld");//viewName 视图名称
    mav.addObject("message", "Hello World!");// 往视图传递对象
    return mav;
    }
    }
  2. 在Class级别这是@RequestMapping

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    @Controller
    @RequestMapping("/appointments")
    public class AppointmentsController {
    private final AppointmentBook appointmentBook;
    @Autowired
    public AppointmentsController(AppointmentBook appointmentBook) {
    this.appointmentBook = appointmentBook;
    }
    @RequestMapping(method = RequestMethod.GET)
    public Map<String, Appointment> get() {
    return appointmentBook.getAppointmentsForToday();
    }
    @RequestMapping(value="/{day}", method = RequestMethod.GET)
    public Map<String, Appointment> getForDay(@PathVariable @DateTimeFormat(iso=ISO.DATE) Date day, Model model) {
    return appointmentBook.getAppointmentsForDay(day);
    }
    @RequestMapping(value="/new", method = RequestMethod.GET)
    public AppointmentForm getNewForm() {
    return new AppointmentForm();
    }
    @RequestMapping(method = RequestMethod.POST)
    public String add(@Valid AppointmentForm appointment, BindingResult result) {
    if (result.hasErrors()) {
    return "appointments/new";
    }
    appointmentBook.addAppointment(appointment);
    return "redirect:/appointments";
    }
    }
  3. @PathVirable

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    @RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)
    public String findOwner(@PathVariable String ownerId, Model model) {
    Owner owner = ownerService.findOwner(ownerId);
    model.addAttribute("owner", owner);
    return "displayOwner";
    }
    @RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)
    public String findOwner(@PathVariable("ownerId") String ownerId, Model model) {
    // implementation omitted
    }

待完善…

坚持原创技术分享,您的支持将鼓励我继续创作!