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);
        }
    }
posted @ 2018-12-05 13:15  wumengjuan  阅读(639)  评论(0)    收藏  举报