首先 我在springmvc 配置文件 配置了默认的视图解析器
现在我写了一个注册页面
输入用户名 可以点击那个【是否被占用】来判断用户名是不是可以用,
当然这个就跳进了 springmvc的控制器中,但是我不想页面发生改变的啊,只是想弹出一个框 来提示用户 可以注册 或者 不可以注册
@RequestMapping("/verifyUser.html")
public String verifyUser(HttpServletRequest request){
boolean isExist = registerService.queryExist(request.getParameter("userName"));
if(isExist){
System.out.println("已经存在用户");
//已经存在用户
return "error";
}else{
System.out.println("可以注册");
return "register";
}
}
这是控制器里的方法,如果用了return的话 就跳转到视图解析器里了
但是我不想这么做
求大神 帮忙出出主意 帮个忙
http://blog.csdn.net/lijun_xiao2009/article/details/8052586
前台发ajax 后台判断后返回boolean 试试吧
function checkName(){
var customerName=$("#newCustomerName").val();
$.ajax({
type : "POST",
url : "/verifyUser.html",//这里地址就是你@RequestMapping括号的地址
dataType : 'json', //我的后台返回的是json
data : {
"customerName" : customerName,
},
success : function(data) {
if (data.code == "200") {
$("#nameCheckMsg").html("<font color='green'>"+"该用户名可以使用");
nameFlag=true;
} else if(data.code == "202"){
$("#nameCheckMsg").html("<font color='red'>"+"用户名不能为空");
nameFlag=false;
}else{
$("#nameCheckMsg").html("<font color='red'>"+"用户名已被注册");
nameFlag=false;
}
},
error : function() {
alert("系统繁忙,请稍后");
}
});
/**
检测用户名重复
*/
@RequestMapping(value="loginAction/checkCustomerName",method=RequestMethod.POST)
public void checkCustomerName(HttpSession session,HttpServletRequest request,HttpServletResponse response,
@RequestParam(value="customerName",required=true) String customerName){
if(customerName==null||customerName==""){
hashMap.put("code", 202);
printe(response);
}
else{
String flag=loginService.checkCustomerName(customerName);
hashMap.put("code", flag);
printe(response);
}
}
public void printe(HttpServletResponse response) {
WriteToPage writeTopage = new WriteToPage();
writeTopage.print(response, hashMap);
writeTopage = null; // 设置为空,让其回收
}
/**
public class WriteToPage {
public void print(HttpServletResponse response,Object obj){
PrintWriter pw;
try {
pw = response.getWriter();
pw.write(JSON.toJSONString(obj));
pw.flush();
pw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
嘛 我的hashmap是全局的 你可以直接在方法里面new一个 后面那些主要是把hashmap转成json输出 其实我搞完都觉得有点麻烦,你可以看情况换换
加个@ResponseBody 注解
点击连接触发事件,调用function,异步提交url返回json格式,解析json相关字段来判断是否已经被占用,具体流程如此
前台用ajax啊,要局部刷新,然后回来判断,就哦了