如何在html中映射图像和文本?假设图像存在于图像文件夹中并且文本来自数据库?如何将它们放在网页中的一个位置?
假设 milkbottel 位于我的图像文件夹中,并且 milkbottle 文本来自数据库,那么如何 **
map dynamically
** 这两者的方式是奶瓶图像和奶瓶文本将位于网页中的一个位置?
最佳答案
数据库内容示例:
id (INT) | caption VARCHAR(255) | location VARCHAR(255)
1 milkbottle /images/milkbottle.jpg
2 orange /images/orange.jpg
JSP 示例
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title></title>
</head>
<body>
<table border="2">
<%
try {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost/test";
String username="root";
String password="root";
String query="SELECT * FROM image_table WHERE caption = 'milkbottle' LIMIT 1";
Connection conn=DriverManager.getConnection(url,username,password);
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(query);
if(rs != null) {
%>
<tr>
<td><%=rs.getString("caption"); %></td>
<td><img src='<%=rs.getString("location"); %>'/></td>
</tr>
<%
}
%>
</table>
<%
rs.close();
stmt.close();
conn.close();
}
catch(Exception e)
{
e.printStackTrace();
}
%>
</body>
</form>
</html>
关于javascript - 如何映射来自不同来源的图像和文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28197846/