jsp
<input type="button" class="button" value="保存" onclick="addCus()"/>
javascript
function addCus(){
var name = document.getElementById("customerName").value;
if(name == ""){
alert("提示:顾客姓名不能为空!");
return false;
}
$.ajax( {
url : "<%=request.getContextPath()%>/customer/findIsExistByName.action",
type : "POST",
cache : false,
async : false,
data : {
"customerName" : name
},
dataType : "json",
success : function(json) {
/* alert(json[0].sex); json中有几条数据 ,json.length 就有几条数据,直接循环读取其中内容*/
if(json.length != 0 || json != ""){
alert("此顾客姓名已经存在!请重新输入");
return false;
}else{
var conValue = confirm("提示:你正在添加顾客信息,是否确定?");
if(conValue){
with (document.getElementById("myform")) {
method = "post";
action = "<%=request.getContextPath()%>/customer/addCus.action";
submit();
}
}
}
}
});
}
struts_xml
<!-- 添加时候 ,判断顾客姓名是否存在 -->
<action name="findIsExistByName" class="CustomerAction" method="findIsExistByName">
<result name="success">/gk/gk_msg.jsp</result>
<result name="error">/card/error.jsp</result>
<result name="exception">/card/exception.jsp</result>
</action>
action
private String json;
public String findIsExistByName(){
try {
List<CustomerInfo> cusList = customerService.getListCusByName(customerName);
json = com.alibaba.fastjson.JSON.toJSONString(cusList);
} catch (Exception e) {
e.printStackTrace();
this.addActionError("错误提示:对不起,系统暂时不能处理您<br>的请求,请稍候重试或与系统管理员联系!");
return "exception";
}
return SUCCESS;
}
gk_msg.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
${json}