1. 程式人生 > >SpringMVC傳參——物件,字串,陣列

SpringMVC傳參——物件,字串,陣列

PageResult.class

public class PagedResult<T> {
	
	private List<T> dataList;//資料
	
	private int currentPage;//當前頁
	
	private int pageSize;//條數
    //省略getter() setter()
}

1,ModelAndView返回值

       注意: 1.必須form提交才能接收引數

                   2.後臺接收方式—物件或引數名

前臺:

<form id="form" action="<%=basePath%>/demo/list.jhtml">
	<input name="pageNo" value="2" />
	<input name="pageSize" value="3" />
</form>
<button class="a">aaaaaa</button>

    $(".a").click(function(){
		$("#form").submit();
	});

後臺:

        @RequestMapping("/list")
	public ModelAndView list(Integer pageNo,Integer pageSize){//引數也可用PageResult接收
		pageNo = pageNo == null?1:pageNo;
		pageSize = pageSize == null?10:pageSize;
		PageHelper.startPage(pageNo,pageSize);  
		ModelAndView model = new ModelAndView();
		model.addObject("pageNo", pageNo);
		model.addObject("pageSize", pageSize);
		return model;
	}

2,JSON返回值---ajax-post

注意: @ResponseBody 需引入Jackson包  

<dependency>  
            <groupId>org.codehaus.jackson</groupId>  
            <artifactId>jackson-mapper-asl</artifactId>  
            <version>1.9.13</version>  
        </dependency>

1,json物件引數

 前臺:

<button class="a">aaaaaa</button>
$(".a").click(function(){
		$.ajax({
			url: '${ctx}/demo/list2.jhtml',
			method: "post",
			data: {currentPage: 1, pageSize: 3},
			dataType: "json",
			success: function(res){
				console.log("res", res);
			}
						
		});
});

  後臺:

第一種方式:  物件 / 引數名

                  ajax 不可加如下引數,否則後臺接收不到引數

                  contentType: "application/json;charset=utf-8"

@RequestMapping(value="/list2")
	@ResponseBody
	public Map<String, Integer> list2(PagedResult<User> page){//亦可用引數名接收
		int pageNo = page.getCurrentPage();
		int pageSize = page.getPageSize();
		System.out.println("pageNo: " + pageNo);
		System.out.println("pageSize: " + pageSize);
		Map<String, Integer> map = new HashMap<>();
        map.put("pageNo", pageNo);
        map.put("pageSize", pageSize);
        return map;
	}

第二種方式:  字元流

@RequestMapping(value="/list7")
	@ResponseBody
	public String list7(HttpServletRequest request) throws IOException{
		StringBuffer sb = new StringBuffer();  
        InputStream is = request.getInputStream();
        String xml = null;
        if(null != is) {
        	InputStreamReader isr = new InputStreamReader(is, "UTF-8");  
        	BufferedReader br = new BufferedReader(isr);  
        	String s = "";  
        	while ((s = br.readLine()) != null) {  
        		sb.append(s);  
        	}
        	br.close();
        	result = sb.toString();
        }
		return result;
	}

第三種方式: 類似get方式

             ajax需加上 contentType: "application/x-www-form-urlencoded"

@RequestMapping(value="/list3")
	@ResponseBody
	public Map<String, Integer> list3(HttpServletRequest request){
		Integer pageSize = Integer.valueOf(request.getParameter("pageSize"));
		Integer pageNo = Integer.valueOf(request.getParameter("currentPage"));
		System.out.println("currentPage: " + currentPage);
		System.out.println("pageSize: " + pageSize);
		Map<String, Integer> map = new HashMap<>();
		map.put("currentPage", currentPage);
		map.put("pageSize", pageSize);
		return map;
	}

2,JSON字串引數

前臺:

        注意:  前臺ajax需加上 contentType: "application/json;charset=utf-8"

<button class="a">aaaaaa</button>
$(".a").click(function(){
	$.ajax({
		url: '${ctx}/demo/list5.jhtml',
		method: "post",
		data: JSON.stringify({pageNo: 1, pageSize: 3}),
        contentType: "application/json;charset=utf-8",
		dataType: "json",
		success: function(res){
		    console.log("res", res);
		}
						
	});
});

後臺:

         注意: 引數前加@RequestBody註解

        這個好像不支援引數名接收,有了解的帥哥靚妹請告知在下,蟹蟹

 

@RequestMapping(value="/list5")
	@ResponseBody
	public Map<String, Integer> list5(@RequestBody PagedResult<User> page){
		int pageNo = page.getCurrentPage();
		int pageSize = page.getPageSize();
		System.out.println("pageNo: " + pageNo);
		System.out.println("pageSize: " + pageSize);
		Map<String, Integer> map = new HashMap<>();
        map.put("pageNo", pageNo);
        map.put("pageSize", pageSize);
        return map;
	}

3,陣列引數

前臺:

<button class="a">aaaaaa</button>
$(".a").click(function(){
		$.ajax({
			url: '${ctx}/demo/list8.jhtml',
			method: "post",
			data: {arr: ["1","2"]},
			dataType: "json",
			success: function(res){
				console.log("res", res);
			}
						
		});
});

後臺: list / 陣列

@RequestMapping(value="/list8")
	@ResponseBody
	public List<String> list7(@RequestParam("arr[]") List<String> arr) throws IOException{
		//引數亦可用String[] arr接收
		return arr;
	}