我想在页面加载时填充选择框,但它不起作用
这是我的html代码文件1
<button id="edit-btn" onclick="popupwindow('edituser.jsp','Add new user',500,300)">Edit user</button>
文件2
<script>
$(document).ready(function(){
$.ajax({
type: "POST",
url: "${pageContext.request.contextPath}/MyServlet",
data: "action=list",
success: function(data){
alert("success");
},
error: function(jqXHR, textStatus, errorThrown) {
alert(errorThrown)
}
});
});
</script>
<select name="currentusername" id="current-username" style="width:157px;">
</select>
米道
public List getAllUserNames(){
List<UserBean> users = new ArrayList<UserBean>();
try {
String sql = "SELECT username FROM users ";
PreparedStatement ps = connection.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
if (rs != null) {
while (rs.next()) {
UserBean userBean = new UserBean();
System.out.println(rs.getString("username"));
userBean.setUsername(rs.getString("username"));
users.add(userBean);
}
}
else{
System.out.println("No users");
}
} catch (SQLException e) {
e.printStackTrace();
}
return users;
}
小服务程序
if(action.equalsIgnoreCase("list")){
System.out.println("list");
request.setAttribute("users", dao.getAllUserNames());
}