向Modal显示struts2操作错误消息

时间:2015-07-20 02:57:34

标签: javascript jquery twitter-bootstrap struts2 modal-dialog

现在我有一个注册模式#modal_A:当用户点击链接时,弹出modal_A对话框,然后用户可以提交他/她的infor,但是当用户名无效时,应该在模式上显示一些错误消息struts2。

以下是jsp页面上的注册表单:

<div class="modal fade" id="registerModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog modal-sm login-position" role="document">
    <div class="modal-cell">
      <div class="modal-content">
        <div class="login-modal">
            <div class="omb_login">
                <div class="row">
                    <div class="col-xs-11 login-left">  
                        <s:form class="omb_loginForm" name="register" theme="simple" action="RegisterAction" autocomplete="off" method="POST">
                            <div class="input-group">
                                <span class="input-group-addon"><i class="fa fa-user" style="font-size: 15px;"></i></span>
                                <s:textfield type="text" class="form-control line-height" name="username" id="username_register" placeholder="User name"/>
                            </div>
                            <span class="help-block"></span>

                            <div class="input-group">
                                <span class="input-group-addon"><i class="fa fa-envelope" style="font-size: 9px;"></i></span>
                                <s:textfield type="text" class="form-control line-height" name="email" id="email_register" placeholder="Email"/>
                            </div>
                            <span class="help-block"></span>

                            <div class="input-group">
                                <span class="input-group-addon"><i class="fa fa-phone-square" style="font-size: 13px;"></i></span>
                                <s:textfield type="text" class="form-control line-height" name="telephone" id="telephone_register" placeholder="Telephone"/>
                            </div>
                            <span class="help-block"></span>

                            <div class="input-group">
                                <span class="input-group-addon"><i class="fa fa-lock" style="font-size: 18px;"></i></span>
                                <s:textfield  type="password" class="form-control" name="password" id="password_register" placeholder="Password"/>
                            </div>
                            <span class="help-block"></span>
                            <s:submit id="hide" class="btn btn-lg login-btn-primary btn-block" type="submit" key="register.register"/>
                            <span class="help-block"></span>
                        </s:form>
                    </div>
                </div>
            </div>
        </div>
      </div>
    </div>
  </div>
</div>

这是struts动作:

<action name="RegisterAction" class="RegisterAction">
       <result name="success">/index.jsp</result>    
       <result name="input">/index.jsp</result>   
</action>

以下是验证人:

<validators>
<field name="username">
    <field-validator type="requiredstring">
        <param name="trim">true</param>
        <message>test!!!!!!!!!!</message>
    </field-validator>
</field>
</validators>

现在,当用户名为null时,我希望它可以在模态上显示消息。

但问题在于:当用户名为null时,会直接跳转到index.jsp页面但没有显示模态。那么如何解决呢?

1 个答案:

答案 0 :(得分:2)

Struts.xml添加结果类型...     

     <result-types>
                    <result-type name="json" class="org.apache.struts2.json.JSONResult"/>
            </result-types> 

Struts Action

        <action name="RegisterAction" class="RegisterAction">
        <result name="INPUT">/index.jsp</result>
        <result name="ERROR" type="json">/index.jsp</result>
        <interceptor-ref name="jsonValidationWorkflowStack"></interceptor-ref>
        <param name="ignoreHierarchy">false</param>
        <param name="includeProperties">actionErrors\[\d+\], fieldErrors\..+$, actionMessages\[\d+\]</param>
        <result name="success" type="json">/index.jsp</result>
        </action>

在Jsp页面

<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user" style="font-size: 15px;"></i></span>
    <input type="text" class="form-control line-height" name="username" id="username_register" placeholder="User name"/>
<input type="button"  id="username_submit" />
</div>

的Ajax

$(document).ready(function() {
       $("#username_submit").click(function(){
           var username=$("#username_register").val();
       $.ajax({
           type: 'GET',
           url:'RegisterAction?username='+encodeURIComponent(username),
           dataType: 'json',
           success: function(data){
            if(data.fieldErrors && !isEmpty(data.fieldErrors)){
           $.each(data.fieldErrors,function(index,value){ //indexfieldname,valuefiled?
          $("#username_register").before('<span>'+value[0]+'</span>');
            });
           return;
           } 



           },
                   error: function() 
                   {

                   }
        });
        return false;
       });});


function isEmpty(obj){//(Object obj = {}isEmpty=true)
    for(var p in obj){
        return false;
    }
    return true;
}

注意:添加struts2json插件Jar ..并在警告中你将有struts2验证错误...希望这将有助于你:)