SpringMvc的傳遞參數方式

1. @requestMapping: 類級別和方法級別的註解, 指明先後臺解析的路徑。 有value屬性(一個參數時默認)指定url路徑解析,method屬性指定提交方式(默認爲get提交) java

  @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) app

  @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中獲取參數post

@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日期類型等的轉化會出錯,此時咱們須要屬性編輯器進行屬性的轉化 //日期傳遞參數會產生異常,所以在傳遞時間參數時,須要進行類型轉換,在初始化時進行數據的綁定與轉化ui

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

     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. class new  CustomDateEditor( new  SimpleDateFormat( "yyyy-MM-dd" ), true ));
     }

 

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------spa

關於前臺angular使用get/post傳遞參數, 後臺接收code

get方式傳參orm

$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);
         }
     }
相關文章
相關標籤/搜索