Struts2 Ajax---Action传值到JSP页面 Json格式

来源:互联网 发布:安卓arm linux打开端口 编辑:程序博客网 时间:2024/05/08 08:00

转载自:http://blog.sina.com.cn/s/blog_a345a8960101krct.html

有几个必须要的包

ezmorph-1.0.3.jar
json-lib-2.1.jar
struts2-json-plugin-2.1.8.1.jar
commons-lang-2.4.jar
commons-beanutils-1.7.0.jar

Action部分

@Controller
public class LookAction extends ActionSupport {
private int thispage; //设置私有变量,用于接受Jsp页面Ajxa传参(get、set方法要写上)
public int getThispage() {
return thispage;
}
public void setThispage(int thispage) {
this.thispage = thispage;
}
private JSONArray jarray; //创建JSONArray 用于向页面传递Json格式数据(get、set方法要写上)
public JSONArray getJarray() {
return jarray;
}
public void setJarray(JSONArray jarray) {
this.jarray = jarray;
}
@Resource LookServiceImp lookService;
@Override
public String execute() throws Exception {
Page page = new Page(thispage, 8);//Page分页工具类
List list = (List)lookService.getPage(page); //调用ServiceImp得到查询对象集合
JSONArray json = new JSONArray().fromObject(list);//创建JSONArray 用fromObject方法装入list集合 
jarray = json; //把集合对象转化为Json格式集合对象
return SUCCESS;
}
}

Struts.xml

<!-- 引用Json类型 成功后传递Action中编译好的json格式对象 -->
    <package name="Json" extends="json-default" namespace="/">
    <action name="look"  class="com.lovo.action.LookAction" method="execute">
    <result type="json" name="success">
    <param name="root">jarray</param>
    </result>
    </action>
    </package>

Jsp页面接收

$.post("look.action",
{thispage:page,},
function(data){
  $.each(data,function(i,list){
alert(list.id)
}
});

0 0
原创粉丝点击