学习spring mvc 三

    xiaoxiao2022-06-23  19

    今天开始第三天的spring mvc框架学习

    ----------------------3小时完成-----------------------------------

    要做的事:学习spring mvc 

    1、使用 HttpEntity<?>

    2、使用@ModelAttribute

    3、使用@InitBinder注解指定数据绑定

    -------------------------------------------------------------------------

    使用 HttpEntity<?>

    HttpEntity与@RequestBody和@ResponseBody类似。除了可以访问request和response body, HttpEntity也可以访问request和response headeers, 例如:

    @RequestMapping("/something")

    public ResponseEntity<String> handle(HttpEntity<byte[]>  requestEntity)   throws  UnsupportedEncodingException{

    String requestHeader = requestEntity.getHeaders().getFirst("MyRequestHeader");

    byte[] requestBody = requestEntity.getBody();

    HttpHeaders responseHeaders = new HttpHeaders();

    responseHeaders.set("MyResponseHeader", "Myvalue");

    return new ResponseEntity<String> ("Hello World", responseHeaders , HttpStatus.CREATED)

    }

    上面的例子中,从请求头MyRequestHeader中获取值,并将boedy读取到一个 byte array。

    将MyResponseHeader添加到response,将Hello World写入到response stream,并设置response status code为201.

    与@RequestBody和@ResponseBody一样,Spring 使用 HttpMessageConverter转换request和response streams。

    方法上使用@ModelAttribute

    @ModelAttribute注解可以被用在方法或者方法参数上。

    一个控制器可以有任意多个@ModelAttribute方法。所有这些方法都将在同一个控制器中的@RequestMapping方法之前被调用。

    @ModelAttribute方法可以定义@ControllerAdvice-annotated类并且这些方法适用于所有的控制器。@ControllerAdvice注解是一个 component annotation 允许实现类在 classpath扫描中被自动检测到。

    使用@RequestHeader注解映射请求头属性

    @RequestHeader注解允许一个方法参数与request header绑定。

    下面是使用请求头的例子:

    Host localhost:8080

    Accept text/html

    Accept-Language fr,en-gb

    Accept-Encoding gzip,deflate

    Accept-Charset utf-8

    Keep-Alive 300

    下面的例子展示了怎么得到头消息中的Accept-Encoding和 Keep-Alive的值:

    @RequestMapping("/displayHeaderInfo.do")

    public void displayHeaderInfo(@RequestHeader("AcceptEncoding") String encoding,

    @RequestHeader("Keep-Alive")  long  keepAlive)   {

    }

    如果方法参数不是String类型,将自动进行转换。

    --使用@InitBinder注解指定数据绑定

    使用@InitBinder注解的控制器方法允许你在控制类中直接配置web数据绑定。@InitBinder注解标识着方法初始化WebDataBinder的方法,将被用于填充使用注解的处理方法的command和form对象参数。

    这样的init-binder方法支持所有@RequestMapping支持的参数,除了command/form对象相应的验证结果对象。Init-binder方法,不能有返回值。因此他们通常定义为void。

    @Controller

    public class MyFormController{

    @InitBinder

    public void initBinder(WebDataBinder binder){

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    dateFormat.setLenient(false);

    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));

    }

    }

    配置一个自定义的WebBindingInitializer为了扩展数据绑定初始化,可以提供一个WebBindingInitializer接口的自定义实现。 

    配置bean如下:

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">

    <property  name="cacheSeconds"  value="0" />

    <property name="webBindingInitializer">

    <bean  class="org.springframework.samples.petclinic.web.ClinicBindingInitializer">

    </property>

    </bean>

    自定义带 @InitBinder方法扩展的数据绑定

    @InitBinder方法也可以在@ControllerAdvice注解的类中使用。

    转载请注明原文地址: https://ju.6miu.com/read-1123190.html

    最新回复(0)