简体   繁体   English

struts2中的ajax字段验证不会重新加载相同的表单

[英]ajax field validation in struts2 not reloading the same form

I've been trying to use the field-validation framework in Struts2 and have it implemented. 我一直在尝试在Struts2中使用字段验证框架并将其实现。 The problem is when doing an AJAX call with jquery tags, it targets a resulting div when I want to reload the same page with the fielderror. 问题是当使用jquery标签进行AJAX调用时,当我想用​​fielderror重新加载同一页面时,它针对的结果是div。

<div class="myform">
    <s:form id="userForm" action="users" theme="simple">
        <h1>Search for Users</h1>
        <p><s:fielderror /></p>
        <label>Username
            <span class="small">Add your name</span>
        </label>
        <s:textfield id="username" name="username"/>
        <label>Point Minimum
            <span class="small">Min. 0 points</span>
        </label>
        <s:textfield name="pointMin"/>

        <label>Point Maximum
            <span class="small">Max. 5000 points</span>
        </label>
        <s:textfield name="pointMax"/>

        <label>Rate Minimum
            <span class="small">Percentage</span>
        </label>
        <s:textfield id="rateMin" name="rateMin"/>

        <label>Rate Maximum
            <span class="small">Percentage</span>
        </label>
        <s:textfield id="rateMax" name="rateMax"/>

        <s:hidden name="searchButtonHit" value="%{true}"/>
        <sj:a formIds="userForm" 
              targets="listingDisplay" 
              button="true"
              validate="only"
              type="submit"> Search
        </sj:a>
        <div class="spacer"></div>
    </s:form>
</div>
<div class="spacer"></div>
<div id="listingDisplay"></div>

The <sj:a> tag targets the listingDisplay div so the fielderror as well as the same form is loaded underneath which looks very strange. <sj:a>标记以listingDisplay div为目标,因此在下面加载了fielderror以及相同的表单,这看起来很奇怪。 Is there a better way of doing this? 有更好的方法吗? Thanks! 谢谢!

Have a look at the below validation link. 请看下面的验证链接。 It adds a string errorhere to the error message and checks for it after response. 它将字符串错误添加到错误消息中,并在响应后检查它。 You can also do this to reload the page and the reload code can be written in 's onSuccessTopics. 您也可以执行此操作以重新加载页面,并且可以在onSuccessTopics中编写重新加载代码。

Ajax Validation Ajax验证

Method 2 方法2

Very simple but may not work on all browsers. 非常简单,但可能不适用于所有浏览器。 Set the targets attribute of 设置的目标属性

 <sj:a targets="myform">

to the same div, within which the form exists. 到存在该表格的同一个div中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM