对于jsp中的下拉列表,getAttribute()返回null [英] getAttribute() returns null for drop down list in jsp
问题描述
我正在尝试使用getAttribute()将值从数据库传递到下拉菜单.但是,它返回null.
I am trying to pass value from database to drop down menu using getAttribute(). However, it returns null.
这是我的jsp(updateLecturer.jsp)文件:
This is my jsp (updateLecturer.jsp) file:
<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):
This is my 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:
This is my 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);
}
请帮助:(非常感谢
推荐答案
如果您在请求中设置了这样的属性
If you set some attribute in request like this
request.setAttribute("key",obj);
您可以通过这样的代码片段将其显示在jsp中
You can get it display in jsp by a scriplet like this
<%=request.getAttribute("key")%>
在您的情况下,请检查以下几点
In your case please check the following points
-
检查以下代码是否为您提供空值.
Check whether the following code is giving you a null.
String lecturerFullname = request.getParameter("LecturerFullame");
如果这为您提供了null,则请检查网址中传递的参数 LecturerFullame .
String lecturerFullname = request.getParameter("LecturerFullame");
If this gives you null then check whether your passing parameter LecturerFullame in url.
在jsp中,请将以下内容强制转换为正确的对象
In the jsp please cast the following to correct object
<option name="lecturerFullname"><%=((String)request.getAttribute("LecturerFullname"))%></option>
让我知道.
这篇关于对于jsp中的下拉列表,getAttribute()返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!