[英]How do I implement a slideshow of images from java servlets without hard-coding the images in my html?
As a learning exercise I have adapted a series of Java servlets written by BalusC to store a list of images uploaded in a mysql database. 作为学习练习,我改编了BalusC编写的一系列Java servlet,以将上载的图像列表存储在mysql数据库中。 The application works fine and displays a list of the images in the sequence they are read from the database statically in html.
该应用程序运行良好,并按照从HTML静态地从数据库中读取图像的顺序显示图像列表。 Howver, I am trying to adapt the html to display these dynamically in a slideshow.
但是,我正在尝试改编html以在幻灯片中动态显示这些内容。 I have found several examples on the internet but all of them seem to hard-code the names of images in the browser code.
我在互联网上找到了几个示例,但是所有这些示例似乎都在浏览器代码中用硬编码了图像名称。 I'm looking for some way of transfering the images read from the database query to a slideshow in the client.
我正在寻找一种将从数据库查询中读取的图像传输到客户端中的幻灯片的方法。
The original source is here 原始资料在这里
After having researched several examples it seems that using javascript is the most common way for doing it, but I cannot find an example which does it dynamically here is my attempt (I am using javascript embedded with html within a java servlet) at it which fails to do more that print out the titles of the images in a consecutive list on the page (but no image): 在研究了几个示例之后,似乎使用javascript是最常用的方法,但是我找不到一个可以动态执行示例的示例,这是我的尝试(我正在使用Java servlet内嵌入html的javascript),但失败了执行更多操作以打印出页面上连续列表中的图像标题(但无图像):
try {
Class.forName(driver);
Connection con = DriverManager.getConnection(url+dbName, userName, password);
PreparedStatement ps = con.prepareStatement("select * from photos");
ResultSet rs = ps.executeQuery();
out.println("<h1>Photos</h1>");
while ( rs.next()) {
out.println("<h4>" + rs.getString("title") + "</h4>");
out.println("<script type=\"text/javascript\">");
out.println(" function clearScreen(){");
out.println(" document.image.src=displayphoto?id=\" + rs.getString(\"id\")\"");
out.println(" document.body.innerHTML=\"\";");
out.println(" setTimeout('clearScreen()','5000');}");
out.println("</script>");
}
con.close();
}
Any pointers or suggestions on a better way of doing this, even purely with Java servlets would be greatly appreciated. 任何更好的实现方法的指针或建议,甚至纯Java servlet都将不胜感激。
Many thanks 非常感谢
Alastair 阿拉斯泰尔
This code generates a <script>
block defining the same clearScreen
function for each photo found in the table (but with a different photo ID at each iteration). 此代码生成一个
<script>
块,该块为表中找到的每张照片定义了相同的clearScreen
函数(但每次迭代具有不同的照片ID)。
It's not very clear what you're trying to do. 尚不清楚您要做什么。 To do a slideshow, you have to iterate over a list of picture IDs.
要进行幻灯片放映,您必须遍历图片ID列表。 You can store this list of IDs
您可以存储此ID列表
If your goal is to implement the first solution, it's really not difficult. 如果您的目标是实施第一个解决方案,那实际上并不困难。 Here is how an array of three IDs (id1, id2 and id3) is declared in JavaScript:
这是在JavaScript中声明三个ID(id1,id2和id3)的数组的方式:
var ids = ['id1', 'id2', 'id3'];
So, all you have to do is to generate the above String frm a List<String>
containing "id1"
, "id2"
and "id3"
. 因此,您要做的就是生成一个上面的String frm一个包含
"id1"
, "id2"
和"id3"
的List<String>
。 A simple loop will do: 一个简单的循环即可:
StringBuilder jsArrayDeclaration = new StringBuilder();
jsArrayDeclaration.append("var ids = [");
boolean first = true;
for (String id : listOfIds) {
if (!first) {
jsArrayDeclaration.append(", ");
}
first = false;
jsArrayDeclaration.append("'");
jsArrayDeclaration.append(id);
jsArrayDeclaration.append("'");
}
jsArrayDeclaration.append("];");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.