//@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;
}
}