Struts2 AJAX表单提交 - Action被调用两次

时间:2013-07-03 12:46:51

标签: java jquery ajax struts2

我正在尝试使用struts2中的ajax提交表单。因此我使用jQuery链接提交表单。通过此链接,我调用我的操作,该操作对我的数据库执行SQL查询,并插入包含数据的新记录或用户填写表单。嗯,这就是计划,实际上它是如何运作的。但由于某些原因我的行动被召唤两次,我真的不知道为什么......

这是我的JSP:

<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="sjg" uri="/struts-jquery-grid-tags"%>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

    <s:form id="formId" name ="formId" theme="simple" method="post" action="" >
        <s:textfield    id="txt1"   name="txt1" />
        <sj:spinner     id="spr1"   name="spr1" />          
    </s:form>   

    <s:url var="createtesturl" action="create-test"/> 
    <sj:a   id="linkId"
            dataType="json"
            href="%{createtesturl}"
            onSuccessTopics="handleJsonPeriodNew"
            formIds="formId"
            button="true" 
            buttonIcon="ui-icon-gear"
    >Submit</sj:a>

这是我的行动(这里执行方法被调用两次):

@ParentPackage(value = "json-default")
public class CreateTestAction extends ActionSupport {
    private static final long serialVersionUID = 2864711857038235887L;
    private static final Logger log = LoggerFactory.getLogger(CreateTestAction.class);

    private String              txt1;
    private String              spr1;
    private Boolean             success = false;

    @Actions( {
        @Action(value = "/create-test", results = {
            @Result(name = "success", type = "json")
        })
    })

    public String execute()
    {       
        DbCon dbCon = null;

        try
        {
            dbCon= new DbCon();
            success = dbCon.createNewTest(txt1, spr1);
        }
        catch (Exception e)
        {
            log.error("Exception", e);
        }
        finally
        {
            if (dbCon!= null)
            {
                dbCon.close();
                dbCon= null;
            }
        }

        return SUCCESS;
    }



    //Getters and Setters for Attributes

    public String getTxt1() {
        return txt1;
    }

    public void setTxt1(String txt1) {
        this.txt1= txt1;
    }

    public String getSpr1() {
        return spr1;
    }

    public void setSpr1(String spr1) {
        this.spr1= spr1;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public Boolean getSuccess() {
        return success;
    }
}

愿任何人都可以帮助我吗?!

0 个答案:

没有答案