(1)前台页面的参数提交方法
$.ajax({
url : '${pageContext.request.contextPath}/userAction!reg',data : $('#index_reFrom').serialize(), //serialize()这个方法序列表表格内容为字符串,这样就省的像下面那样一个一个写了
// {
// name : $('#index_reFrom input[name=name]').val(),
// pwd : $('#index_reFrom input[name=pwd]').val
// },
dataType : 'json', //指定后台返回类型为json格式的
success : function(data, textStatus, jqXHR) {
if (data.success) {
$('#index_regDialog').dialog('close');
}
$.messager.show({
title : '提示',
msg : data.msg,
showType : 'slide'
});
}
});
(2)后台接受前台参数,并返回值给前台
public void reg(){
String name=ServletActionContext.getRequest().getParameter("name");
String pwd=ServletActionContext.getRequest().getParameter("pwd");
Map<String, Object> m = new HashMap<>();
try {
userService.save(name,pwd);
m.put("success", true);
m.put("msg", "注册成功!");
} catch (Exception e) {
e.printStackTrace();
m.put("success", true);
m.put("msg", "注册失败!");
}
try {
ServletActionContext.getResponse().setCharacterEncoding("utf-8");
ServletActionContext.getResponse().getWriter().write(JSON.toJSONString(m)); //将m转换为json字符串
} catch (IOException e) {
e.printStackTrace();
}