Struts2:如何实现action与jsp的数据交互

简单记录下,方便自己日后的使用

jsp页面:

//校验手机号码绑定客户-输入手机号就校验 
function checkTel(){
            var userType = $('#userType').val();
            var mobile = "";
            if(userType == '8'){
                mobile = $("#organTel").val();
                $.ajax({
                    url : "${basePath}/macaoAction/customer_checkTel.do",
                    type : "post",
                    dataType : "json",
                    data: {
                        "macaoCardCustomer.userType":userType,
                        "macaoCardCustomer.organTel":mobile
                        },
                    success:function(data) {
                        loadingHide();
                        if(data.count > '0'){
                            alertMsg("该手机号码无法用于办理业务,一个手机号码仅能绑定一个个人客户。");
                       }
                    },
                    error : function() {
                        loadingHide();
                        alert("网络异常,稍后请重试");
                    }
                });
            }else{
                mobile = $("#agentTel").val();
                 $.ajax({
                     url : "${basePath}/macaoAction/customer_checkTel.do",
                     type : "post",
                     dataType : "json",
                     data: {
                         "macaoCardCustomer.userType":userType,
                         "macaoCardCustomer.agentTel":mobile
                         },
                     success:function(data) {
                         loadingHide();
                         if(data.count > '4'){
                                alertMsg("该手机号码无法用于办理业务,一个手机号码绑定的单位客户数量不能大于5个。");
                        }
                     },
                     error : function() {
                         loadingHide();
                         alert("网络异常,稍后请重试");
                     }
                 });
            }
        }


action中的方法:

//检查手机号-前端调用
public void checkTel(){
    String mobile = null;
    String userType = macaoCardCustomer.getUserType();
    JSONObject json=new JSONObject();
    if("8".equals(userType)) {
         mobile = macaoCardCustomer.getOrganTel();
    }else {
         mobile = macaoCardCustomer.getAgentTel();
    }
        
    count = macaoService.queryCustomerCountByTel(mobile, userType);
    json.accumulate("count", count);
    outMessage(json.toString());
}

关键点:
1、ajax中的URL是进入action中方法的路径,要在统一认证那里配置

2、data里面的是传入action的数据

3、data.***是从action里面获取的数据,要定义***全局变量和对应的getter/setter方法,还要存在json里面
 

猜你喜欢

转载自blog.csdn.net/Javaxiaobaismc/article/details/100926486