简体   繁体   English

如何在不对HTML图像进行硬编码的情况下实现Java Servlet图像的幻灯片显示?

[英]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列表

  • as a JavaScript array in the HTML code. 作为HTML代码中的JavaScript数组。 The servlet code would have to generate the JavaScript code defining an array of IDs Servlet代码必须生成定义ID数组的JavaScript代码
  • as an Iterator at server-side, in a session attribute. 在会话属性中作为服务器端的迭代器。 To display the next photo, the displayphoto servlet would have to get the iterator from the session, get its next ID, and serve the bytes of the image having this ID 要显示下一张照片,displayphoto servlet必须从会话中获取迭代器,获取下一个ID,并提供具有该ID的图像的字节
  • nowhere. 无处。 Each time a new photo must be displayed, its index would be passed to the displayphoto servlet, and the servlet would generate a SQL query to get the photo at this index. 每次必须显示新照片时,它的索引都将传递到displayphoto servlet,并且该servlet将生成SQL查询以将照片获取到该索引。 Of course, the SQL query would have to use some sort order to always return the photos in the same order. 当然,SQL查询必须使用某种排序顺序才能始终以相同顺序返回照片。

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.

相关问题 在Java 8中,如何在不在我的环境中对其进行硬编码的情况下获取主机名? - In Java 8, how do I get my hostname without hard-coding it in my environment? 如何在不对URL进行硬编码的情况下调用REST API get方法? - How can I call my REST API get method without hard-coding a URL? 我可以在没有硬编码的情况下在 Windows 上运行 Java 中构建 Linux 路径吗? - Can I build a Linux path in Java running on Windows without hard-coding? 从工厂请求项目,而无需对每种情况进行硬编码 - Requesting item from Factory without hard-coding every case 如何在不对目录进行硬编码的情况下将某些内容保存到桌面? - How to save something to the desktop without hard-coding the directory? 用Java硬编码Map的替代方法? - Alternative ways for hard-coding Map in Java? 从 android 应用程序访问 AWS S3,无需硬编码访问密钥 - Accessing AWS S3 from android app without hard-coding access keys 如何实现睡眠以显示图像的幻灯片 - How to implement sleep to display a slideshow of images 从Java中的图像列表制作幻灯片? - making slideshow from list of images in java? 避免硬编码休眠属性 - Avoid hard-coding Hibernate properties
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM