1

我正在尝试在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;
    }
}

谁能帮助我?!

4

0 回答 0