我在这里疯狂试图找出,为什么我的JSP没有在表格中显示数据。像servlet这样的连接到MySQL并填充数据,但是我不能将这些数据推送到JSP.setAttribute()方法正在工作(对象在那里)。我无法提取特定信息并在浏览器中显示格式化表单。如果有人可以指出我如何解决此问题...为什么JSTL表达式不会加载正确的信息.Eclipse不会返回任何错误和整个应用程序编译....它只显示空白页面。您可以找到代码。
提示:我正在测试是否可以推送LIST并且可以显示对象。它有效,所以对象是可见的"到JSP。当我尝试加载特定信息(用户名,密码或电子邮件)时,它返回空白。感谢您提供任何帮助/提示。
您可以在下面找到代码。
UserBean.java
package com.admin;
public class UserBean {
private String password;
private String firstname;
private String lastname;
private String email;
private String usertype;
private String datecreated;
private String username;
public UserBean(){};
public UserBean(String username2, String password2, String firstname2, String lastname2, String email2,
String usertype2, String datecreated2) {
}
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 getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUsertype() {
return usertype;
}
public void setUsertype(String usertype) {
this.usertype = usertype;
}
public String getDatecreated() {
return datecreated;
}
public void setDatecreated(String datecreated) {
this.datecreated = datecreated;
}
}
User.DAO.java
package com.admin;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.clientdb.ConnectionManager;
public class UserDAO {
public static List<UserBean> listOfUsers()
{
List<UserBean> userList=new ArrayList<>();
String query="SELECT * FROM users";
try
{
Connection conn = ConnectionManager.getConnection();
Statement stm = conn.createStatement();
ResultSet rs = stm.executeQuery(query);
while(rs.next())
{
String username=rs.getString("username");
String password=rs.getString("password");
String firstname=rs.getString("firstname");
String lastname=rs.getString("lastname");
String email=rs.getString("email");
String usertype=rs.getString("usertype");
String datecreated=rs.getString("date_created");
UserBean user=new UserBean(username,password,firstname,lastname,email,usertype,datecreated);
userList.add(user);
}
stm.close();
rs.close();
conn.close();
}
catch(Exception ex){System.out.println(ex);}
return userList;
}}
UserAdmin.java
package com.admin;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class UserAdmin extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List<UserBean> userList = UserDAO.listOfUsers();
request.getSession().setAttribute("userList",userList);
request.getRequestDispatcher("userlist.jsp").forward(request,response);
}
}
userlist.jsp
<table class="table table-hover" style="margin-top:20px;">
<thead>
<tr>
<th>Username</th>
<th>Password</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>User Type</th>
<th>Date Created</th>
</tr>
</thead>
<tbody>
<c:forEach items="${userList}" var="user">
<tr>
<td><c:out value="${user.username}"/></td>
<td><c:out value="${user.password}"/></td>
<td><c:out value="${user.firstname}"/></td>
<td><c:out value="${user.lastname}"/></td>
<td><c:out value="${user.email}"/></td>
<td><c:out value="${user.usertype}"/></td>
<td><c:out value="${user.datecreated}"/></td>
<td>
<a href="reset.html" role="button" class="btn btn-primary">RESET</a>
<!-- <button type="button" class="btn btn-danger" data-toggle="modal" data-target="#userDeleteModal">DELETE</button>-->
<a href="deleteUser.do?email=${user.email}" role="button" class="btn btn-danger">DELETE</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
答案 0 :(得分:1)
UserBean的构造函数不会为字段赋值。
public UserBean(String username2, String password2, String firstname2, String lastname2, String email2,
String usertype2, String datecreated2) {
username = username2;
password = password2;
// ... etc
}
你还记得导入c:core吗?
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
答案 1 :(得分:-1)
试试这个:
<c:forEach items="${sessionScope.userList}" var="user">
<tr>
<td><c:out value="${user.username}"/></td>
<td><c:out value="${user.password}"/></td>
<td><c:out value="${user.firstname}"/></td>
<td><c:out value="${user.lastname}"/></td>
<td><c:out value="${user.email}"/></td>
<td><c:out value="${user.usertype}"/></td>
<td><c:out value="${user.datecreated}"/></td>
<td>
<a href="reset.html" role="button" class="btn btn-primary">RESET</a>
<!-- <button type="button" class="btn btn-danger" data-toggle="modal" data-target="#userDeleteModal">DELETE</button>-->
<a href="deleteUser.do?email=${user.email}" role="button" class="btn btn-danger">DELETE</a>
</td>
</tr>
</c:forEach>
答案 2 :(得分:-1)
<c:forEach items="${sessionScope.userList}" var="user">