Spring3 MVC中Ajax的使用
来源:互联网 发布:淘宝上发票抬头写个人 编辑:程序博客网 时间:2024/04/30 09:59
来源:http://hhdem.com/2012/01/30/spring3-mvc-ajax-sample/
现在已经开始使用Spring3了,MVC虽然用了很久不过Ajax一直通过DWR实现,不过既然Spring3已经支持Ajax访问,那就必须折腾下了。
1. 首先,新建Controller类用于接收ajax请求,这里建立AjaxCheckController,目的是检查注册用户是否已经存在
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@Controller
@RequestMapping
(
"/a"
)
public
class
AjaxCheckController
extends
BaseController {
@Autowired
private
UserManager userManager;
@RequestMapping
(value =
"/checkexists/loginname/{loginName}"
, method = RequestMethod.GET)
public
@ResponseBody
Map<String, Object> checkLoginNameExists(
@PathVariable
String loginName) {
User user = userManager.findUserByLoginName(loginName);
Map<String, Object> modelMap =
new
HashMap<String, Object>();
modelMap.put(
"exists"
, user !=
null
);
return
modelMap;
}
@RequestMapping
(value =
"/checkexists/email"
, method = RequestMethod.GET)
public
@ResponseBody
Map<String, Object> checkEmailExists(
@RequestParam
(value =
"email"
, required =
true
) String email) {
User user = userManager.findUserByEmail(email);
Map<String, Object> modelMap =
new
HashMap<String, Object>();
modelMap.put(
"exists"
, user !=
null
);
return
modelMap;
}
}
2. 请求页面,在这里就是注册页面了,部分js代码如下:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
function
loginOnChange(event) {
....
$.ajax({
type :
'GET'
,
contentType :
'application/json'
,
url :
'a/checkexists/loginname/'
+loginname,
dataType :
'json'
,
success :
function
(data) {
if
(data && data.exists) {
log_tip_div.style.display =
"inline"
;
log_tip.innerHTML=
"<img src='<@common.basePath/>/img/exclamation.gif'/> 该用户名已经被使用"
;
canSub =
false
;
loginUserError =
true
;
isLoginUserExists =
true
;
}
else
{
log_tip_div.style.display =
"inline"
;
log_tip.innerHTML=
"<img src='<@common.basePath/>/img/fit.gif'/> 用户名可以使用"
;
canSub =
true
;
loginUserError =
false
;
isLoginUserExists =
true
;
}
},
error :
function
() {
alert(
"error"
)
}
});
.....
}
function
emailOnChange(event) {
....
$.ajax({
type :
'GET'
,
contentType :
'application/json'
,
url :
'a/checkexists/email'
,
data:
'email='
+email,
dataType :
'json'
,
success :
function
(data) {
if
(data && data.exists) {
email_tip_div.style.display =
"inline"
;
email_tip.innerHTML=
"<img src='<@common.basePath/>/img/exclamation.gif'/> 该邮箱已经注册过"
;
canSub =
false
;
emailError =
true
;
isEmailExists =
true
;
}
else
{
email_tip_div.style.display =
"inline"
;
email_tip.innerHTML=
"<img src='<@common.basePath/>/img/fit.gif'/> 邮箱未被注册过,可以使用"
;
canSub =
true
;
emailError =
false
;
isEmailExists =
true
}
},
error :
function
() {
alert(
"error"
)
}
});
....
}
到此就可以了,可以通过jquery轻松访问Spring的Controller,是不是很简单呀!
- Spring3 MVC中Ajax的使用
- spring3.0.5MVC ajax+json 的问题
- Asp.net MVC 中Ajax的使用
- 解决spring3 mvc中乱码的问题。
- 解决spring3 mvc中乱码的问题。
- 解决spring3 mvc中乱码的问题。
- 解决spring3 mvc中乱码的问题。
- 解决spring3 mvc中乱码的问题
- 解决spring3 mvc中乱码的问题。
- 解决spring3 mvc中乱码的问题。
- Spring3 MVC 的介绍和使用
- Spring3 MVC 的介绍和使用
- spring3 MVC+jquery+ajax+annotations
- Spring3.1 MVC和jQuery ajax的集成
- Spring3 MVC + jQuery easyUI 做的ajax版本用户管理
- Spring3 MVC + jQuery easyUI 做的ajax版本用户管理
- mvc中使用Ajax.ActionLink
- mvc中使用ajax方法
- 匹配时刻
- QSqlDatabase: QMYSQL driver not loaded 解决方法
- 导师制
- Windows Phone开发环境搭建
- strcpy函数实现
- Spring3 MVC中Ajax的使用
- 开发基于ADO的Delphi数据库应用程序
- java动态代理设计模式
- EEG & MCI 第二天 NoteExpress
- 杂感
- 总结
- Android™ 2.1系统图标
- 字符串拷贝
- 指针变量传递给函数