zoukankan      html  css  js  c++  java
  • SpringMvc的传递参数方式

    1. @requestMapping: 类级别和方法级别的注解, 指明前后台解析的路径。 有value属性(一个参数时默认)指定url路径解析,method属性指定提交方式(默认为get提交) 

      @RequestMapping(value = "/testing")
      public class QuestionSetDisplayController extends BaseController {}
      @RequestMapping(value = "/applicant/recover")
      public BaseModel recover(String cellphone) throws OTPException {
          return userService.recover(cellphone);
      }

    2. @RequestParam: 请求参数规则注解。 value属性匹配前台传递的参数(一个参数时默认),required属性此字段是否必须传值(boolean,默认为true),defaultValue此参数的默认值(存在此参数时,说明前台不必需传递参数,required为false) 

      @RequestMapping("/login")  //url: /login?name=tom

       public String login(@RequestParam(value="age",required=false,defaultValue="24") String agenum,@RequestParam("name") String name){
           return "hello";
       }

    3.  @PathVariable: url参数注解, 一般用于从url中获取参数

    @RequestMapping(value = "/system/getAllCodeTableData/{category}", method = RequestMethod.GET) 
    //前台url: '/system/getAllCodeTableData/APPLICANT_ENGLISH'
     public List<CodeTableModel> getCodeTableModelByCategory(@PathVariable String category) throws OTPException {<br>     return codeTableService.getCodeTableModelByCategory(category); <br>}

    4. 特殊的 属性编辑器 在前台到后台data日期类型等的转化会出错,此时我们需要属性编辑器进行属性的转化 //日期传递参数会产生异常,因此在传递时间参数时,需要进行类型转换,在初始化时进行数据的绑定与转化

     @RequestMapping(value="/todate/{data}",method=RequestMethod.GET)

        public String todate(@PathVariable("data") Date date){
            System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(date));
            return "start";
        }
        @InitBinder    //初始化参数绑定, 日期类型的转化,
        public void initBinder(ServletRequestDataBinder binder){
            binder.registerCustomEditor(java.util.Date.classnew CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
        }

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

    关于前台angular使用get/post传递参数, 后台接收

    get方式传参

    $scope.getFlows = function(key) {

                $scope.flows = Flow.search({type:key}) // 参数以{key: value, ...}传递
            }
     
    @GetMapping("")
        @ResponseBody
        public List<DefinitionDTO> query(
                @RequestParam(value="type") String type) throws Exception {
            // 后台以requestparam来获取参数
            if (!type.equals("all")) {
                return activitiService.findByKey(type);
            }
            return activitiService.findAll();
        }

    post传参

    $scope.batchDelete = function() {

                $scope.selectedVehicles = getSelected();
                if (angular.isDefined($scope.selectedVehicles) && $scope.selectedVehicles.length > 0) {
                    var vehicle = {ids: $scope.selectedVehicles}; //前台封装成对象
                    VehicleInfo.batchDelete(vehicle, function(response) {
                        toPage(1, $scope.pageInfos.size, null);
                    });
                }
            }
     
     
    @PostMapping("/batchDelete")
        @ResponseBody  //后台以requestbody注解来获取请求对象参数
        public void batchDelete(@RequestBody VehicleDTO vehicle) throws Exception {
            for (Long id : vehicle.getIds()) {
                vehicleService.deleteVehicle(id, Boolean.TRUE);
            }
        }
  • 相关阅读:
    使用doctest单元测试方式培训讲解:Python字符串格式化(适合测试工程师的学习方法)
    树莓派Raspberry中成功安装RobotFramework+Selenium
    树莓派Raspberry实践笔记-安装使用minicom
    树莓派Raspberry实践笔记-解决官方文档打开慢问题
    window电脑蓝屏
    fastjson基本用法
    自定义注解
    curl工具
    观察者模式
    工厂模式
  • 原文地址:https://www.cnblogs.com/523823-wu/p/10070377.html
Copyright © 2011-2022 走看看