@RequestMapping注解的简单学习 和 @requestParam @PathVariable

来源:互联网 发布:杭创软件 编辑:程序博客网 时间:2024/06/10 17:06
从第一次接触到@RequestMapping注解,我们就知道是帮助spring匹配url需要调用的方法,来执行方法的。与struts2的方法动态调用不同,但是有可以自由设置。

@RequestMapping 中的属性还可以设置value 和request的类型,可以设置为post 和get 
[如下]
@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";    }}

URL模板模式

那么现在我们来说说@RequestMapping和@PathVariable组合来拿到 url传来的值。
@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"; }

 如上: 我们可以用http://localhost:8080/springMVC/example/owners/aa

则  ownerId 的值为 aa 

那我们怎么摸索出来传多个值呢??
@RequestMapping(value ="/user/{id}&{name}", method=RequestMethod .GET)
     public String findOwner( @PathVariable String id,@PathVariable String name,Model model){
         model.addAttribute ("message", id);
         System.out .print( name);
         return "helloWorld" ;
        
     }
http://localhost:8080/springMVC_2/example/user/aa&vc
则id的值是aa   name 为vc


URL模板模式和正则表达式(这个真的是很巧妙啊,对url变量做个验证)
@RequestMapping("/spring-web/{symbolicName:[a-z-]+}-{version:\d\.\d\.\d}.{extension:\.[a-z]}")  public void handle(@PathVariable String version, @PathVariable String extension) {        // ...  }}


我们再来讲讲@RequestParam(我们常见的传值方法如:localhost:8080/user?id=11)
@Controller@RequestMapping("/pets")@SessionAttributes("pet")public class EditPetForm {    // ...    @RequestMapping(method = RequestMethod.GET)    public String setupForm(@RequestParam("petId") int petId, ModelMap model) {        Pet pet = this.clinic.loadPet(petId);        model.addAttribute("pet", pet);        return "petForm";    }    // ...


而且我们还可以弄默认值哦~~
@RequestParam(value="id", required=false)
@Controller@RequestMapping("/pets")@SessionAttributes("pet")public class EditPetForm {    // ...    @RequestMapping(method = RequestMethod.GET)    public String setupForm(@RequestParam(value="petId",defaultValue="12",required=false) int petId, ModelMap model) {        Pet pet = this.clinic.loadPet(petId);        model.addAttribute("pet", pet);        return "petForm";    }    // ...






















原创粉丝点击