spring mvc实现查询

实体类:User
package cn.bdqn.pojo;

public class User {
private String userName;
private String password;
private String position;
private String email; public User(String userName,String password,String position,String email){
super();
this.userName = userName;
this.password = password;
this.position = position;
this.email = email;
} public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
} }

控制器:注意@Controller不加就不是处理器

@Controller
@RequestMapping("/user")//项目部署的根目录http://localhost:8080/springmvc2/user/list
public class UserController {
private Map<String, User> userList= new HashMap<String, User>();
//添加数据
public UserController() {
userList.put("hl", new User("hl","123456","部门经理","hanlu@bdqn.cn"));
userList.put("zs", new User("zs","123456","质量经理","zhangsan@bdqn.cn"));
userList.put("zw", new User("zw","123456","开发工程师","zhangwei@bdqn.cn"));
userList.put("ly", new User("ly","123456","实施顾问","liyu@bdqn.cn"));
}
//查询
@RequestMapping(value="/userlist",method=RequestMethod.GET)
public String list(Model model){
      //把userList的集合对应到key(userlist)
model.addAttribute("userlist",userList);
return "user/userlist";//逻辑视图名
} }

建立页面userlist.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>list</title>
</head>
<body>
<c:forEach items="${userlist}" var="um">//不能用user
-----${um.value.userName}
-----${um.value.password}
-----${um.value.position}
-----${um.value.email}
<br/>
</c:forEach>
</body>
</html>

方法二:用ArrayList方法

@RequestMapping(value="/userlist",method=RequestMethod.GET)
public String list(HttpServletRequest request){
List<User> userList = new ArrayList<User>();
userList.add(new User("1","T01","111111","tO1@bdqn.cn"));
userList.add(new User("2","T02","222222","tO2@bdqn.cn"));
userList.add(new User("3","T03","333333","tO3@bdqn.cn"));
request.setAttribute("userList", userList);
return "user/userlist";
}

展示页面就得改成这样

<c:forEach items="${userList }" var="ul">
id:${user.id }<br/>
userName:${user.userName }<br/>
password:${user.password }<br/>
email:${user.email }<br/>
</c:forEach>
上一篇:ASP.NET 正则替换URL参数值


下一篇:iOS网络开发-AFNetworking请求asp.net WebService