页面ajax请求传参及java后端数据接收

Controller:
package com.ysl.PassingParameters.controller;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ysl.PassingParameters.bean.User;
import com.ysl.PassingParameters.dto.RetMsg;

@Controller
public class TestController {

/**
 * List传参
 * @param listString
 * @return
 */
@RequestMapping("/listString")
@ResponseBody
public RetMsg listString(@RequestParam("listString[]") List listString){
    System.out.println("listString:"+listString.toString());
    return RetMsg.success();
}


/**
 * List传参
 * @param listUser
 * @return
 */
@RequestMapping("/listUsers")
@ResponseBody
public RetMsg listUsers(@RequestBody List listUser){
    System.out.println("username:"+listUser.get(0).getUsername());
    return RetMsg.success();
}


/**
 * User[]传参
 * @param arrayUsers
 * @return
 */
@RequestMapping("/arrayUsers")
@ResponseBody
public RetMsg arrayUsers(@RequestBody User[] arrayUsers){
    System.out.println("username:"+arrayUsers[0].getUsername());
    return RetMsg.success();
}


/**
 * List>传参
 * @param listMap
 * @return
 */
@RequestMapping("/listMap")
@ResponseBody
public RetMsg listMap(@RequestBody List> listMap){
    System.out.println("username:"+listMap.get(0).get("username"));
    return RetMsg.success();
}

/**
 * User对象传参
 * @param arrayUsers
 * @return
 */
@RequestMapping("/users")
@ResponseBody
public RetMsg users(@RequestBody User users){
    System.out.println("username:"+users.getUsername()); 
    System.out.println("username:"+users.getList().get(0).getUsername()); 
    return RetMsg.success();
}

}

页面:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>

<%application.setAttribute("path", request.getContextPath());%>

    


Hello World!

@RequestBody主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的);

GET方式无请求体,所以使用@RequestBody接收数据时,前端不能使用GET方式提交数据,而是用POST方式进行提交。

你可能感兴趣的:(Ajax,java,力量)