Spring 的MVC @RequestMapping

 @RequestMapping

@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 " ;
    }
}

DispatcherServle
< servlet >
        
< servlet - name > dispatcher </ servlet - name >
        
< servlet - class > org.springframework.web.servlet.DispatcherServlet </ servlet - class >
        
< load - on - startup > 1 </ load - on - startup >
    
</ servlet >
    
< servlet - mapping >
        
< servlet - name > dispatcher </ servlet - name >
        
< url - pattern >* . do </ url - pattern >
    
</ servlet - mapping >
 

/appointments /new.do
映射方法是 getNewForm()
/appointments.do 
GET请求映射方法是  get()
POST请求映射方法是 add(@Valid AppointmentForm appointment, BindingResult result)



通过参数条件缩小路径映射
@RequestMapping(value = "/path", params="myParam=myValue")
public void add(){...}
http://localhost:8080/DynamicWebProject/hao/hello.do?myParam=myValue
URL中只有存在myParam=myValue参数时才被允许访问路径映射的方法

@RequestMapping(value = "/path", method = RequestMethod.POST, headers="content-type=text/*")
public void add(){...}
只有在URL的头信息中包含有content-type=text/*(content-type=text/xml)的POST请求才能访问add()方法



配合@RequestMapping使用的方法或注解
org.springframework.web.context.request.WebRequest或org.springframework.web.context.request.NativeWebRequest。通用请求参数允许访问 request/session属性,到本机的Servlet / Portlet API的。

java.util.Locale 当前请求的区域设置,由解析器提供最具体的语言环境。

java.io.InputStream /java.io.Reader访问请求的内容。这个值是原始的InputStream /Reader ,由Servlet API公开。

java.io.OutputStream /java.io.Writer生成响应的内容。这个值是原始的OutputStream /Writer,由Servlet API公开。

@ PathVariable注明获取URI模板变量的参数。

@ RequestParam注明为获取特定的servlet请求参数的参数。

@ RequestHeader 访问特定的servlet请求的HTTP头的参数。参数值转换为声明的方法的参数类型。

@ RequestBody 访问HTTP请求体参数。参数值转换为所声明的方法的参数类型使用HttpMessageConverters。

HttpEntity <?>访问servlet请求的HTTP头和内容。请求流将被转换为实体采用HttpMessageConverters。

java.util.Map/ org.springframework.ui.Model /org.springframework.ui.ModelMap为丰富的隐含模型,暴露在Web视图

org.springframework.validation.Errors / org.springframework.validation.BindingResult

org.springframework.web.bind.support.SessionStatus

猜你喜欢

转载自blog.csdn.net/ssyan/article/details/7063736
今日推荐