需导入JSON包
一、action文件
package com.action;
import java.util.ArrayList;import java.util.List;
import org.apache.struts2.json.annotations.JSON;
import com.opensymphony.xwork2.ActionSupport;
public class OrderAction extends ActionSupport{
int orderId;
List<OrderItem> ois=new ArrayList<OrderItem>();
@JSON(serialize=false)
public int getOrderId() {
return orderId;
}
public void setOrderId(int orderId) {
this.orderId = orderId;
}
public List<OrderItem> getOis() {
return ois;
}
public void setOis(List<OrderItem> ois) {
this.ois = ois;
}
public String hh(){
//queryOrderItemsByOrderId(orderId)
if(orderId==100){
OrderItem oi1=new OrderItem();
oi1.setIoId(10);
oi1.setAmount(10);
oi1.setPrice(100.5);
oi1.setProductName("shuibei");
OrderItem oi2=new OrderItem();
oi2.setIoId(10);
oi2.setAmount(10);
oi2.setPrice(100.5);
oi2.setProductName("yashua");
ois.add(oi1);
ois.add(oi2);
}else if(orderId==101){
OrderItem oi1=new OrderItem();
oi1.setIoId(10);
oi1.setAmount(10);
oi1.setPrice(100.5);
oi1.setProductName("shuibei2");
OrderItem oi2=new OrderItem();
oi2.setIoId(10);
oi2.setAmount(10);
oi2.setPrice(100.5);
oi2.setProductName("yashua2");
ois.add(oi1);
ois.add(oi2);
}
return "ok";
}
}
二、action配置文件
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package namespace="/abc" name="hh" extends="json-default">
<action name="order" class="com.action.OrderAction" method="hh">
<result name="ok" type="json"></result>
</action>
</package>
</struts>
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
三、jsp文件
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'ab.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<style type="text/css">
dd{
width: 80%;
background-color: #42A8FF;
margin-bottom: 2px;
margin-top: 2px;
display:none;
}
.a{
color: red;
}
</style>
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("span.dd").hover(
function(){
$(this).addClass("a");
},
function(){
$(this).removeClass("a");
}
);
$("span.dd").click(function(){
var id=$(this).attr("name");
var cur_div=$(this).parent().parent();
//alert(id);
//{"ois":[{"ioId":xx,"productName":"xx","price":xxx,"amount":xxx},{xxxxx}]}
$.post("/ajax_struts/abc/order.action"
,"orderId="+id
,function(abc){
for(var i=0;i<abc.ois.length;i++){
cur_div.after("<dd>"+abc.ois[i].ioId+" "+abc.ois[i].productName+" "+abc.ois[i].price+" "+abc.ois[i].amount+"</dd>");
}
$("dd").show("slow");
}
,"json");
});
});
</script>
</head>
<body style="background-color:#FFCE2E">
<dl>
<dt name="ddddd" style="width: 100%;background-color: #42A8FF; margin-bottom: 1px" >
<div style="float:left;width:90%">order1</div>
<div name="det" style="width:100%; text-align: right"><span class="dd" name="100" style="cursor: pointer">详情</span></div>
</dt>
<dt style="width: 100%;background-color: #42A8FF; margin-bottom: 1px" >
<div style="float:left;width:90%">order2</div>
<div name="det" style="width:100%; text-align: right"><span class="dd" name="101" style="cursor: pointer">详情</span></div>
</dt>
</dl>
</body>
</html>