JSP表达式语言不从JavaBean呈现属性
嗯,这个错误肯定是由于我犯了某种错误造成的。下面是我的代码 servlet:JSP表达式语言不从JavaBean呈现属性,jsp,jakarta-ee,model-view-controller,javabeans,el,Jsp,Jakarta Ee,Model View Controller,Javabeans,El,嗯,这个错误肯定是由于我犯了某种错误造成的。下面是我的代码 servlet: package Servlets; // import statements public class AdminResource extends HttpServlet { List<userList> users = new ArrayList<>(); protected void processRequest(HttpServletRequest request,
package Servlets;
// import statements
public class AdminResource extends HttpServlet {
List<userList> users = new ArrayList<>();
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Database db = (Database) getServletContext().getAttribute("db");
String sql = "select * from TAHMID_NIPS.GROUPS";
ResultSet rs;
try {
rs = db.runSql(sql);
// int i = 0;
while (rs.next()) {
userList user = new userList(rs.getString("USERNAME"), rs.getString("GROUPID"));
users.add(user);
/* used a printWriter here to access the bean in the servlet
out.println(users.get(i).getUserName());
out.println(users.get(i).getGroupID());
i++;
*/
}
request.setAttribute("users", users);
request.getRequestDispatcher("userList.jsp").forward(request, response);
} catch(SQLException ex){}
}
// doGet(), doPost(), getServletInfo() methods..
}
调度的JSP视图:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>These are the users in the database!</h1><br>
<c:forEach var="iterator" items="${users}">
<c:out value="${iterator.UserName}"/> <br>
</c:forEach>
</body>
</html>
JSP页面
这些是数据库中的用户
在web.xml文件中声明数据库URL、密码和其他必需信息,并实现contextListener类。当数据显示在servlet中时,应用程序工作正常。但由于我们是MVC的粉丝,当我试图用EL访问bean时,问题就开始了。无法访问这些字段
关于数据库:这是一个名为Groups的表,有两个字段:UserName、GroupID。但是它们都没有显示在JSP视图中
问题可能发生在JSP视图中的EL中。专家们,请帮忙 更改名称并使用java命名标准,如变量
用户名
、组ID
和类用户列表
,因为两个名称的getter和setter方法相同。同时更改此行
userList user=newuserlist(rs.getString(“用户名”)、rs.getString(“GROUPID”)代码>到这个
userList user=newuserlist(rs.getString(“GROUPID”)、rs.getString(“USERNAME”)代码>
由于您的构造函数。请参阅。实际上,您必须以某种方式(比如作为请求或会话属性)将bean传递给JSP,然后才能在EL上调用它。另外,为了更好的安全性,最好不要使用纯EL。让代码正常工作的最简单方法是更改Servlet AdminResource中的一行。更改模型列表(上下文);请求.setAttribute(“用户”,model.list(上下文))@t0mppa我根据您提供的类似问题链接更改了servlet代码并删除了查询提供程序java类。然后,我能够使用servlet中的注释代码部分访问servlet中的bean。但是仍然无法在JSP中查看记录。JSPEL中是否有users.get(i).getUserName()的等价物?或者我需要做些修改吗?请帮忙。谢谢你提供的链接。至少现在我确信这个bean工作得很好。新年快乐..你能改变你的bean以符合Java命名标准吗,例如,类名UserList
和字段userName
和groupID
,然后把EL改成${iterator.userName}
?那么它应该被正确地提取。所以你不能提取用户名,因为你忽略了命名标准?这是一些有趣的行为。很高兴知道。你应该把这当作一个答案@t0mppa。
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>These are the users in the database!</h1><br>
<c:forEach var="iterator" items="${users}">
<c:out value="${iterator.UserName}"/> <br>
</c:forEach>
</body>
</html>