struts2 的验证框架如何用ajax请求,
不可能使用他的验证框架必须要刷新页面吧?有高手知道怎么解决不?先谢谢了!

解决方案 »

  1.   

    用jQuery的ajax框架的,都封装好了,直接用,很方便的说
      

  2.   

    这个我知道,但ajax请求验证的时候,验证框架验证完会跳向
    <result name="input">中配置的页面,而不是返回错误信息啊
      

  3.   

    ajax是不会跳转页面的,即使你配置了<result name="input">
      

  4.   

    如果是验证框架的话,你试着修改下,把错误write回来。
      

  5.   

    你还是自己ajax到你后台的action 进行验证就可以了
    通过返回true 不通过返回false
    就可以了啊
    js$.ajax( {
      type : 'POST',
    url : 'xxx.action',
    data : {
    xx: 'xx',
    xxxx:xxxx

    },
    dataType : json,
    error : function() {
    $('#xxx').html('连接服务器超时,请稍后重试...');
    },
    success : function(data) {
                                          //判断返回的是true还是false
                                         //true就提交
    submit();
    }
    });javapublic String XXX(){
       //验证过程

    return null;
    }
      

  6.   

    public String XXX(){
       //验证过程

    return null;
    }
    return null;
    试试return NONE;
      

  7.   

    多谢各位,不过看来这验证框架用处也不怎么大啊,不能异步。
    唉,只有耦合到action里了
      

  8.   

    为什么不能异步?  自己写个result类好了啊,把错误信息打出来。
      

  9.   


    什么意思?完全可以啊:点击提交按钮触发这个方法:function postdata(){
    $.ajax({
    cache: true,
    type: "POST",
    url:"/GBGCB/coa/cus/updcusema/flow.action",
    data:$('#emailAddressEditForm').serialize(),
    async: false,
        error: function(request) {
            alert("error");
        },
        success: function(data) {
         $("#commonLayout_updcusema").parent().html(data);
        }
    });
    return true;
    };//action中的alidate方法,如果有错误消息,默认是返回到input页面,但是你用ajax提交的话页面是不会跳的
    public void validate() {
    // validate the  otherEmail
    if ( null == primaryEmail && !UpdcusemaCustomValidatorEmail.validate(otherEmail.getEmailAddress()) ){
    this.primaryEmail = ((UpdcusemaContext)getContext()).getEmail1();
    this.addActionError("<div class='h12 MainAreaText'>Other Email Address is not right.</div><br><br>" +
    "<div class='h12 MainAreaText'>* Presence of '@' symbol in e-mail address.</div><br>" +
    "<div class='h12 MainAreaText'>* At least 1 symbol before '@' symbol.</div><br>" +
    "<div class='h12 MainAreaText'>* Presence of '.' symbol after '@' symbol.</div><br>" +
    "<div class='h12 MainAreaText'>* Presence of at least 2 symbols after '.' symbol that's set after '@' symbol.</div><br>" +
    "<div class='h12 MainAreaText'>* No leading and trailing spaces.</div><br>" +
    "<div class='h12 MainAreaText'>* Uppercase and lowercase English letters (a - z, A - Z) can be used.</div><br>" +
    "<div class='h12 MainAreaText'>* Digits 0 to 9 can be used.</div><br>" +
    "<div class='h12 MainAreaText'>* Characters !, #, $, %, &, ', *, +, -, /, =, ?, ^, _, `, {, |, }, ~ can be used.</div>"
    );
    } // validate primary email
    if ( null == otherEmail && !UpdcusemaCustomValidatorEmail.validate(primaryEmail.getEmailAddress()) ) {
    this.otherEmail = ((UpdcusemaContext)getContext()).getEmail2();
    System.out.println("primaryEmail.getEmailAddress():"+primaryEmail.getEmailAddress());
    System.out.println("otherEmail.getEmailAddress():"+otherEmail.getEmailAddress());
    this.addActionError("<div class='h12 MainAreaText'>Primary Email Address is not right.</div><br><br>" +
    "<div class='h12 MainAreaText'>* Presence of '@' symbol in e-mail address.</div><br>" +
    "<div class='h12 MainAreaText'>* At least 1 symbol before '@' symbol.</div><br>" +
    "<div class='h12 MainAreaText'>* Presence of '.' symbol after '@' symbol.</div><br>" +
    "<div class='h12 MainAreaText'>* Presence of at least 2 symbols after '.' symbol that's set after '@' symbol.</div><br>" +
    "<div class='h12 MainAreaText'>* No leading and trailing spaces.</div><br>" +
    "<div class='h12 MainAreaText'>* Uppercase and lowercase English letters (a – z, A – Z) can be used.</div><br>" +
    "<div class='h12 MainAreaText'>* Digits 0 to 9 can be used.</div><br>" +
    "<div class='h12 MainAreaText'>* Characters !, #, $, %, &, ', *, +, -, /, =, ?, ^, _, `, {, |, }, ~ can be used.</div>"
    );
    }
    }
      

  10.   

    虽然后不是我想要的,但还是谢谢各位的热心啊,我已经解决了,在这分享下,等下贴博客去一:自己定义一个result
    package result;import java.io.PrintWriter;
    import java.util.Map;import org.apache.struts2.ServletActionContext;
    import net.sf.json.JSONObject;
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.Result;
    import com.opensymphony.xwork2.util.ValueStack;public class validators_json implements Result { @SuppressWarnings("unchecked")
    @Override
    public void execute(ActionInvocation arg0) throws Exception {
    //获值栈中fieldErrors的值
    ValueStack vc = ActionContext.getContext().getValueStack();
    Map<String, String[]>  ferrors = (Map<String,String[]>)  vc.findValue("fieldErrors");
    //获得输出流
    ServletActionContext.getResponse().setCharacterEncoding("utf8");
    ServletActionContext.getResponse().setContentType("text/html");
    PrintWriter out = ServletActionContext.getResponse().getWriter();
    //将map转换为json
    JSONObject json =JSONObject.fromObject(ferrors);
    //想客户端输出
    System.out.println(json.toString());
    out.print(json.toString());
    out.close();
    // for (Map.Entry<String, String[]> entry : ferrors.entrySet())
    //    System.out.println("key:" + entry.getKey() + "  value:" + entry.getValue()); }}二,然后修改配置文件struts.xml添加type的定义
    <result-types>
    <result-type name="validators_json" class="result.validators_json" default="true">   </result-type>
    </result-types>
    使用
    <result name="input" type="validators_json"></result> 
    这样 验证框架所产生的message就可以以json的方式发向客户端了,当然发送方式可以自己定义
    实现了ajax+struts2验证框架异步验证数据。唉,菜鸟啊,搞了大半天
      

  11.   

    用jdom.jar包,配置XML如下
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
    <struts>
    <package name="ajax" extends="json-default">
    <!-- ajax -->
    <action name="ajax_*" class="com.jlzhongdong.ajax.action.AjaxAction" method="{1}">
    <result type="json"></result>
    </action>
    </package>
    </struts>