Java 使用jquery动态设置图像标记的src
我正在我的项目中使用Struts。我希望每当我的页面加载一个ajax调用时,它都会在我的Action类中使用包含图像链接的对象填充ArrayList。我在jsp页面中使用此ArrayList来填充图像src。我是jquery新手,所以请查看您是否能提供帮助。提前感谢 Home.javaJava 使用jquery动态设置图像标记的src,java,jquery,ajax,struts2,hibernate-4.x,Java,Jquery,Ajax,Struts2,Hibernate 4.x,我正在我的项目中使用Struts。我希望每当我的页面加载一个ajax调用时,它都会在我的Action类中使用包含图像链接的对象填充ArrayList。我在jsp页面中使用此ArrayList来填充图像src。我是jquery新手,所以请查看您是否能提供帮助。提前感谢 Home.java package com.rst; import java.util.ArrayList; import java.util.List; import org.hibernate.Session; impor
package com.rst;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class Home extends ActionSupport{
Product product = new Product();
DAO d = new DAO();
List<Product> products = new ArrayList<Product>();
public String execute(){
try{
Session session = HibernateUtil.getSessionFactory().openSession();
System.out.println("in action");
products = d.getImages(session);
for(Product p:products){
System.out.println(p.getProduct_name());
}
}
catch(Exception e){
e.printStackTrace();
}
return SUCCESS;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public List<Product> getProducts() {
return products;
}
public void setProducts(List<Product> products) {
this.products = products;
}
}
java
package com.rst;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
public class DAO {
public List<Product> getImages(Session session){
Transaction t = session.beginTransaction();
List<Product> list = new ArrayList<Product>();
try{
System.out.println("in dao method");
Query q = session.createQuery("from com.rst.Product as p where p.flag = 'new'");
list = q.list();
}
catch(Exception e){
e.printStackTrace();
}
t.commit();
session.close();
System.out.println("success");
return list;
}
}
home.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"</script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery
/1.6/jquery.min.js"></script>
<title>Products</title>
<script>
$.ajax({url:"home",success:function(result){
var x = document.images;
$(x[0]).attr("src", <s:property value="products[0].image_link"/>);
$(x[1]).attr("src", <s:property value="products[1].image_link"/>);
$("#main").show();
}});
</script>
<style type="text/css">
#main{
visibility: hidden;
}
</style>
</head>
<body>
<b>Example of Iterator Tag</b><br/>
<div id = main>
<h1>welcome</h1>
<img src= "" />
<img src=""/>
<br/>
</div>
</body>
</html>
首先,您不需要包含两个单独的jquery库:1.11版和1.6版,我建议您删除1.6版: 还有一个隐藏具有显示属性的元素的建议:
#main{
display:none;
}
在标记部分,可以将id值用引号括起来:
<div id ="main">
然后在jQuery中,您需要将ajax调用放在doc ready块中:
$(function () {
$.ajax({
url: "home", // <--your controller method
type: 'post', //<---type post | get your choice
dataType: 'json', //<---should be json as response seems js object with key value pairs
success: function (result) {
$('#main img').each(function () { // loop in #main's images
$(this).attr('src', result.image_link); // and place the src here
});
$("#main").show(); // <---then in the last you have to show it.
}
});
});
html是什么样子的?您是否至少尝试过使用ajax检索JSP上的记录?当我从另一个页面调用home.JSP时,通过调用我的操作类(即home.java)来调用index.JSP,它在f9中工作。我的意思是图像显示得非常完美。在JavaScript中,您需要在图像标记上正确引用attr调用中的值。谢谢您的帮助。但是如何从我的操作类发送结果。@user3824596该操作是用Java进行的。你是如何定义你的行为的?