使用JSTL和EL,设计用户类User,包含性别,年龄,性别,生日,电话、爱好这些字段,并添加有参构造方法和对应字段的getter、setter方法,并输出用户列表的数据
时间: 2023-05-30 16:03:14 浏览: 199
User类代码:
```java
public class User {
private String name;
private String gender;
private int age;
private Date birthday;
private String phone;
private List<String> hobbies;
public User(String name, String gender, int age, Date birthday, String phone, List<String> hobbies) {
this.name = name;
this.gender = gender;
this.age = age;
this.birthday = birthday;
this.phone = phone;
this.hobbies = hobbies;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public List<String> getHobbies() {
return hobbies;
}
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
}
```
输出用户列表的JSP页面代码:
```jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html>
<head>
<title>User List</title>
</head>
<body>
<table>
<tr>
<th>Name</th>
<th>Gender</th>
<th>Age</th>
<th>Birthday</th>
<th>Phone</th>
<th>Hobbies</th>
</tr>
<c:forEach items="${users}" var="user">
<tr>
<td>${user.name}</td>
<td>${user.gender}</td>
<td>${user.age}</td>
<td><fmt:formatDate value="${user.birthday}" pattern="yyyy-MM-dd"/></td>
<td>${user.phone}</td>
<td>
<c:forEach items="${user.hobbies}" var="hobby" varStatus="status">
${hobby}<c:if test="${!status.last}">, </c:if>
</c:forEach>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
```
在Servlet中获取用户列表并向JSP页面传递:
```java
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<User> users = new ArrayList<>();
users.add(new User("Tom", "male", 20, new Date(), "123456789", Arrays.asList("reading", "music")));
users.add(new User("Jane", "female", 22, new Date(), "987654321", Arrays.asList("traveling", "swimming")));
request.setAttribute("users", users);
request.getRequestDispatcher("user_list.jsp").forward(request, response);
}
```
运行结果:
```
Name Gender Age Birthday Phone Hobbies
Tom male 20 2022-02-18 123456789 reading, music
Jane female 22 2022-02-18 987654321 traveling, swimming
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)