我已经编写了一个程序,用于从java中的mysql数据库中选择特定列,我想使用jsp在我的服务器端显示它。 在我的 jsp 页面中,我有一个选择标签和一些选项,选择这些选项时将显示我的数据库中的选项的值
例如:机场将仅显示我已插入数据库的机场的值 当我在服务器上调试代码时,我可以看到像这样的值 [abc , xyz] 但是当我在服务器上运行它时,我只能看到我选择作为输出的选项(机场)而不是值。
所以我想我需要迭代列表,我对如何在jsp第2页中迭代列表进行了一些研究,但我想这不是我应该如何使用它的方式
上述代码:
java端,从数据库中选择列的java代码(工作正常)
public List readCategoryMsg(String gcm_msg_type) {
List msgList = new ArrayList<String>();
try {
Class.forName("com.mysql.jdbc.Driver");
connection = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/gcm",
"root", "root");
state = (Statement) connection.createStatement();
prep = (PreparedStatement) connection
.prepareStatement("Select gcm_message from gcm_msg where
gcm_msg_type = ?");
prep.setString(1, gcm_msg_type);
rSet = prep.executeQuery();
while (rSet.next()) {
String msg_type = rSet.getString("gcm_message");
msgList.add(msg_type);
}
connection.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return msgList;
}
jsp页面1:(其中有选择并将其提交到操作页面以显示输出的选项)
<form action="category_type_results.jsp" method="get">
<div align="left">
<br><select name="category_type">
<option value="airport">Airport</option>
<option value="art gallery">Art Gallery</option>
<option value="atm">ATM</option>
<option value="bank">Bank</option>
<option value="book store">Book Store</option>
<option value="bus station">Bus Station</option>
<option value="cafe">Cafe</option>
</div>
<div>
<input type="submit" value="Show Category Message " />
</div>
</form>
jsp 第 2 页:category_type_results.jsp
我已经注释掉了我尝试过但不正确的内容,需要一些帮助来了解如何显示数据库中所选选项的值列表
<%
ServiceSql serviceSql = new ServiceSql();
String gcm_msg_type = request.getParameter("category_type");
serviceSql.readCategoryMsg(gcm_msg_type);
//List<String> msgList = serviceSql.readCategoryMsg("gcm_msg_type");
//for (int i = 0; i < msgList.size(); i++) {
// msgList.get(i);
}
%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<h3>
<%=gcm_type_msg%>
//<%=msgList%>
</h3>
</body>
我的数据库列的外观,与我想在服务器端显示的列表相同
gcm_message(column name)
hello gcm (values)
hello gcm ..
hello gcm ..
hello gcm ..
hello gcm ..
hello gcm (values)
任何建议都会有很大帮助 谢谢你
最佳答案
使用JSTL迭代您的列表:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
</head>
<body>
<%
ServiceSql serviceSql = new ServiceSql();
String gcm_msg_type = request.getParameter("category_type");
List msgList= serviceSql.readCategoryMsg(gcm_msg_type);
request.setAttribute("msgList", msgList);
%>
<h3>
<c:forEach items="${msgList}" var="msg">
${msg}
</c:forEach>
</h3>
关于java - 如何根据从mysql数据库获取的值迭代jsp中的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28053352/