spring传集合

//@ModelAttribute("log")中的log  与form中的id相对应   form对象
	@RequestMapping(value="/login")
	public String login(@ModelAttribute User user ){
		//boolean flag=testService.login(user);
		if("jcl".equals(user.getName()) && "123".equals(user.getPassword())){
			System.out.print("登陆成功");
		}else {
			System.out.print("登陆失败");
		}
		return user.getName();
	}
	
	@RequestMapping(value="/list")
	public @ResponseBody String login(@ModelAttribute UserList list ){
		
		System.out.println(list);
		
		return "OK";
	}



/**
	 * post请求方式
	 */
	
	@Test
	public void postHTML(){
		//创建httpClient
		CloseableHttpClient httpClient=HttpClientBuilder.create().build();
		
		String url="http://localhost:8080/SpringMVC_01/list.do";
		
		List<NameValuePair> pairs=new ArrayList<NameValuePair>();
		
	/*	User user=new User();
		user.setName("jcl");
		user.setPassword("123");
		
		pairs.add(new BasicNameValuePair("user", user));*/
						
		pairs.add(new BasicNameValuePair("userID", "12"));
		pairs.add(new BasicNameValuePair("list[0].name", "12"));
		pairs.add(new BasicNameValuePair("list[0].password", "123"));
		
		//post请求方式
		HttpPost httpPost=new HttpPost(url);
		
		
		
		//创建响应
		CloseableHttpResponse response=null;
		try{
			//传递参数,并转码
			httpPost.setEntity(new UrlEncodedFormEntity(pairs,HTTP.UTF_8));
			
			response=httpClient.execute(httpPost);
			
			HttpEntity httpEntity=response.getEntity();
			
			String content=EntityUtils.toString(httpEntity);
			System.out.print(content);
			

			httpPost = new HttpPost(url);
			httpPost.setEntity(new UrlEncodedFormEntity(pairs,HTTP.UTF_8));
			
			response=httpClient.execute(httpPost);
			
			 httpEntity=response.getEntity();
			
			 content=EntityUtils.toString(httpEntity);
			System.out.print(content);

		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			try{
				httpClient.close();
			}catch (Exception e) {
				e.printStackTrace();
			}
			
		}
	}
}

package org.jcl.pojo;

import java.util.List;

public class UserList {
	String userID;
	private List<User> list;

	public String getUserID() {
		return userID;
	}

	public void setUserID(String userID) {
		this.userID = userID;
	}

	public List<User> getList() {
		return list;
	}

	public void setList(List<User> list) {
		this.list = list;
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值