我在项目中使用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;
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;
}
}
DAO.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>
答案 0 :(得分:0)
首先,您不需要包含两个单独的jquery库:ver 1.11 and ver 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.
}
});
});