struts2+json配置方式和jar包

Struts2.0的Action

 

 

package unite.action.index;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import unite.dao.AddCartDAO;
import unite.util.pages.PageUtil;
import unite.util.s2services.Services;
import unite.vo.Bestellt;
import unite.vo.Orderform;
import unite.vo.Userdateperfect;

import com.googlecode.jsonplugin.annotations.JSON;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class AddCartAction extends ActionSupport {

    private Bestellt bestellt;
    private PageUtil pageUtil=new PageUtil();
    private Userdateperfect userdate;
    @SuppressWarnings("unchecked")
    private List list=new ArrayList();
    private String udaddress1;
    private String udaddress2;
    private String udaddress3;
    private String typeString;
    
    
    @SuppressWarnings("unchecked")
    public List getList() {
        return list;
    }



    @SuppressWarnings("unchecked")
    public void setList(List list) {
        this.list = list;
    }



    public AddCartAction() {
        bestellt = new Bestellt();
    }

    

    public PageUtil getPageUtil() {
        return pageUtil;
    }



    public void setPageUtil(PageUtil pageUtil) {
        this.pageUtil = pageUtil;
    }


      @JSON(serialize=false)
    public String getTypeString() {
        return typeString;
    }



    public void setTypeString(String typeString) {
        this.typeString = typeString;
    }


      @JSON(serialize=false)
    public String getUdaddress1() {
        return udaddress1;
    }



    public void setUdaddress1(String udaddress1) {
        this.udaddress1 = udaddress1;
    }


      @JSON(serialize=false)
    public String getUdaddress2() {
        return udaddress2;
    }



    public void setUdaddress2(String udaddress2) {
        this.udaddress2 = udaddress2;
    }

//这里是注解方式屏蔽不需要转json的变量
      @JSON(serialize=false)
    public String getUdaddress3() {
        return udaddress3;
    }



    public void setUdaddress3(String udaddress3) {
        this.udaddress3 = udaddress3;
    }


      @JSON(serialize=false)
    public Userdateperfect getUserdate() {
        return userdate;
    }


    public void setUserdate(Userdateperfect userdate) {
        this.userdate = userdate;
    }


    /** 查询备选地址 */
public String addCartAddressList() throws Exception {
list=new ArrayList();

User user=new User();

user.setUdid(1);

Orderform orderform=new Orderform();

orderform.setOfid(1);

user.getOrderforms().add(orderform);

list.add(user);

pageUtil.setCurrentPage(1);

pageUtil.setNextPage(false);
}

return SUCCESS;
}

}
 

 

Struts2.0的xml

 

 

 

注:只会返回list的json格式

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC
 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
 <package name="AddCartAction" namespace="" extends="json-default">

<!-- 备选地址列表 

配置多个对象时要注意按action对象的前后循序摆放,如果是boolean类型的不管你的变量名开头字母是大写还是小写,这里配置都要写小写的

-->
 <action name="addCartAddressList" class="unite.action.index.AddCartAction" method="addCartAddressList">
 <result name="success" type="json">
 <param name="includeProperties">


 pageUtil\.currentPage,pageUtil\.nextPage,
 list\[\d+\]\.udid,list\[\d+\]\.orderforms\[\d+\]\.orderform\.ofid


</param>
 </result>
 </action>

</package>
</struts>

 


jquery的ajax调用函数



$.ajax({
 type: "POST", //请求方式
 async: false,
 url: "addCartAddressList.action",
 dataType: "json",
 data: {
 randoms: Math.random(),
 key : values,
 sumintegral : $("#Point").text(),
 sumPrice :$("#ItemAmount").text()
 },
 success: function(data) {
 if(data)
 {
// var a= eval('('+data+')');
// var b=a['list'];
 alert(data.list[0].udid);
 //location.href="/users/consigneeAddress1.jsp";
 }else
 {
 //结账失败,可能是您的账号已经注销或未登录!
 alert("\u7ED3\u8D26\u5931\u8D25,\u53EF\u80FD\u662F\u60A8\u7684\u8D26\u53F7\u5DF2\u7ECF\u6CE8\u9500\u6216\u672A\u767B\u5F55\uFF01");
 }
 },
 error: function(e) {
 //服务器连接失败!
 alert("\u670D\u52A1\u5668\u8FDE\u63A5\u5931\u8D25\uFF01");
 }
 });

你可能感兴趣的:(apache,jquery,json,Ajax,struts)