zoukankan      html  css  js  c++  java
  • Spring MVC之Action输入参数


    第一部分:Action输入参数
    Spring MVC 通过@RequestMapping注解映射请求,最终的真正执行代码为处理器方法,即@RequestMapping注解的方法。Spring MVC方法可以接受的参数包含:
      1.1)基本数据类型,如int,String,double...
      1.2)自定义数据类型,如自定义的pojo对象
      1.3)Servlet API中的Request和Response。如:ServletRequest或HttpServletRequest。
      1.4)Session对象。如:HttpSession。注意此对象不是线程安全的,可设置RequestMappingHandlerAdapter的 “synchronizeOnSession”属性为true来同步。
      1.5)org.springframework.web.context.request.WebRequest或org.springframework.web.context.request.NativeWebRequest。
      1.6)java.util.Locale对象。需要在Servlet环境中配置 LocaleResolver。
      1.7)java.io.InputStream / java.io.Reader用于访问请求输入流。
      1.8)java.io.OutputStream / java.io.Writer用于生成的输出流。
      1.9)java.security.Principal用于保存当前的认证用户。
      1.10)@PathVariable注解的参数。
      1.11)@MatrixVariable注解的参数。
      1.12)@RequestParam注解的请求参数。
      1.13)@RequestHeader注解的请求头参数。
      1.14)@RequestBody注解的请求体参数。
      1.15)@RequestPart注解的上传文件请求体参数。请求类型为”multipart/form-data”。
      1.16)HttpEntity<?>:请求实体对象,可以访问头信息和内容体。
      1.17)java.util.Map / org.springframework.ui.Model / org.springframework.ui.ModelMap:用于响应模型类,存储响应数据。
      1.18)org.springframework.web.servlet.mvc.support.RedirectAttributes:用于在redirect的重定向中传递数据。
      1.19)命令或 表单对象。
      1.20)org.springframework.validation.Errors / org.springframework.validation.BindingResult对象。
      1.21)org.springframework.web.bind.support.SessionStatus可以用来控制Session的清理。
      1.22)org.springframework.web.util.UriComponentsBuilder可以用来查询当前请求的主机名、端口号、Schema、Context Path、Servlet Mapping信息。

    1、基本数据类型
    方法的参数可以是任意基本数据类型,如果方法参数名与http中请求的参数名称相同时会进行自动映射。
      public String index(Model model, int id, String name) {

      }

    2、自定义数据类型
    Spring MVC会通过反射把请求中的参数设置到自定义类型的对象中。
      public String index(Model model, Member member) {

      }

    3、集合类型
    Spring MVC中不能直接在方法的参数中使用集合类型(List 和 Map),必须使用集合类型的包装类。
      //人员集合
      public class MemberList {
        private List<Member> items;

        public List<Member> getItems() {
          return items;
        }

        public void setItems(List<Member> items) {
          this.items = items;
        }
      }
    然后使用
      public String index(Model model, MemberList members) {

      }


    4、使用@RequestParam注解的请求参数。
        @RequestMapping(value="/index.do")
        public String index(@RequestParam(value="code", required=true) String code,@RequestParam(value="pwd", required=true) String pwd, ModelMap model) {
          Member member = authService.validate(code,pwd);
          model.addAttribute("member", member);
          return "index";
        }
    使用 @RequestParam 可以处理一些细节逻辑,如默认值、是否必须、别名等。

    5、使用@RequestBody注解的请求体参数。
      @RequestMapping(value="/index.do", method = RequestMethod.PUT)
      public void index(@RequestBody String body, Writer writer) throws IOException {
        writer.write(body);
      }

      由HttpMessageConverter来转换body至对象。它可以被转换成支持的任意类型。 RequestMappingHandlerAdapter 默认的支持有:
        5.1)ByteArrayHttpMessageConverter :byte[]转换。
        5.2)StringHttpMessageConverter :转换成String。
        5.3)FormHttpMessageConverter :转换成MultiValueMap<String, String>。
        5.4)SourceHttpMessageConverter :转换成javax.xml.transform.Source。
      转换的参数还可以加上@Valid注解,以进行转换校验,如果校验失败将抛出MethodArgumentNotValidException,此异常默认情况由 DefaultHandlerExceptionResolver进行管理,将会向客户端发送400错误码。

    6、使用HttpEntity<?>
      @RequestMapping(value="/index.do")
      public ResponseEntity<String> index(HttpEntity<byte[]> requestEntity) throws Exception {
        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);
      }

    7、使用@ModelAttribute

      @ModelAttribute使用在方法上,就相当于一个公共model池一样(前提是在同一个Controller中),在同一个Controller中,所有@RequestMapping注解的方法执行前,都要先执行这些由@ModelAtrribute注解的方法,将数据存入到当前请求的Model中。它可以单个添加属性,也可以批量地添加。
      @ModelAttribute
      public void bindModel(@RequestParam String code,@RequestParam String pwd, Model model){
        model.addAttribute(authService.validate(code,pwd));
      }

      @ModelAttribute使用在方法参数上,被注解的参数会先从请求的Model中查找,如果未找到,则会实例化一个新对象,放入请求的Model中。
      查找路径:
        存在于@SessionAttributes中。
        存在于同一个Controller中的注解于方法级别上的@ModelAttribute中。
        Url的模式匹配和转换中。
      @RequestMapping(value="/member/{member}", method = RequestMethod.PUT)
      public void bind(@ModelAttribute("member") Member member){

      }

    8、使用@SessionAttributes
    在Controller类上使用此注解,会从请求的model中查找@SessionAttributes指定的属性名或属性类型,将它们保存到Session中。
      /**
      * @功能 权限控制器
      * @作者 Davee.Yuan
      * @日期 2017-01-16
      * @说明 http://localhost:8080/auth/index
      */
      @Controller
      @RequestMapping("/sso")
      @SessionAttributes("member")
      public class SsoController{

      }

    9、使用@RedirectAttributes
      方法的签名中指定一个RedirectAttributes 类型的参数,用于在请求返回 RedirectView时传递数据。
      @RequestMapping(value="/redirect.do")
      public String redirect(RedirectAttributes attr){
        attr.addAttribute("status",9999);
        return "redirect:/toLogin";
      }

    10、使用@CookieValue
      @RequestMapping(value="/display.do")
      public String display(@CookieValue("JSESSIONID") String cookie){
        //---------------------
        return "";
      }

    第二部分:类型转换
      Spring MVC控制器接收到的原始请求数据都是文本类型,而先前使用的@RequestParam、@CookieValue等注解的参数可以为String类型,也可以是其它,Spring默认支持一些简单的数据类型如int、long、Date等类型的自动转换,如果需要对自定义的类型或复杂类型进行转换的话,需要自定义转换器绑定。绑定的方式有两种:局部绑定、全局绑定(它还有两种实现形式):

      局部绑定:在@Controller中使用@InitBinder注解方法进行转换器的绑定,用它绑定的方法返回类型应设置成void,方法参数WebDataBinder集合了WebRequest和java.util.Locale,它可以将转换器绑定至Context中:
      @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 接口类,将它绑定至全局配置中,如实现类ClinicBindingInitializer:
      <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注解于@ControllerAdvice注解的类中,来实现全局绑定

    内容主要来自于:https://blog.zenfery.cc/archives/131.html?replytocom=14

  • 相关阅读:
    原型模式
    简单工厂模式与工厂方法模式
    监听器 Listener
    代理模式
    装饰模式
    软件设计的原则
    事务的特性和隔离级别
    JDBC事务(三)ThreadLocal绑定Connection
    JDBC事务(二)转账示例
    JDBC事务(一)
  • 原文地址:https://www.cnblogs.com/yuanyee/p/6289617.html
Copyright © 2011-2022 走看看