[英]getAttribute() returns null for drop down list in jsp
我试图使用getAttribute()将值从数据库传递到下拉菜单。 但是,它返回null。
这是我的jsp(updateLecturer.jsp)文件:
<form action="updateLecturer" class="sky-form">
<header>Update Lecturer Information</header>
<center>
<fieldset>
<section>
<label class="select">
<select name="selectLecturer" id="lecturerFullname">
<option value="0" selected disabled>Lecturers Name</option>
**<option name="lecturerFullname"><%=((LecturerBean)request.getAttribute("LecturerFullname"))%></option>**
</select>
</label>
</section>
</center>
<footer>
<center><button type="submit" class="button">Update</button></center>
</footer>
</form>
这是我的Servlet UpdateLecturerServlet.java):
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
String lecturerFullname = request.getParameter("LecturerFullame");
LecturerBean lecturer = new LecturerBean();
lecturer.setLecturerFullname(lecturerFullname);
request.setAttribute("LecturerFullname",lecturerFullname);
RequestDispatcher view = getServletContext().getRequestDispatcher("/updateLecturer.jsp");
view.forward(request,response);
}
这是我的UpdateLecturerDAO:
static Connection currentCon = null;
static ResultSet rs = null;
public static LecturerBean selectlecturer(LecturerBean Lbean) {
// preparing some objects for connection
System.out.println("JIJIJI");
Statement stmt = null;
String lecturerFullname = Lbean.getLecturerFullname();
System.out.println("j444444");
String searchQuery = "select lecturerfullname from lecturer";
System.out.println("Your lecturer is " + lecturerFullname);
System.out.println("Query: " + searchQuery);
try {
// connect to DB
currentCon = JavaConnectionDB.getConnection();
stmt = currentCon.createStatement();
rs = stmt.executeQuery(searchQuery);
// boolean more = rs.next();
while(rs.next())
{
LecturerBean lecturer = new LecturerBean();
lecturer.setLecturerFullname(rs.getString("LecturerFullname"));
lecturer.add(lecturer);
}
}
catch (Exception ex) {
System.out.println("Select failed: An Exception has occurred! " + ex);
}
我以前从未使用过任何此类代码,因此我不确定属性系统如何工作,但我注意到您的代码中存在异常:
设置位置: request.setAttribute("LecturerFullname",lecturerFullname);
您将在何处获得: lecturer.setLecturerFullname(request.getAttribute("lecturerFullname",lecturer));
注意到了吗? 在您要设置它的地方,放一个大写的“ L”,我相信它是区分大小写的。 尝试时不会受伤。
如果您在请求中设置这样的属性
request.setAttribute("key",obj);
您可以像这样通过脚本将其显示在jsp中
<%=request.getAttribute("key")%>
您的情况请检查以下几点
检查以下代码是否为您提供了空值。
String lecturerFullname = request.getParameter("LecturerFullame");
如果这为您提供了null,请检查url中是否传递了参数LecturerFullame 。
在jsp中,请强制以下内容以更正对象
<option name="lecturerFullname"><%=((String)request.getAttribute("LecturerFullname"))%></option>
让我知道。
在UpdateLecturerServlet.java
。
//calling selectlecturer() to retrieve list full names and store it in session
request.setAttribute("LecturerFullname",selectlecturer());
RequestDispatcher view = getServletContext().getRequestDispatcher("/updateLecturer.jsp");
view.forward(request,response);
现在是selectlecturer()
方法。
public static List<LecturerBean> selectlecturer() {
//DB query to retrieve all lecturer fullnames
List<LecturerBean> lecturers = new ArrayList<LecturerBean>();
while(rs.next())
{
LecturerBean lecturer = new LecturerBean();
lecturer.setLecturerFullname(rs.getString("LecturerFullname"));
lecturers.add(lecturer);
}
return lecturers;//return the list lecturer fullnames
}
在updateLecturer.jsp
。
<%
java.util.List<LecturerBean> lists = (java.util.List<LecturerBean>)request.getAttribute("LecturerFullname"));
for(LecturerBean bean:lists)
{
%>
<option name="lecturerFullname"><%=bean.getLecturerFullname()%></option>
<%
}
%>
但是您应该避免在JSP中使用Java代码 。 仅供参考,我正在提供此代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.