2012-05-03 107 views
2

我有一个Ajax表单验证问题。我使用的是struts-core 2.3.1.2,struts-jquery-plugin 3.3.0和struts-json-plugin。Struts Ajax表单验证

如果ajax表单将由ajax请求提交并且验证失败,则会出现问题。因此,整个表单将被放置在结果元素上。因此,您可以在ajax sumbit按钮上激活ajax验证。 http://code.google.com/p/struts2-jquery/wiki/Validation

这里也是过时的信息: http://struts.apache.org/2.2.3.1/docs/ajax-validation.html

但拦截 “jsonValidationWorkflowStack” 在struts-default.xml中缺少像写在帖子:jsonValidationWorkflowStack seems to be removed in Struts 2.3.1

据源出到struts- json插件在struts-plugin.xml中。我不知道我怎么可以直接使用,但我建立我自己的堆栈中的struts.xml:

<!-- Sample JSON validation stack --> 
<interceptor-stack name="jsonValidationWorkflowStack"> 
      <interceptor-ref name="basicStack"/> 
      <interceptor-ref name="validation"> 
       <param name="excludeMethods">input,back,cancel</param> 
      </interceptor-ref> 
      <interceptor-ref name="jsonValidation"/> 
      <interceptor-ref name="workflow"/> 
     </interceptor-stack> 
    </interceptors> 
<action name="updateMySettings" method="execute" class="de.ra.daod.actions.MyAppSettingAction"> 
     <interceptor-ref name="jsonValidationWorkflowStack"/> 
        <!-- This is not beauty within ajax --> 
     <result name="input">/WEB-INF/jsp/mysetting_ajax.jsp</result> 
     <result name="success" type="stream"> 
      <param name="contentType">text/html</param> 
      <param name="inputName">inputStream</param> 
     </result> 
    </action> 

我的形式看起来像:

<s:head /> 
<sj:head /> 
    <!-- This files are needed for AJAX Validation of XHTML Forms --> 
    <script src="${pageContext.request.contextPath}/struts/xhtml/validation.js" type="text/javascript"></script> 

<s:form id="form" action="private/updateMySettings" theme="xhtml"> 
       <s:textfield id="screenRes" key="appSetting.screenResolution" label="Screen resolution" required="true" /> 
<s:select key="appSetting.screenDepth" label="Color depth" list="#{'8':'8','16':'16','24':'24'}" required="true" /> 
<sj:submit value="Update Settings" targets="status" validate="true"/> 
      </s:form> 

不幸的是,我得到一个javascript错误如果验证失败,或不:

Uncaught TypeError: Object #<Object> has no method 'indexOf' 
f.extend.ajax jquery-1.7.1.min.js:4 
b.fn.ajaxSubmit 
a.struts2_jquery.validateForm jquery.struts2-3.3.0.min.js:18 
a.subscribeHandler.h.beforeSubmit jquery.struts2-3.3.0.min.js:18 
b.fn.ajaxSubmit 
a.subscribeHandler.e jquery.struts2-3.3.0.min.js:18 
e.extend.each jquery-1.7.1.min.js:2 
a.subscribeHandler.e jquery.struts2-3.3.0.min.js:18 
f.event.dispatch jquery-1.7.1.min.js:3 
f.event.add.h.handle.i jquery-1.7.1.min.js:3 
f.event.trigger jquery-1.7.1.min.js:3 
f.fn.extend.trigger jquery-1.7.1.min.js:3 
e.extend.each jquery-1.7.1.min.js:2 
e.fn.e.each jquery-1.7.1.min.js:2 
f.fn.extend.trigger jquery-1.7.1.min.js:3 
d.fn.extend.publish jquery.subscribe.min.js:16 
e.extend.each jquery-1.7.1.min.js:2 
d.fn.extend.publish jquery.subscribe.min.js:16 
(anonymous function) jquery.struts2-3.3.0.min.js:18 
f.event.dispatch jquery-1.7.1.min.js:3 
f.event.add.h.handle.i jquery-1.7.1.min.js:3 

看来,从响应JSON对象无法处理,我不知道为什么因为我遵循旧的指示。我假设原因是struts/utils.js中的StrutsUtils.getValidationErrors函数,如果该函数与json对象一起使用,但我不确定。谁能帮忙?

+0

没有人有想法吗?必须有可能在struts 2.3.1.2中验证ajax请求吗?由于最新的struts版本不支持jsonValidationWorkflowStack,我添加了插件struts-json-plugin 2.3.1.2,其中jsonValidationWorkflowStack可用,但它只存在具有上述结果的旧指导。谁能帮忙? – Caphalor40k

回答

0

你正在解决Struts2 Issue。升级到最新版本2.3.3或2.3.4,你的问题应该消失。

+0

非常感谢,这解决了我的问题与JavaScript错误。它工作良好,但并不完美,因为如果我发送没有验证错误的表单,流式结果将被放置在目标(div容器)上,然后我发送带有验证错误的表单,并且此目标容器中的内容不会删除,因此它可能会令人困惑。我认为如果我们有验证错误,成功请求的结果将被删除?谢谢 – Caphalor40k

+0

我猜想流结果和ajax不能很好地一起玩。只需删除目标属性。 – Johannes

1

我猜想流结果和ajax在一起玩的不好。只需删除目标 属性即可。 - jogep

我不同意你,因为AJAX的男高音没有加载新的页面,而是将任何东西加载到当前页面,这就是为什么我使用AJAX提交按钮。注意动作的用户而不重新加载页面本身。解决方法是使用javascript清除状态div元素的内容。我认为这可以在struts2-jquery-plugin中自动完成。这是我嵌入选项卡窗格中的表单。

<%@ page language="java" contentType="text/html; charset=UTF-8" 
pageEncoding="UTF-8"%> 
<%@ taglib prefix="s" uri="/struts-tags" %> 
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%> 

<s:if test="appSetting != null"> 
<h3>Application settings</h3> 
<div id="status" class="welcome"></div> 
<table> 
    <tr> 
     <td> 
      <s:form id="form" action="updateMySettings" theme="xhtml"> 
       <s:textfield id="screenRes" key="appSetting.screenResolution" 
        label="Screen resolution" required="true" /> 
       <s:select key="appSetting.screenDepth" label="Color depth" 
        list="#{'8':'8','16':'16','24':'24'}" required="true" /> 
       <sj:submit id="updateSetting" value="Update Settings" targets="status" validate="true" /> 
      </s:form> 
     </td> 
     <td valign="top"> 
      <button id="fullScreen">Use full screen</button> 
      <button id="fullBrowser">Use full browser</button> 
     </td> 
    </tr> 
</table> 
<script> 
$("#fullScreen").click(function() { 
    scr_width = Math.max(screen.width,screen.height); 
    scr_height = Math.min(screen.width,screen.height); 
    $("#screenRes").val(scr_width + 'x' + scr_height); 
}); 
$("#fullBrowser").click(function() { 
    brw_width = Math.max(window.innerWidth, window.innerHeight); 
    brw_height = Math.min(window.innerWidth, window.innerHeight); 
    $("#screenRes").val(brw_width + 'x' + brw_height); 
}); 
$("#updateSetting").click(function() { 
    $("#status").empty(); 
}) 
</script> 
</s:if> 
<s:else> 
<p>No settings available.</p> 
</s:else> 

它工作得很好。