日期:2014-05-16  浏览次数:20454 次

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