如何使用AJax获得数据,并将此数据封装在request的属性值中,并且页面可以访问

「已注销」 「已注销」 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
在JSP页面中需要从后端获取最新商品集合newList,将此newList数据使用EL在<c:forEach items="${newList}" var="p">中遍历
但此种方法破坏了MVC模式,不可取


//JSP页面中的Javay语言,这种写法不可取
<%

ProductService productService=new ProductServiceIpml();
List<Product> newList=productService.findByNew();
request.setAttribute("newList",newList);
%>
//遍历上面的newList属性,
<c:forEach items="${newList}" var="p">
<div class="col-md-2" style="text-align:center;height:200px;padding:10px 0px;">
<p><a href="#"'>${p.pname}</a></p>
</div>
</c:forEach>


想用Ajax来调用Servlet,获取newList的值并将其设置为request.setAttribute("newList",newList);,然而,Ajax是在页面完全加载完成之后才调用后端数据的,页面在中的<c:forEach items="${newList}" var="p"></c:forEach>的${newList}在调用Ajax前为空,调用之后,页面早就加载完毕

那么该如何使得

1.页面调用Ajax去获取newList数据

$(function () {
$.post(URL,Param,function (flag) {
/*

访问URL获取newList数据
*/
});
});

2.并将数据装在
request.setAttribute("newList",newList);

3.页面能够遍历数据

<c:forEach items="${newList}" var="p">
<div class="col-md-2" style="text-align:center;height:200px;padding:10px 0px;">
<p><a href="#"'>${p.pname}</a></p>
</div>
</c:forEach>


那么该如何解决这个问题呢?
谢谢各位,不胜感激!!!
...全文

相关推荐

评论 4
收藏 已收藏
分享
返回顶部
「已注销」
J2ME 13,096 成员 26,491 内容